Changesets · Branches · Tags · Compare
| Changeset | 6a7a94404ac91937f467cadce1c9435e7cb11b36e1220a76d090f85508dbd2fa |
|---|---|
| Author | Nate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-07-02 18:08 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-02 18:08 -07:00 |
| Parents | 14901dcb199b, 80e64a13e0b2 |
5 files changed
modified core/nextEdit/diff/diff.ts +80 −0
| @@ -112,3 +112,83 @@ | ||
| 112 | 112 | }, |
| 113 | 113 | }; |
| 114 | 114 | } |
| 115 | + | |
| 116 | +/** | |
| 117 | + * Check if the diff is indeed a FIM. | |
| 118 | + * @param oldEditRange Original string content. | |
| 119 | + * @param newEditRange New string content. | |
| 120 | + * @param cursorPosition The position of the cursor in the old string. | |
| 121 | + * @returns boolean indicating if the change is purely additive (FIM) | |
| 122 | + * @returns string of FIM text content. | |
| 123 | + */ | |
| 124 | +export function checkFim( | |
| 125 | + oldEditRange: string, | |
| 126 | + newEditRange: string, | |
| 127 | + cursorPosition: { line: number; character: number }, | |
| 128 | +): | |
| 129 | + | { | |
| 130 | + isFim: true; | |
| 131 | + fimText: string; | |
| 132 | + } | |
| 133 | + | { | |
| 134 | + isFim: false; | |
| 135 | + fimText: null; | |
| 136 | + } { | |
| 137 | + // Find the common prefix. | |
| 138 | + let prefixLength = 0; | |
| 139 | + while ( | |
| 140 | + prefixLength < oldEditRange.length && | |
| 141 | + prefixLength < newEditRange.length && | |
| 142 | + oldEditRange[prefixLength] === newEditRange[prefixLength] | |
| 143 | + ) { | |
| 144 | + prefixLength++; | |
| 145 | + } | |
| 146 | + | |
| 147 | + // Find the common suffix | |
| 148 | + let oldSuffixPos = oldEditRange.length - 1; | |
| 149 | + let newSuffixPos = newEditRange.length - 1; | |
| 150 | + | |
| 151 | + while ( | |
| 152 | + oldSuffixPos >= prefixLength && | |
| 153 | + newSuffixPos >= prefixLength && | |
| 154 | + oldEditRange[oldSuffixPos] === newEditRange[newSuffixPos] | |
| 155 | + ) { | |
| 156 | + oldSuffixPos--; | |
| 157 | + newSuffixPos--; | |
| 158 | + } | |
| 159 | + | |
| 160 | + // The old text is purely preserved if: | |
| 161 | + // 1. The prefix ends before or at the cursor. | |
| 162 | + // 2. The suffix starts after or at the cursor. | |
| 163 | + // 3. There's no gap between prefix and suffix in the old text. | |
| 164 | + | |
| 165 | + const suffixStartInOld = oldSuffixPos + 1; | |
| 166 | + const suffixStartInNew = newSuffixPos + 1; | |
| 167 | + | |
| 168 | + // Convert cursor position to an offset in the string. | |
| 169 | + // For simplicity, we need to calculate the cursor's position in the string. | |
| 170 | + // This requires knowledge of line endings in the oldEditRange. | |
| 171 | + const lines = oldEditRange.substring(0, prefixLength).split("\n"); | |
| 172 | + const cursorOffset = | |
| 173 | + lines.length > 1 | |
| 174 | + ? lines.slice(0, -1).reduce((sum, line) => sum + line.length + 1, 0) + | |
| 175 | + cursorPosition.character | |
| 176 | + : cursorPosition.character; | |
| 177 | + | |
| 178 | + // Check if the cursor is positioned between the prefix and suffix. | |
| 179 | + const cursorBetweenPrefixAndSuffix = | |
| 180 | + prefixLength <= cursorOffset && cursorOffset <= suffixStartInOld; | |
| 181 | + | |
| 182 | + // Check if the old text is completely preserved (no deletion). | |
| 183 | + const noTextDeleted = suffixStartInOld - prefixLength <= 0; | |
| 184 | + | |
| 185 | + const isFim = cursorBetweenPrefixAndSuffix && noTextDeleted; | |
| 186 | + | |
| 187 | + if (isFim) { | |
| 188 | + // Extract the content between prefix and suffix in the new string. | |
| 189 | + const fimText = newEditRange.substring(prefixLength, suffixStartInNew); | |
| 190 | + return { isFim, fimText }; | |
| 191 | + } else { | |
| 192 | + return { isFim, fimText: null }; | |
| 193 | + } | |
| 194 | +} |
added core/nextEdit/diff/diff.vitest.ts +92 −0
| @@ -0,0 +1,92 @@ | ||
| 1 | +import { describe, expect, it } from "vitest"; | |
| 2 | +import { checkFim } from "./diff"; | |
| 3 | + | |
| 4 | +describe("checkFim", () => { | |
| 5 | + const testCases = [ | |
| 6 | + { | |
| 7 | + name: "simple insertion at cursor", | |
| 8 | + oldEditRange: ["function test() {", " ", "}"].join("\n"), | |
| 9 | + newEditRange: ["function test() {", " console.log('hello');", "}"].join( | |
| 10 | + "\n", | |
| 11 | + ), | |
| 12 | + cursorPosition: { line: 1, character: 2 }, | |
| 13 | + expected: { | |
| 14 | + isFim: true, | |
| 15 | + fimText: "console.log('hello');", | |
| 16 | + }, | |
| 17 | + }, | |
| 18 | + { | |
| 19 | + name: "not a FIM because insertion starts before { and new suffix is different from {}", | |
| 20 | + oldEditRange: ["function test() {}"].join("\n"), | |
| 21 | + newEditRange: ["function test() {console.log('test');}"].join("\n"), | |
| 22 | + cursorPosition: { line: 0, character: 16 }, | |
| 23 | + expected: { | |
| 24 | + isFim: false, | |
| 25 | + fimText: null, | |
| 26 | + }, | |
| 27 | + }, | |
| 28 | + { | |
| 29 | + name: "insertion with cursor at beginning", | |
| 30 | + oldEditRange: ["function test() {}"].join("\n"), | |
| 31 | + newEditRange: ["function test() {console.log('test');}"].join("\n"), | |
| 32 | + cursorPosition: { line: 0, character: 17 }, | |
| 33 | + expected: { | |
| 34 | + isFim: true, | |
| 35 | + fimText: "console.log('test');", | |
| 36 | + }, | |
| 37 | + }, | |
| 38 | + { | |
| 39 | + name: "not a FIM because text was deleted", | |
| 40 | + oldEditRange: ["function test() {", " return 42;", "}"].join("\n"), | |
| 41 | + newEditRange: ["function test() {", " console.log('hello');", "}"].join( | |
| 42 | + "\n", | |
| 43 | + ), | |
| 44 | + cursorPosition: { line: 1, character: 2 }, | |
| 45 | + expected: { | |
| 46 | + isFim: false, | |
| 47 | + fimText: null, | |
| 48 | + }, | |
| 49 | + }, | |
| 50 | + { | |
| 51 | + name: "not a FIM because text was changed before cursor", | |
| 52 | + oldEditRange: ["let x = 1;"].join("\n"), | |
| 53 | + newEditRange: ["const x = 1; // initialized x"].join("\n"), | |
| 54 | + cursorPosition: { line: 0, character: 8 }, | |
| 55 | + expected: { | |
| 56 | + isFim: false, | |
| 57 | + fimText: null, | |
| 58 | + }, | |
| 59 | + }, | |
| 60 | + { | |
| 61 | + name: "multi-line insertion at cursor", | |
| 62 | + oldEditRange: [ | |
| 63 | + "function process() {", | |
| 64 | + " ", | |
| 65 | + " return result;", | |
| 66 | + "}", | |
| 67 | + ].join("\n"), | |
| 68 | + newEditRange: [ | |
| 69 | + "function process() {", | |
| 70 | + " const data = fetchData();", | |
| 71 | + " const result = processData(data);", | |
| 72 | + " return result;", | |
| 73 | + "}", | |
| 74 | + ].join("\n"), | |
| 75 | + cursorPosition: { line: 1, character: 2 }, | |
| 76 | + expected: { | |
| 77 | + isFim: true, | |
| 78 | + fimText: | |
| 79 | + "const data = fetchData();\n const result = processData(data);", | |
| 80 | + }, | |
| 81 | + }, | |
| 82 | + ]; | |
| 83 | + | |
| 84 | + testCases.forEach( | |
| 85 | + ({ name, oldEditRange, newEditRange, cursorPosition, expected }) => { | |
| 86 | + it(name, () => { | |
| 87 | + const result = checkFim(oldEditRange, newEditRange, cursorPosition); | |
| 88 | + expect(result).toEqual(expected); | |
| 89 | + }); | |
| 90 | + }, | |
| 91 | + ); | |
| 92 | +}); |
modified core/nextEdit/templating/NextEditPromptEngine.ts +14 −3
| @@ -49,7 +49,7 @@ | ||
| 49 | 49 | function templateRendererOfModel( |
| 50 | 50 | modelName: NextEditModelName, |
| 51 | 51 | ): TemplateRenderer { |
| 52 | - const template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; | |
| 52 | + let template = NEXT_EDIT_MODEL_TEMPLATES[modelName]; | |
| 53 | 53 | if (!template) { |
| 54 | 54 | throw new Error(`Model ${modelName} is not supported for next edit.`); |
| 55 | 55 | } |
| @@ -65,7 +65,7 @@ | ||
| 65 | 65 | helper: HelperVars, |
| 66 | 66 | userEdits: string, |
| 67 | 67 | ): UserPrompt { |
| 68 | - const modelName = helper.modelName as NextEditModelName; | |
| 68 | + let modelName = helper.modelName as NextEditModelName; | |
| 69 | 69 | |
| 70 | 70 | if (modelName === "this field is not used") { |
| 71 | 71 | return { |
| @@ -76,7 +76,18 @@ | ||
| 76 | 76 | |
| 77 | 77 | // Validate that the modelName is actually a supported model. |
| 78 | 78 | if (!Object.keys(NEXT_EDIT_MODEL_TEMPLATES).includes(modelName)) { |
| 79 | - throw new Error(`${helper.modelName} is not yet supported for next edit.`); | |
| 79 | + // Check if modelName includes any known model name as substring. | |
| 80 | + const matchingModel = Object.keys(NEXT_EDIT_MODEL_TEMPLATES).find((key) => | |
| 81 | + modelName.includes(key), | |
| 82 | + ); | |
| 83 | + | |
| 84 | + if (matchingModel) { | |
| 85 | + modelName = matchingModel as NextEditModelName; | |
| 86 | + } else { | |
| 87 | + throw new Error( | |
| 88 | + `${helper.modelName} is not yet supported for next edit.`, | |
| 89 | + ); | |
| 90 | + } | |
| 80 | 91 | } |
| 81 | 92 | |
| 82 | 93 | const renderer = templateRendererOfModel(modelName); |
modified extensions/vscode/src/activation/NextEditWindowManager.ts +4 −25
| @@ -3,8 +3,8 @@ | ||
| 3 | 3 | // @ts-ignore |
| 4 | 4 | import * as vscode from "vscode"; |
| 5 | 5 | |
| 6 | +import { DiffLine } from "core"; | |
| 6 | 7 | import { CodeRenderer } from "core/codeRenderer/CodeRenderer"; |
| 7 | -import { myersDiff } from "core/diff/myers"; | |
| 8 | 8 | import { |
| 9 | 9 | NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, |
| 10 | 10 | NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, |
| @@ -225,7 +225,10 @@ | ||
| 225 | 225 | */ |
| 226 | 226 | public async showNextEditWindow( |
| 227 | 227 | editor: vscode.TextEditor, |
| 228 | + currCursorPos: vscode.Position, | |
| 229 | + editableRegionStartLine: number, | |
| 228 | 230 | newEditRangeSlice: string, |
| 231 | + diffLines: DiffLine[], | |
| 229 | 232 | ) { |
| 230 | 233 | if (!newEditRangeSlice || !this.shouldRenderTip(editor.document.uri)) { |
| 231 | 234 | return; |
| @@ -237,30 +240,6 @@ | ||
| 237 | 240 | // Store the current tooltip text for accepting later. |
| 238 | 241 | this.currentTooltipText = newEditRangeSlice; |
| 239 | 242 | |
| 240 | - // Get the contents of the old (current) editable region. | |
| 241 | - const currCursorPos = editor.selection.active; | |
| 242 | - const editableRegionStartLine = Math.max( | |
| 243 | - currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, | |
| 244 | - 0, | |
| 245 | - ); | |
| 246 | - const editableRegionEndLine = Math.min( | |
| 247 | - currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, | |
| 248 | - editor.document.lineCount - 1, | |
| 249 | - ); | |
| 250 | - const oldEditRangeSlice = editor.document | |
| 251 | - .getText() | |
| 252 | - .split("\n") | |
| 253 | - .slice(editableRegionStartLine, editableRegionEndLine + 1) | |
| 254 | - .join("\n"); | |
| 255 | - | |
| 256 | - // We don't need to show the next edit window under these conditions: | |
| 257 | - // - There are no predicted edits. | |
| 258 | - // - The predicted edits are identical to the previous version. | |
| 259 | - if (newEditRangeSlice === "" || oldEditRangeSlice === newEditRangeSlice) | |
| 260 | - return; | |
| 261 | - | |
| 262 | - const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); | |
| 263 | - | |
| 264 | 243 | // How far away is the current line from the start of the editable region? |
| 265 | 244 | const lineOffsetAtCursorPos = currCursorPos.line - editableRegionStartLine; |
| 266 | 245 |
modified extensions/vscode/src/autocomplete/completionProvider.ts +68 −7
| @@ -15,6 +15,12 @@ | ||
| 15 | 15 | import { VsCodeIde } from "../VsCodeIde"; |
| 16 | 16 | import { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 17 | 17 | |
| 18 | +import { myersDiff } from "core/diff/myers"; | |
| 19 | +import { | |
| 20 | + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, | |
| 21 | + NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, | |
| 22 | +} from "core/nextEdit/constants"; | |
| 23 | +import { checkFim } from "core/nextEdit/diff/diff"; | |
| 18 | 24 | import { NextEditProvider } from "core/nextEdit/NextEditProvider"; |
| 19 | 25 | import { NextEditWindowManager } from "../activation/NextEditWindowManager"; |
| 20 | 26 | import { getDefinitionsFromLsp } from "./lsp"; |
| @@ -302,20 +308,14 @@ | ||
| 302 | 308 | new vscode.Position(startPos.line, result.range.end), |
| 303 | 309 | ); |
| 304 | 310 | } |
| 305 | - // console.log("range", range.start, range.end); | |
| 306 | 311 | } else { |
| 307 | 312 | // Extend the range to the end of the line for multiline completions |
| 308 | 313 | range = new vscode.Range(startPos, document.lineAt(startPos).range.end); |
| 309 | - // console.log("range", range.start, range.end); | |
| 310 | 314 | } |
| 311 | 315 | |
| 312 | 316 | const completionItem = new vscode.InlineCompletionItem( |
| 313 | 317 | completionText, |
| 314 | 318 | range, |
| 315 | - // new vscode.Range( | |
| 316 | - // new vscode.Position(36, 3), | |
| 317 | - // new vscode.Position(36, 3), | |
| 318 | - // ), | |
| 319 | 319 | { |
| 320 | 320 | title: "Log Autocomplete Outcome", |
| 321 | 321 | command: "continue.logAutocompleteOutcome", |
| @@ -327,10 +327,71 @@ | ||
| 327 | 327 | |
| 328 | 328 | if (this.isNextEditActive) { |
| 329 | 329 | const editor = vscode.window.activeTextEditor; |
| 330 | - if (editor && NextEditWindowManager.isInstantiated()) { | |
| 330 | + if (!editor) { | |
| 331 | + return undefined; | |
| 332 | + } | |
| 333 | + | |
| 334 | + // Check the diff between old and new editable region. | |
| 335 | + const newEditRangeSlice = completionText; | |
| 336 | + | |
| 337 | + // We don't need to show the next edit window if the predicted edits is empty. | |
| 338 | + if (newEditRangeSlice === "") { | |
| 339 | + return undefined; | |
| 340 | + } | |
| 341 | + | |
| 342 | + // Get the contents of the old (current) editable region. | |
| 343 | + const currCursorPos = editor.selection.active; | |
| 344 | + const editableRegionStartLine = Math.max( | |
| 345 | + currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, | |
| 346 | + 0, | |
| 347 | + ); | |
| 348 | + const editableRegionEndLine = Math.min( | |
| 349 | + currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, | |
| 350 | + editor.document.lineCount - 1, | |
| 351 | + ); | |
| 352 | + const oldEditRangeSlice = editor.document | |
| 353 | + .getText() | |
| 354 | + .split("\n") | |
| 355 | + .slice(editableRegionStartLine, editableRegionEndLine + 1) | |
| 356 | + .join("\n"); | |
| 357 | + | |
| 358 | + // We don't need to show the next edit window if the predicted edits are identical to the previous version. | |
| 359 | + if (oldEditRangeSlice === newEditRangeSlice) { | |
| 360 | + return undefined; | |
| 361 | + } | |
| 362 | + | |
| 363 | + // If the diff is a FIM, render a ghost text. | |
| 364 | + const { isFim, fimText } = checkFim( | |
| 365 | + oldEditRangeSlice, | |
| 366 | + newEditRangeSlice, | |
| 367 | + currCursorPos, | |
| 368 | + ); | |
| 369 | + if (isFim) { | |
| 370 | + const completionItem = new vscode.InlineCompletionItem( | |
| 371 | + fimText, | |
| 372 | + new vscode.Range( | |
| 373 | + new vscode.Position(currCursorPos.line, currCursorPos.character), | |
| 374 | + new vscode.Position(currCursorPos.line, currCursorPos.character), | |
| 375 | + ), | |
| 376 | + { | |
| 377 | + title: "Log Autocomplete Outcome", | |
| 378 | + command: "continue.logAutocompleteOutcome", | |
| 379 | + arguments: [input.completionId, this.completionProvider], | |
| 380 | + }, | |
| 381 | + ); | |
| 382 | + return [completionItem]; | |
| 383 | + } | |
| 384 | + | |
| 385 | + // Else, render a next edit window. | |
| 386 | + const diffLines = myersDiff(oldEditRangeSlice, newEditRangeSlice); | |
| 387 | + | |
| 388 | + if (NextEditWindowManager.isInstantiated()) { | |
| 331 | 389 | await NextEditWindowManager.getInstance().showNextEditWindow( |
| 332 | 390 | editor, |
| 391 | + currCursorPos, | |
| 392 | + editableRegionStartLine, | |
| 333 | 393 | completionText, |
| 394 | + diffLines, | |
| 334 | 395 | ); |
| 335 | 396 | } |
| 336 | 397 |