Skip to content

Commit

Permalink
fix a Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
a793181018 committed Nov 26, 2024
1 parent 5398079 commit 1d8f8e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 130 deletions.
70 changes: 30 additions & 40 deletions prompts/genius/zh-cn/code/auto-method.vm
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,72 @@

1. **类信息:**
* **类名:** `[$context.classInfo.name]`
#if($context.classInfo.fields.size() > 0)
* **成员变量:**
#foreach($field in $context.classInfo.fields)
* `[$field.accessModifier] [$field.type] [$field.name]` - `[$field.xmlDoc]`
#end
* `[$field.accessModifier] [$field.type] [$field.name]` - `#if($field.doc)$field.doc#else无#end`
#end
* ...
#end
#if($context.classInfo.propertyInfos.size() > 0)
* **属性访问器:**
#foreach($propertyInfo in $context.classInfo.propertyInfos)
* `[$propertyInfo.accessModifier] [$propertyInfo.type] [$propertyInfo.name]` - `[$propertyInfo.doc]`
* `[$propertyInfo.accessModifier] [$propertyInfo.type] [$propertyInfo.name]` - `#if($propertyInfo.doc)$propertyInfo.doc#else无#end`
* `get` - `[get 描述]`
* `set` - `[set 描述]`
#end
* ...
* **类成员数据结构:**

* `[数据结构类型] [数据结构名]` - `[数据结构描述]`
* **成员:**
* `[成员类型] [成员名]` - `[成员描述]`
* ...
* ...
#end
#if($context.classInfo.completedMethods.size() > 0)
* **已完成成员方法:**
#foreach($method in $context.classInfo.completedMethods)
* `[method.name]` - `[method.methodXmlDoc]`
* **参数:**
#foreach($method in $context.classInfo.completedMethods)
* **方法签名:** `[$method.name]`
* **方法描述:** `#if($method.methodDoc)$method.methodDoc#else无#end`
* **参数:**
#foreach($param in $method.parameters)
* `[param.type] [param.name]` - `[param.doc]`
#end
* ...
* **返回值:** `[method.returnType]` - `[method.returnDoc]`
* `[$param.type] [$param.name]` - `#if($param.doc)$param.doc#else无#end`
#end
* ...
* ...
* **返回值:** `[$method.returnType]` - `#if($method.returnDoc)$method.returnDoc#else无#end`
#end
#end


#if($context.classInfo.unfinishedMethods.size() > 0)
2. **未完成方法信息:**
#foreach($method in $context.classInfo.unfinishedMethods)
* **方法签名:** `[$method.name]`
* **方法描述:** `[$method.methodXmlDoc]`
* **方法描述:** `#if($method.methodDoc)$method.methodDoc#else无#end`
* **参数:**
#foreach($param in $method.parameters)
* `[$param.type] [$param.name]` - `[$param.doc]`
* `[$param.type] [$param.name]` - `#if($param.doc)$param.doc#else无#end`
#end
* ...
* **返回值:** `[$method.returnType]` - `[method.returnDoc]`
* **返回值:** `[$method.returnType]` - `#if($method.returnDoc)$method.returnDoc#else无#end`
#end
#end

#if($context.customFrameworkCodeFragments.size() > 0)
3. **相关代码片段:**
#foreach($customFrameworkCodeFragment in $context.customFrameworkCodeFragments)
* **片段 :**
```csharp
$customFrameworkCodeFragment.code
```
* **描述:** `[customFrameworkCodeFragment.doc]`
* **描述:** `#if($customFrameworkCodeFragment.doc)$customFrameworkCodeFragment.doc#else无#end`
#end
#end

#if($context.codeSamples.size() > 0)
4. **可以仿照的代码示例:**
#foreach($codeSample in $context.codeSamples)
* **示例 1:**
```csharp
$codeSample.code
```
* **描述:** `codeSample.doc`
* **描述:** `#if($codeSample.doc)$codeSample.doc#else无#end`
#end
#end


**要求:**

Expand All @@ -80,19 +86,3 @@
* 保持代码风格一致,遵循 C# 编码规范。
* 使用适当的命名、缩进和注释。

**示例:**

```csharp
/// <summary>
/// [方法描述]
/// </summary>
/// <param name="[参数名]">[参数描述]</param>
/// <returns>[返回值描述]</returns>
public [返回值类型] [方法名]([参数类型] [参数名])
{
// [方法体注释]
[代码实现]
}
```

**请根据以上模板,完成 `[类名]` 类中未实现的方法 `[方法名]`。**
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,39 @@ export class FrameworkCodeFragment implements IDataStorage {
for (let j = 0; j < declarationList.children.length; j++) {
if (
declarationList.children[j].type == 'method_declaration' ||
declarationList.children[j].type == 'field_declaration'||
declarationList.children[j].type == 'field_declaration' ||
declarationList.children[j].type == 'constructor_declaration'
) {
let node = declarationList.children[j];
let block: SyntaxNode | undefined = undefined;
let isPublic = false;
for (let k = 0; k < node.children.length; k++) {
let nodeChild = node.children[k];
if (nodeChild.type == 'modifier') {
if (nodeChild.text != 'public') {
isPublic = false;
codeTemp = codeTemp.replace(node.text, '');
let previousSibling = node.previousSibling;
let codeTemp1: string = '';
needDeleteNodes.push(node);
while (previousSibling) {
if (previousSibling.type == 'comment') {
needDeleteNodes.push(previousSibling);
codeTemp1 = previousSibling.text + codeTemp1;
previousSibling = previousSibling.previousSibling;
} else {
break;
}
}

codeTemp = codeTemp.replace(codeTemp1, '');
} else {
isPublic = true;
}
}
if (nodeChild.type == 'block' && isPublic) {
codeTemp = codeTemp.replace(node.children[k].text, '');
needDeleteNodes.push(node.children[k]);
block = node.children[k];
}
}
if (!isPublic) {
let previousSibling = node.previousSibling;
while (previousSibling) {
if (previousSibling.type == 'comment') {
needDeleteNodes.push(previousSibling);
previousSibling = previousSibling.previousSibling;
} else {
break;
}
}
needDeleteNodes.push(node);
} else if (block) {
needDeleteNodes.push(block);
}
}
}
Expand All @@ -141,10 +141,8 @@ export class FrameworkCodeFragment implements IDataStorage {
console.log(result);
this.code = result;
}

}
}else
{
} else {
this.code = code;
}
this.filePath = filePath;
Expand Down Expand Up @@ -192,83 +190,31 @@ export class FrameworkCodeFragment implements IDataStorage {
return frameworkCodeFragment;
}
}

class TreeSitterContentModifier {
private root: SyntaxNode;

constructor(root: SyntaxNode) {
this.root = root;
this.root = root;
}

public removeChildContent(children: SyntaxNode[]): string {
let rootContent = this.root.text;
// 对子节点按 startPosition 进行排序,确保从后往前移除内容
children.sort((a, b) => {
const startA = a.startPosition;
const startB = b.startPosition;
if (startA.row === startB.row) {
return startB.column - startA.column;
}
return startB.row - startA.row;
});

// 从后往前移除子节点的内容
for (const child of children) {
const startPos = child.startPosition;
const endPos = child.endPosition;

// 计算子节点内容的起始和结束位置在根节点内容中的索引
let startIndex = this.calculateIndex(rootContent, startPos);
let endIndex = this.calculateIndex(rootContent, endPos);

console.log(`Removing node: ${child.type}, start: ${JSON.stringify(startPos)}, end: ${JSON.stringify(endPos)}`);
console.log(`Start index: ${startIndex}, End index: ${endIndex}`);

// 确保删除节点的所有部分,包括节点前后的空白字符和换行符
let startRow = startPos.row;
let endRow = endPos.row;
let startColumn = startPos.column;
let endColumn = endPos.column;

// 删除节点前的空白字符和换行符
while (startRow > 0 && /\s/.test(rootContent[startIndex - 1])) {
startIndex--;
if (rootContent[startIndex] === '\n') {
startRow--;
startColumn = 0;
} else {
startColumn--;
}
}

// 删除节点后的空白字符和换行符
while (endRow < rootContent.split('\n').length - 1 && /\s/.test(rootContent[endIndex])) {
endIndex++;
if (rootContent[endIndex] === '\n') {
endRow++;
endColumn = 0;
} else {
endColumn++;
}
}

// 一次性删除计算出的完整范围
rootContent = rootContent.slice(0, startIndex) + rootContent.slice(endIndex);
}
return rootContent;
let rootContent = this.root.text;
// 对子节点按 startIndex 进行排序,确保从后往前移除内容
children.sort((a, b) => b.startIndex - a.startIndex);

// 从后往前移除子节点的内容
for (let i = 0; i < children.length; i++) {
let startIndex = children[i].startIndex;
let endIndex = children[i].endIndex;
let endNote = rootContent.charAt(endIndex - i);
let startNote = rootContent.charAt(startIndex - i);
while (endIndex < rootContent.length && /\s/.test(rootContent[endIndex])) {
endIndex++;
}
// 一次性删除计算出的完整范围
rootContent = rootContent.slice(0, startIndex) + rootContent.slice(endIndex);
}
return rootContent;
}

private calculateIndex(content: string, point: Point): number {
let index = 0;
for (let row = 0; row < point.row; row++) {
const newlineIndex = content.indexOf('\n');
if (newlineIndex === -1) {
throw new Error(`Invalid point: row ${row} not found in content`);
}
index += newlineIndex + 1;
content = content.slice(newlineIndex + 1);
}
index += point.column;
return index;
}
}

0 comments on commit 1d8f8e8

Please sign in to comment.