Skip to content

Commit

Permalink
Adress review comments
Browse files Browse the repository at this point in the history
Make suffix calculation robust against beeing at line 0

Signed-off-by: Jonas Helming <[email protected]>
  • Loading branch information
JonasHelming committed Nov 28, 2024
1 parent bad7fdd commit dd8a33d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 8 additions & 3 deletions packages/ai-code-completion/src/browser/code-completion-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,17 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent {
return undefined;
}

const maxContextLines = this.preferences.get<number>(PREF_AI_INLINE_COMPLETION_MAX_CONTEXT_LINES, 0);
const maxContextLines = this.preferences.get<number>(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;

Expand All @@ -69,7 +74,7 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent {
linesBeforeCursor
);
const suffixLines = Math.min(
maxContextLines - prefixLines,
Math.floor(maxContextLines / 2),
linesAfterCursor
);

Expand Down

0 comments on commit dd8a33d

Please sign in to comment.