Merge pull request #6389 from continuedev/feature/nextEdit

Dynamically switch between rendering ghost text and svg decoration based on the model output
This commit is contained in:
Nate Sesti 2025-07-02 18:08:37 -07:00 • committed by GitHub
commit bfdaf84e56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 258 additions and 35 deletions

View file

@ -112,3 +112,83 @@ 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: true;
fimText: string;
}
| {
isFim: false;
fimText: null;
} {
// 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;
const isFim = cursorBetweenPrefixAndSuffix && noTextDeleted;
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 };
}
}

View file

@ -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);
});
},
);
});

View file

@ -49,7 +49,7 @@ const NEXT_EDIT_MODEL_TEMPLATES: Record<NextEditModelName, NextEditTemplate> = {
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.`);
}
@ -65,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 {
@ -76,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);

View file

@ -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;

View file

@ -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,
);
}