Skip to content

Commit 8d8cb95

Browse files
authored
Merge pull request #550 from devchat-ai/fix_code_completion_function_range_error
Refactor findFunctionRanges to Improve Performance and Readability
2 parents 826806e + 740b27f commit 8d8cb95

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/contributes/codecomplete/ast/findFunctions.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
3737
if (!querySource) {
3838
return [];
3939
}
40-
40+
4141
const extension = filepath.split('.').pop() || '';
4242
let query: Parser.Query | undefined = functionCache.get(extension);
4343
if (!query) {
@@ -46,17 +46,18 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
4646
}
4747
const matches = query?.matches(node);
4848

49-
return (
50-
matches?.flatMap((match) => {
49+
const functionRanges: FunctionRange[] = [];
50+
if (matches) {
51+
for (const match of matches) {
5152
// find functionNode through tag name
5253
const functionNode = match.captures.find((capture) => capture.name === "function")?.node;
5354
const bodyNode = match.captures.find((capture) => capture.name === "function.body")?.node;
5455
const nameNode = match.captures.find((capture) => capture.name === "function.name")?.node;
55-
if (!functionNode ||!bodyNode) {
56-
return [];
56+
if (!functionNode || !bodyNode) {
57+
continue;
5758
}
5859

59-
const results = {
60+
const functionRange: FunctionRange = {
6061
define: {
6162
start: {
6263
row: functionNode.startPosition.row,
@@ -77,11 +78,26 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
7778
column: bodyNode.endPosition.column,
7879
},
7980
},
80-
name: nameNode?.text?? "",
81+
name: nameNode?.text ?? "",
8182
};
82-
return results;
83-
}) ?? []
84-
);
83+
84+
// Check if this function range is not fully contained within another function range
85+
const isContained = functionRanges.some(range => {
86+
return (
87+
range.define.start.row <= functionRange.define.start.row &&
88+
range.define.end.row >= functionRange.define.end.row &&
89+
range.body.start.row <= functionRange.body.start.row &&
90+
range.body.end.row >= functionRange.body.end.row
91+
);
92+
});
93+
94+
if (!isContained) {
95+
functionRanges.push(functionRange);
96+
}
97+
}
98+
}
99+
100+
return functionRanges;
85101
}
86102

87103
export async function findFunctionNodes(filepath: string, node: Parser.SyntaxNode): Promise<Parser.SyntaxNode[]> {

src/contributes/codecomplete/chunkFilter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class LLMStreamComplete {
208208
isBrace = true;
209209
}
210210
if (inMiddleLine && !isBrace) {
211-
logger.channel()?.trace("stopAtFirstBrace: inMiddleLine");
211+
logger.channel()?.trace("stopAtFirstBrace: inMiddleLine, receive chunk: " + chunkText);
212212
break;
213213
}
214214
}
@@ -303,6 +303,7 @@ export class LLMStreamComplete {
303303
}
304304

305305
if (isAllMatch && firstMatchLine + lines.length >= this.curLineNum) {
306+
logger.channel()?.trace(`All lines are repeated in before 50 lines, remove them.`);
306307
return [];
307308
}
308309
return lines;

0 commit comments

Comments
 (0)