Skip to content

Commit 01a1c5c

Browse files
authored
Merge pull request #570 from devchat-ai/bug/code-completion-exceeds-limit-#403
Fix: Limit code completion context to prevent exceeding limit
2 parents 2390e45 + b96e476 commit 01a1c5c

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/contributes/codecomplete/promptCreator.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,25 @@ export async function currentFileContext(
4949
}
5050

5151
const functionRanges = await findFunctionRanges(filepath, ast.rootNode);
52-
return await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn);
52+
const result: {prefix: string, suffix: string} = await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn);
53+
54+
const completeContextLimit = DevChatConfig.getInstance().get("complete_context_limit", 3000);
55+
const prefixMaxLength = Math.min(completeContextLimit * 0.7, result.prefix.length);
56+
const suffixMaxLength = completeContextLimit - prefixMaxLength;
57+
58+
let prefix = result.prefix;
59+
let suffix = result.suffix;
60+
61+
if (prefix.length + suffix.length > completeContextLimit) {
62+
if (prefix.length > prefixMaxLength) {
63+
prefix = prefix.slice(-prefixMaxLength);
64+
}
65+
if (suffix.length > suffixMaxLength) {
66+
suffix = suffix.slice(0, suffixMaxLength);
67+
}
68+
}
69+
70+
return { prefix, suffix };
5371
}
5472

5573

0 commit comments

Comments
 (0)