From 09b588c0bef771943725c8e7ddad180383391389 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 1 Jul 2025 16:56:43 -0700 Subject: [PATCH 1/3] 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/3] 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/3] 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, + }; +}