From 09b588c0bef771943725c8e7ddad180383391389 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 1 Jul 2025 16:56:43 -0700 Subject: [PATCH 1/7] dynamically switch between rendering ghost text and svg decoration --- .../src/autocomplete/completionProvider.ts | 75 +++++++++++++++++-- 1 file changed, 68 insertions(+), 7 deletions(-) diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index 1f154c8b4..24bd86fa0 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -15,6 +15,12 @@ import { handleLLMError } from "../util/errorHandling"; import { VsCodeIde } from "../VsCodeIde"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; +import { myersDiff } from "core/diff/myers"; +import { + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, + NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, +} from "core/nextEdit/constants"; +import { checkFim } from "core/nextEdit/diff/diff"; import { NextEditProvider } from "core/nextEdit/NextEditProvider"; import { NextEditWindowManager } from "../activation/NextEditWindowManager"; import { getDefinitionsFromLsp } from "./lsp"; @@ -302,20 +308,14 @@ export class ContinueCompletionProvider new vscode.Position(startPos.line, result.range.end), ); } - // console.log("range", range.start, range.end); } else { // Extend the range to the end of the line for multiline completions range = new vscode.Range(startPos, document.lineAt(startPos).range.end); - // console.log("range", range.start, range.end); } const completionItem = new vscode.InlineCompletionItem( completionText, range, - // new vscode.Range( - // new vscode.Position(36, 3), - // new vscode.Position(36, 3), - // ), { title: "Log Autocomplete Outcome", command: "continue.logAutocompleteOutcome", @@ -327,10 +327,71 @@ export class ContinueCompletionProvider if (this.isNextEditActive) { const editor = vscode.window.activeTextEditor; - if (editor && NextEditWindowManager.isInstantiated()) { + if (!editor) { + return undefined; + } + + // Check the diff between old and new editable region. + const newEditRangeSlice = completionText; + + // We don't need to show the next edit window if the predicted edits is empty. + if (newEditRangeSlice === "") { + return undefined; + } + + // Get the contents of the old (current) editable region. + const currCursorPos = editor.selection.active; + const editableRegionStartLine = Math.max( + currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, + 0, + ); + const editableRegionEndLine = Math.min( + currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, + editor.document.lineCount - 1, + ); + const oldEditRangeSlice = editor.document + .getText() + .split("\n") + .slice(editableRegionStartLine, editableRegionEndLine + 1) + .join("\n"); + + // We don't need to show the next edit window if the predicted edits are identical to the previous version. + if (oldEditRangeSlice === newEditRangeSlice) { + return undefined; + } + + // If the diff is a FIM, render a ghost text. + const { isFim, fimText } = checkFim( + oldEditRangeSlice, + newEditRangeSlice, + currCursorPos, + ); + if (isFim) { + const completionItem = new vscode.InlineCompletionItem( + fimText, + new vscode.Range( + new vscode.Position(currCursorPos.line, currCursorPos.character), + new vscode.Position(currCursorPos.line, currCursorPos.character), + ), + { + title: "Log Autocomplete Outcome", + command: "continue.logAutocompleteOutcome", + arguments: [input.completionId, this.completionProvider], + }, + ); + return [completionItem]; + } + + // Else, render a next edit window. + const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); + + if (NextEditWindowManager.isInstantiated()) { await NextEditWindowManager.getInstance().showNextEditWindow( editor, + currCursorPos, + editableRegionStartLine, completionText, + diffLines, ); } From f21963bec2b2319a4d0a9bc07379b1330208aefc Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 1 Jul 2025 16:57:17 -0700 Subject: [PATCH 2/7] remove diff calculation logic --- .../src/activation/NextEditWindowManager.ts | 29 +++---------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/extensions/vscode/src/activation/NextEditWindowManager.ts b/extensions/vscode/src/activation/NextEditWindowManager.ts index ece62384a..25cd64acc 100644 --- a/extensions/vscode/src/activation/NextEditWindowManager.ts +++ b/extensions/vscode/src/activation/NextEditWindowManager.ts @@ -3,8 +3,8 @@ import { EXTENSION_NAME } from "core/control-plane/env"; // @ts-ignore import * as vscode from "vscode"; +import { DiffLine } from "core"; import { CodeRenderer } from "core/codeRenderer/CodeRenderer"; -import { myersDiff } from "core/diff/myers"; import { NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, @@ -225,7 +225,10 @@ export class NextEditWindowManager { */ public async showNextEditWindow( editor: vscode.TextEditor, + currCursorPos: vscode.Position, + editableRegionStartLine: number, newEditRangeSlice: string, + diffLines: DiffLine[], ) { if (!newEditRangeSlice || !this.shouldRenderTip(editor.document.uri)) { return; @@ -237,30 +240,6 @@ export class NextEditWindowManager { // Store the current tooltip text for accepting later. this.currentTooltipText = newEditRangeSlice; - // Get the contents of the old (current) editable region. - const currCursorPos = editor.selection.active; - const editableRegionStartLine = Math.max( - currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, - 0, - ); - const editableRegionEndLine = Math.min( - currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, - editor.document.lineCount - 1, - ); - const oldEditRangeSlice = editor.document - .getText() - .split("\n") - .slice(editableRegionStartLine, editableRegionEndLine + 1) - .join("\n"); - - // We don't need to show the next edit window under these conditions: - // - There are no predicted edits. - // - The predicted edits are identical to the previous version. - if (newEditRangeSlice === "" || oldEditRangeSlice === newEditRangeSlice) - return; - - const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); - // How far away is the current line from the start of the editable region? const lineOffsetAtCursorPos = currCursorPos.line - editableRegionStartLine; From 490ec4813a945a667609370c20581d3421e0ddd0 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 1 Jul 2025 16:57:33 -0700 Subject: [PATCH 3/7] create a helper function to check if a diff is FIM --- core/nextEdit/diff/diff.ts | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/core/nextEdit/diff/diff.ts b/core/nextEdit/diff/diff.ts index c8f971651..9eaf98b13 100644 --- a/core/nextEdit/diff/diff.ts +++ b/core/nextEdit/diff/diff.ts @@ -112,3 +112,76 @@ export function getRenderableDiffWithGutterAnnotations( }, }; } + +/** + * Check if the diff is indeed a FIM. + * @param oldEditRange Original string content. + * @param newEditRange New string content. + * @param cursorPosition The position of the cursor in the old string. + * @returns boolean indicating if the change is purely additive (FIM) + * @returns string of FIM text content. + */ +export function checkFim( + oldEditRange: string, + newEditRange: string, + cursorPosition: { line: number; character: number }, +): { + isFim: boolean; + fimText: string; +} { + // Find the common prefix. + let prefixLength = 0; + while ( + prefixLength < oldEditRange.length && + prefixLength < newEditRange.length && + oldEditRange[prefixLength] === newEditRange[prefixLength] + ) { + prefixLength++; + } + + // Find the common suffix + let oldSuffixPos = oldEditRange.length - 1; + let newSuffixPos = newEditRange.length - 1; + + while ( + oldSuffixPos >= prefixLength && + newSuffixPos >= prefixLength && + oldEditRange[oldSuffixPos] === newEditRange[newSuffixPos] + ) { + oldSuffixPos--; + newSuffixPos--; + } + + // The old text is purely preserved if: + // 1. The prefix ends before or at the cursor. + // 2. The suffix starts after or at the cursor. + // 3. There's no gap between prefix and suffix in the old text. + + const suffixStartInOld = oldSuffixPos + 1; + const suffixStartInNew = newSuffixPos + 1; + + // Convert cursor position to an offset in the string. + // For simplicity, we need to calculate the cursor's position in the string. + // This requires knowledge of line endings in the oldEditRange. + const lines = oldEditRange.substring(0, prefixLength).split("\n"); + const cursorOffset = + lines.length > 1 + ? lines.slice(0, -1).reduce((sum, line) => sum + line.length + 1, 0) + + cursorPosition.character + : cursorPosition.character; + + // Check if the cursor is positioned between the prefix and suffix. + const cursorBetweenPrefixAndSuffix = + prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld; + + // Check if the old text is completely preserved (no deletion). + const noTextDeleted = suffixStartInOld - prefixLength <= 0; + + // Extract the content between prefix and suffix in the new string. + const fimText = newEditRange.substring(prefixLength, suffixStartInNew); + + return { + isFim: cursorBetweenPrefixAndSuffix && noTextDeleted, + fimText, + }; +} From 857836af541c6f24dce1f598bb836f34b6d2a4bd Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Wed, 2 Jul 2025 15:33:55 -0700 Subject: [PATCH 4/7] add test --- core/nextEdit/diff/diff.vitest.ts | 92 +++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 core/nextEdit/diff/diff.vitest.ts diff --git a/core/nextEdit/diff/diff.vitest.ts b/core/nextEdit/diff/diff.vitest.ts new file mode 100644 index 000000000..fa161d49c --- /dev/null +++ b/core/nextEdit/diff/diff.vitest.ts @@ -0,0 +1,92 @@ +import { describe, expect, it } from "vitest"; +import { checkFim } from "./diff"; + +describe("checkFim", () => { + const testCases = [ + { + name: "simple insertion at cursor", + oldEditRange: ["function test() {", " ", "}"].join("\n"), + newEditRange: ["function test() {", " console.log('hello');", "}"].join( + "\n", + ), + cursorPosition: { line: 1, character: 2 }, + expected: { + isFim: true, + fimText: "console.log('hello');", + }, + }, + { + name: "not a FIM because insertion starts before { and new suffix is different from {}", + oldEditRange: ["function test() {}"].join("\n"), + newEditRange: ["function test() {console.log('test');}"].join("\n"), + cursorPosition: { line: 0, character: 16 }, + expected: { + isFim: false, + fimText: null, + }, + }, + { + name: "insertion with cursor at beginning", + oldEditRange: ["function test() {}"].join("\n"), + newEditRange: ["function test() {console.log('test');}"].join("\n"), + cursorPosition: { line: 0, character: 17 }, + expected: { + isFim: true, + fimText: "console.log('test');", + }, + }, + { + name: "not a FIM because text was deleted", + oldEditRange: ["function test() {", " return 42;", "}"].join("\n"), + newEditRange: ["function test() {", " console.log('hello');", "}"].join( + "\n", + ), + cursorPosition: { line: 1, character: 2 }, + expected: { + isFim: false, + fimText: null, + }, + }, + { + name: "not a FIM because text was changed before cursor", + oldEditRange: ["let x = 1;"].join("\n"), + newEditRange: ["const x = 1; // initialized x"].join("\n"), + cursorPosition: { line: 0, character: 8 }, + expected: { + isFim: false, + fimText: null, + }, + }, + { + name: "multi-line insertion at cursor", + oldEditRange: [ + "function process() {", + " ", + " return result;", + "}", + ].join("\n"), + newEditRange: [ + "function process() {", + " const data = fetchData();", + " const result = processData(data);", + " return result;", + "}", + ].join("\n"), + cursorPosition: { line: 1, character: 2 }, + expected: { + isFim: true, + fimText: + "const data = fetchData();\n const result = processData(data);", + }, + }, + ]; + + testCases.forEach( + ({ name, oldEditRange, newEditRange, cursorPosition, expected }) => { + it(name, () => { + const result = checkFim(oldEditRange, newEditRange, cursorPosition); + expect(result).toEqual(expected); + }); + }, + ); +}); From df27a3c821d25e2033bc749941ace1cfa09840ed Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Wed, 2 Jul 2025 15:36:02 -0700 Subject: [PATCH 5/7] check if model name is included in set values --- core/nextEdit/templating/NextEditPromptEngine.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index cee143168..d69730606 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -49,9 +49,18 @@ const NEXT_EDIT_MODEL_TEMPLATES: Record = { function templateRendererOfModel( modelName: NextEditModelName, ): TemplateRenderer { - const template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; + let template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; if (!template) { - throw new Error(`Model ${modelName} is not supported for next edit.`); + // Check if modelName includes any known model name as substring. + const matchingModel = Object.keys(NEXT_EDIT_MODEL_TEMPLATES).find((key) => + modelName.includes(key), + ); + + if (matchingModel) { + template = NEXT_EDIT_MODEL_TEMPLATES[matchingModel as NextEditModelName]; + } else { + throw new Error(`Model ${modelName} is not supported for next edit.`); + } } const compiledTemplate = Handlebars.compile(template.template); From ab96f9529baf771c729ec41ffbdad72aac08c352 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Wed, 2 Jul 2025 15:37:59 -0700 Subject: [PATCH 6/7] add a discriminate union --- core/nextEdit/diff/diff.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/core/nextEdit/diff/diff.ts b/core/nextEdit/diff/diff.ts index 9eaf98b13..bca4a6d08 100644 --- a/core/nextEdit/diff/diff.ts +++ b/core/nextEdit/diff/diff.ts @@ -125,10 +125,15 @@ export function checkFim( oldEditRange: string, newEditRange: string, cursorPosition: { line: number; character: number }, -): { - isFim: boolean; - fimText: string; -} { +): + | { + isFim: true; + fimText: string; + } + | { + isFim: false; + fimText: null; + } { // Find the common prefix. let prefixLength = 0; while ( @@ -177,11 +182,13 @@ export function checkFim( // Check if the old text is completely preserved (no deletion). const noTextDeleted = suffixStartInOld - prefixLength <= 0; - // Extract the content between prefix and suffix in the new string. - const fimText = newEditRange.substring(prefixLength, suffixStartInNew); + const isFim = cursorBetweenPrefixAndSuffix && noTextDeleted; - return { - isFim: cursorBetweenPrefixAndSuffix && noTextDeleted, - fimText, - }; + if (isFim) { + // Extract the content between prefix and suffix in the new string. + const fimText = newEditRange.substring(prefixLength, suffixStartInNew); + return { isFim, fimText }; + } else { + return { isFim, fimText: null }; + } } From 023e8ae9fc738cdad42e23686349de5554fe2b33 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Wed, 2 Jul 2025 17:53:58 -0700 Subject: [PATCH 7/7] move check to an outer layer --- .../templating/NextEditPromptEngine.ts | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index d69730606..7b21232d6 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -51,16 +51,7 @@ function templateRendererOfModel( ): TemplateRenderer { let template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; if (!template) { - // Check if modelName includes any known model name as substring. - const matchingModel = Object.keys(NEXT_EDIT_MODEL_TEMPLATES).find((key) => - modelName.includes(key), - ); - - if (matchingModel) { - template = NEXT_EDIT_MODEL_TEMPLATES[matchingModel as NextEditModelName]; - } else { - throw new Error(`Model ${modelName} is not supported for next edit.`); - } + throw new Error(`Model ${modelName} is not supported for next edit.`); } const compiledTemplate = Handlebars.compile(template.template); @@ -74,7 +65,7 @@ export function renderPrompt( helper: HelperVars, userEdits: string, ): UserPrompt { - const modelName = helper.modelName as NextEditModelName; + let modelName = helper.modelName as NextEditModelName; if (modelName === "this field is not used") { return { @@ -85,7 +76,18 @@ export function renderPrompt( // Validate that the modelName is actually a supported model. if (!Object.keys(NEXT_EDIT_MODEL_TEMPLATES).includes(modelName)) { - throw new Error(`${helper.modelName} is not yet supported for next edit.`); + // Check if modelName includes any known model name as substring. + const matchingModel = Object.keys(NEXT_EDIT_MODEL_TEMPLATES).find((key) => + modelName.includes(key), + ); + + if (matchingModel) { + modelName = matchingModel as NextEditModelName; + } else { + throw new Error( + `${helper.modelName} is not yet supported for next edit.`, + ); + } } const renderer = templateRendererOfModel(modelName);