From b96e476c80745c823a2bfdaa77b086ce8079892e Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Thu, 20 Jun 2024 16:30:14 +0800 Subject: [PATCH] fix: Limit code completion context to prevent exceeding limit - Adjusted collapseCodeBlock function to respect complete_context_limit - Implemented prefix and suffix length limiting to prevent exceeding limit - Ensured code completion does not exceed context limit even with folded functions Closes #403 --- src/contributes/codecomplete/promptCreator.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/contributes/codecomplete/promptCreator.ts b/src/contributes/codecomplete/promptCreator.ts index 055b330..00f37fc 100644 --- a/src/contributes/codecomplete/promptCreator.ts +++ b/src/contributes/codecomplete/promptCreator.ts @@ -49,7 +49,25 @@ export async function currentFileContext( } const functionRanges = await findFunctionRanges(filepath, ast.rootNode); - return await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn); + const result: {prefix: string, suffix: string} = await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn); + + const completeContextLimit = DevChatConfig.getInstance().get("complete_context_limit", 3000); + const prefixMaxLength = Math.min(completeContextLimit * 0.7, result.prefix.length); + const suffixMaxLength = completeContextLimit - prefixMaxLength; + + let prefix = result.prefix; + let suffix = result.suffix; + + if (prefix.length + suffix.length > completeContextLimit) { + if (prefix.length > prefixMaxLength) { + prefix = prefix.slice(-prefixMaxLength); + } + if (suffix.length > suffixMaxLength) { + suffix = suffix.slice(0, suffixMaxLength); + } + } + + return { prefix, suffix }; }