From dd8a33dafcd90831f5405f7e03d45149c413d874 Mon Sep 17 00:00:00 2001 From: Jonas Helming Date: Thu, 28 Nov 2024 01:16:05 +0100 Subject: [PATCH] Adress review comments Make suffix calculation robust against beeing at line 0 Signed-off-by: Jonas Helming --- .../src/browser/ai-code-completion-preference.ts | 4 ++-- .../src/browser/code-completion-agent.ts | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/ai-code-completion/src/browser/ai-code-completion-preference.ts b/packages/ai-code-completion/src/browser/ai-code-completion-preference.ts index 991b450631b7f..c45f752774272 100644 --- a/packages/ai-code-completion/src/browser/ai-code-completion-preference.ts +++ b/packages/ai-code-completion/src/browser/ai-code-completion-preference.ts @@ -44,8 +44,8 @@ export const AICodeCompletionPreferencesSchema: PreferenceSchema = { [PREF_AI_INLINE_COMPLETION_MAX_CONTEXT_LINES]: { title: 'Maximum Context Lines', type: 'number', - description: 'The maximum number of lines shared between the prefix and suffix for AI code completion.\ - Set this to 0 to use the full file as context without any line limit.', + description: 'The maximum number of lines used as context, distributed among the lines before and after the cursor position (prefix and suffix).\ + Set this to -1 to use the full file as context without any line limit and 0 to only use the current line.', default: 0, minimum: 0 } diff --git a/packages/ai-code-completion/src/browser/code-completion-agent.ts b/packages/ai-code-completion/src/browser/code-completion-agent.ts index 05c5dc5a7695d..abb550cbb3711 100644 --- a/packages/ai-code-completion/src/browser/code-completion-agent.ts +++ b/packages/ai-code-completion/src/browser/code-completion-agent.ts @@ -54,12 +54,17 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent { return undefined; } - const maxContextLines = this.preferences.get(PREF_AI_INLINE_COMPLETION_MAX_CONTEXT_LINES, 0); + const maxContextLines = this.preferences.get(PREF_AI_INLINE_COMPLETION_MAX_CONTEXT_LINES, -1); let prefixStartLine = 1; let suffixEndLine = model.getLineCount(); + // if maxContextLines is -1, use the full file as context without any line limit - if (maxContextLines > 0) { + if (maxContextLines === 0) { + // Only the cursor line + prefixStartLine = position.lineNumber; + suffixEndLine = position.lineNumber; + } else if (maxContextLines > 0) { const linesBeforeCursor = position.lineNumber - 1; const linesAfterCursor = model.getLineCount() - position.lineNumber; @@ -69,7 +74,7 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent { linesBeforeCursor ); const suffixLines = Math.min( - maxContextLines - prefixLines, + Math.floor(maxContextLines / 2), linesAfterCursor );