Merge pull request #550 from devchat-ai/fix_code_completion_function_range_error

Refactor findFunctionRanges to Improve Performance and Readability
This commit is contained in:
boob.yang 2024-06-06 20:56:52 +08:00 committed by GitHub
commit 8d8cb95d69
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 11 deletions

View File

@ -46,17 +46,18 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
}
const matches = query?.matches(node);
return (
matches?.flatMap((match) => {
const functionRanges: FunctionRange[] = [];
if (matches) {
for (const match of matches) {
// find functionNode through tag name
const functionNode = match.captures.find((capture) => capture.name === "function")?.node;
const bodyNode = match.captures.find((capture) => capture.name === "function.body")?.node;
const nameNode = match.captures.find((capture) => capture.name === "function.name")?.node;
if (!functionNode ||!bodyNode) {
return [];
if (!functionNode || !bodyNode) {
continue;
}
const results = {
const functionRange: FunctionRange = {
define: {
start: {
row: functionNode.startPosition.row,
@ -77,11 +78,26 @@ export async function findFunctionRanges(filepath: string, node: Parser.SyntaxNo
column: bodyNode.endPosition.column,
},
},
name: nameNode?.text?? "",
name: nameNode?.text ?? "",
};
return results;
}) ?? []
);
// Check if this function range is not fully contained within another function range
const isContained = functionRanges.some(range => {
return (
range.define.start.row <= functionRange.define.start.row &&
range.define.end.row >= functionRange.define.end.row &&
range.body.start.row <= functionRange.body.start.row &&
range.body.end.row >= functionRange.body.end.row
);
});
if (!isContained) {
functionRanges.push(functionRange);
}
}
}
return functionRanges;
}
export async function findFunctionNodes(filepath: string, node: Parser.SyntaxNode): Promise<Parser.SyntaxNode[]> {

View File

@ -208,7 +208,7 @@ export class LLMStreamComplete {
isBrace = true;
}
if (inMiddleLine && !isBrace) {
logger.channel()?.trace("stopAtFirstBrace: inMiddleLine");
logger.channel()?.trace("stopAtFirstBrace: inMiddleLine, receive chunk: " + chunkText);
break;
}
}
@ -303,6 +303,7 @@ export class LLMStreamComplete {
}
if (isAllMatch && firstMatchLine + lines.length >= this.curLineNum) {
logger.channel()?.trace(`All lines are repeated in before 50 lines, remove them.`);
return [];
}
return lines;