From 3ee6c3b1687857faa732fe8f45f38abd7406dd2a Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Wed, 5 Feb 2025 18:11:38 +0100 Subject: [PATCH 1/4] update completion logic to handle midline insertions more accurately in completionProvider.ts --- .../vscode/src/autocomplete/completionProvider.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index f7ddf82f5..aca4c5478 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -41,8 +41,7 @@ interface VsCodeCompletionInput { } export class ContinueCompletionProvider - implements vscode.InlineCompletionItemProvider -{ + implements vscode.InlineCompletionItemProvider { private onError(e: any) { const options = ["Documentation"]; if (e.message.includes("Ollama may not be installed")) { @@ -280,14 +279,8 @@ export class ContinueCompletionProvider document.lineAt(startPos).range.end, ); } else if ( - //diffPatternMatches(diffs, ["+", "-"]) || - //diffPatternMatches(diffs, ["-", "+"]) - diffs.every((diff, index) => - index % 2 === 0 ? diff.added : diff.removed - ) || - diffs.every((diff, index) => - index % 2 === 0 ? diff.removed : diff.added - ) + diffPatternMatches(diffs, ["+", "-"]) || + diffPatternMatches(diffs, ["-", "+"]) ) { // We are midline and the model just inserted without repeating to the end of the line // We want to move the cursor to the end of the line @@ -310,7 +303,7 @@ export class ContinueCompletionProvider } else { // If the first part of the diff isn't an insertion, then the model is // probably rewriting other parts of the line - return undefined; + // return undefined; - Let's assume it's simply an insertion } } } else { From be2c9cb92d6b4bd8774b141377ef26480b7756bc Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Fri, 7 Feb 2025 22:44:20 +0100 Subject: [PATCH 2/4] Create processSingleLineCompletion pure function with unit tests --- .../util/completionTestUtils.test.ts | 62 ++++++++++++ core/autocomplete/util/completionTestUtils.ts | 63 ++++++++++++ .../util/processSingleLineCompletion.test.ts | 97 +++++++++++++++++++ .../util/processSingleLineCompletion.ts | 92 ++++++++++++++++++ .../src/autocomplete/completionProvider.ts | 83 ++++------------ 5 files changed, 331 insertions(+), 66 deletions(-) create mode 100644 core/autocomplete/util/completionTestUtils.test.ts create mode 100644 core/autocomplete/util/completionTestUtils.ts create mode 100644 core/autocomplete/util/processSingleLineCompletion.test.ts create mode 100644 core/autocomplete/util/processSingleLineCompletion.ts diff --git a/core/autocomplete/util/completionTestUtils.test.ts b/core/autocomplete/util/completionTestUtils.test.ts new file mode 100644 index 000000000..7ef76eba0 --- /dev/null +++ b/core/autocomplete/util/completionTestUtils.test.ts @@ -0,0 +1,62 @@ +import { processTestCase, type CompletionTestCase } from './completionTestUtils'; + +describe('processTestCase utility', () => { + it('processes simple console.log completion', () => { + const testCase: CompletionTestCase = { + original: 'console.log(|cur||till|)', + completion: '"foo,", bar', + }; + + expect(processTestCase(testCase)).toEqual({ + input: { + lastLineOfCompletionText: '"foo,", bar', + currentText: ')', + cursorPosition: 'console.log('.length, + }, + expectedResult: { + completionText: '"foo,", bar', + }, + }); + }); + + it('processes simple console.log completion with overwriting', () => { + const testCase: CompletionTestCase = { + original: 'console.log(|cur|)|till|', + completion: '"foo,", bar);', + }; + + expect(processTestCase(testCase)).toEqual({ + input: { + lastLineOfCompletionText: '"foo,", bar);', + currentText: ')', + cursorPosition: 'console.log('.length, + }, + expectedResult: { + completionText: '"foo,", bar);', + range: { + start: 'console.log('.length, + end: 'console.log()'.length + }, + }, + }); + }); + + it('partially applying completion', () => { + const testCase: CompletionTestCase = { + original: '|cur||till|fetch("https://example.com");', + completion: 'await fetch("https://example.com");', + appliedCompletion: 'await ' + }; + + expect(processTestCase(testCase)).toEqual({ + input: { + lastLineOfCompletionText: 'await fetch("https://example.com");', + currentText: 'fetch("https://example.com");', + cursorPosition: 0, + }, + expectedResult: { + completionText: 'await ' + }, + }); + }); +}); diff --git a/core/autocomplete/util/completionTestUtils.ts b/core/autocomplete/util/completionTestUtils.ts new file mode 100644 index 000000000..4ca10d739 --- /dev/null +++ b/core/autocomplete/util/completionTestUtils.ts @@ -0,0 +1,63 @@ +export interface CompletionTestCase { + original: string; // Text with |cur| and |till| markers + completion: string; // Text to insert/overwrite + appliedCompletion?: string | null; + cursorMarker?: string; + tillMarker?: string; +} + +export interface ProcessedTestCase { + input: { + lastLineOfCompletionText: string; + currentText: string; + cursorPosition: number; + }; + expectedResult: { + completionText: string; + range?: { + start: number; + end: number; + }; + }; +} + +export function processTestCase({ + original, + completion, + appliedCompletion = null, + cursorMarker = '|cur|', + tillMarker = '|till|', +}: CompletionTestCase): ProcessedTestCase { + // Validate cursor marker + if (!original.includes(cursorMarker)) { + throw new Error('Cursor marker not found in original text'); + } + + const cursorPos = original.indexOf(cursorMarker); + original = original.replace(cursorMarker, ''); + + let tillPos = original.indexOf(tillMarker); + if (tillPos < 0) { + tillPos = cursorPos; + } else { + original = original.replace(tillMarker, '') + } + + // Calculate currentText based on what's between cursor and till marker + const currentText = original.substring(cursorPos); + + return { + input: { + lastLineOfCompletionText: completion, + currentText, + cursorPosition: cursorPos, + }, + expectedResult: { + completionText: appliedCompletion || completion, + range: cursorPos === tillPos ? undefined : { + start: cursorPos, + end: tillPos, + }, + }, + }; +} \ No newline at end of file diff --git a/core/autocomplete/util/processSingleLineCompletion.test.ts b/core/autocomplete/util/processSingleLineCompletion.test.ts new file mode 100644 index 000000000..7384ab949 --- /dev/null +++ b/core/autocomplete/util/processSingleLineCompletion.test.ts @@ -0,0 +1,97 @@ +import { processTestCase } from './completionTestUtils'; +import { processSingleLineCompletion } from './processSingleLineCompletion'; + +describe('processSingleLineCompletion', () => { + + it('should handle simple end of line completion', () => { + const testCase = processTestCase({ + original: 'console.log(|cur|', + completion: '"Hello, world!")', + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + + it('should handle midline insert repeating the end of line', () => { + const testCase = processTestCase({ + original: 'console.log(|cur|);|till|', + completion: '"Hello, world!");', + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + + it('should handle midline insert repeating the end of line plus adding a semicolon', () => { + const testCase = processTestCase({ + original: 'console.log(|cur|)|till|', + completion: '"Hello, world!");', + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + + it('should handle simple midline insert', () => { + const testCase = processTestCase({ + original: 'console.log(|cur|)', + completion: '"Hello, world!"', + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + + it('should handle complex dif with addition in the beginning', () => { + const testCase = processTestCase({ + original: 'console.log(|cur||till|, "param1", )', // TODO + completion: '"Hello world!", "param1", param1);', + appliedCompletion: '"Hello world!"' + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + + it('should handle simple insertion even with random equality', () => { + const testCase = processTestCase({ + original: 'print(f"Foobar length: |cur||till|")', + completion: '{len(foobar)}', + }); + + const result = processSingleLineCompletion( + testCase.input.lastLineOfCompletionText, + testCase.input.currentText, + testCase.input.cursorPosition + ); + + expect(result).toEqual(testCase.expectedResult); + }); + +}); diff --git a/core/autocomplete/util/processSingleLineCompletion.ts b/core/autocomplete/util/processSingleLineCompletion.ts new file mode 100644 index 000000000..cc9da95d0 --- /dev/null +++ b/core/autocomplete/util/processSingleLineCompletion.ts @@ -0,0 +1,92 @@ +import * as Diff from "diff" + +interface SingleLineCompletionResult { + completionText: string; + range?: { + start: number; + end: number; + }; +} + + +interface DiffType { + count?: number; + added?: boolean; + removed?: boolean; + value: string; +} + + +function diffPatternMatches( + diffs: DiffType[], + pattern: DiffPartType[], +): boolean { + if (diffs.length !== pattern.length) { + return false; + } + + for (let i = 0; i < diffs.length; i++) { + const diff = diffs[i]; + const diffPartType: DiffPartType = + !diff.added && !diff.removed ? "=" : diff.added ? "+" : "-"; + + if (diffPartType !== pattern[i]) { + return false; + } + } + + return true; +} + +type DiffPartType = "+" | "-" | "="; + +export function processSingleLineCompletion( + lastLineOfCompletionText: string, + currentText: string, + cursorPosition: number +): SingleLineCompletionResult | undefined { + const diffs: DiffType[] = Diff.diffWords(currentText, lastLineOfCompletionText); + + if (diffPatternMatches(diffs, ["+"])) { + // Just insert, we're already at the end of the line + return { + completionText: lastLineOfCompletionText, + }; + } + + if ( + diffPatternMatches(diffs, ["+", "="]) || + diffPatternMatches(diffs, ["+", "=", "+"]) + ) { + // The model repeated the text after the cursor to the end of the line + return { + completionText: lastLineOfCompletionText, + range: { + start: cursorPosition, + end: currentText.length + cursorPosition, + }, + }; + } + + if ( + diffPatternMatches(diffs, ["+", "-"]) || + diffPatternMatches(diffs, ["-", "+"]) + ) { + // We are midline and the model just inserted without repeating to the end of the line + return { + completionText: lastLineOfCompletionText, + }; + } + + // For any other diff pattern, just use the first added part if available + if (diffs[0]?.added) { + return { + completionText: diffs[0].value, + }; + } + + // Default case: treat as simple insertion + return { + completionText: lastLineOfCompletionText, + }; +} diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index aca4c5478..7a1816256 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -24,8 +24,7 @@ import { import type { TabAutocompleteModel } from "../util/loadAutocompleteModel"; import { startLocalOllama } from "core/util/ollamaHelper"; import type { IDE } from "core"; - -const Diff = require("diff"); +import { processSingleLineCompletion } from "core/autocomplete/util/processSingleLineCompletion"; interface DiffType { count: number; @@ -258,54 +257,29 @@ export class ContinueCompletionProvider const isSingleLineCompletion = outcome.completion.split("\n").length <= 1; if (isSingleLineCompletion) { - const lastLineOfCompletionText = completionText.split("\n").pop(); + const lastLineOfCompletionText = completionText.split("\n").pop() || ""; const currentText = document .lineAt(startPos) .text.substring(startPos.character); - const diffs: DiffType[] = Diff.diffWords( - currentText, + + const result = processSingleLineCompletion( lastLineOfCompletionText, + currentText, + startPos.character ); - if (diffPatternMatches(diffs, ["+"])) { - // Just insert, we're already at the end of the line - } else if ( - diffPatternMatches(diffs, ["+", "="]) || - diffPatternMatches(diffs, ["+", "=", "+"]) - ) { - // The model repeated the text after the cursor to the end of the line - range = new vscode.Range( - startPos, - document.lineAt(startPos).range.end, - ); - } else if ( - diffPatternMatches(diffs, ["+", "-"]) || - diffPatternMatches(diffs, ["-", "+"]) - ) { - // We are midline and the model just inserted without repeating to the end of the line - // We want to move the cursor to the end of the line - // range = new vscode.Range( - // startPos, - // document.lineAt(startPos).range.end, - // ); - // // Find the last removed part of the diff - // const lastRemovedIndex = findLastIndex( - // diffs, - // (diff) => diff.removed === true, - // ); - // const lastRemovedContent = diffs[lastRemovedIndex].value; - // completionText += lastRemovedContent; - } else { - // Diff is too complicated, just insert the first added part of the diff - // This is the safe way to ensure that it is displayed - if (diffs[0]?.added) { - completionText = diffs[0].value; - } else { - // If the first part of the diff isn't an insertion, then the model is - // probably rewriting other parts of the line - // return undefined; - Let's assume it's simply an insertion - } + if (result === undefined) { + return undefined; } + + completionText = result.completionText; + if (result.range) { + range = new vscode.Range( + new vscode.Position(startPos.line, result.range.start), + new vscode.Position(startPos.line, result.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); @@ -352,26 +326,3 @@ export class ContinueCompletionProvider return true; } } - -type DiffPartType = "+" | "-" | "="; - -function diffPatternMatches( - diffs: DiffType[], - pattern: DiffPartType[], -): boolean { - if (diffs.length !== pattern.length) { - return false; - } - - for (let i = 0; i < diffs.length; i++) { - const diff = diffs[i]; - const diffPartType: DiffPartType = - !diff.added && !diff.removed ? "=" : diff.added ? "+" : "-"; - - if (diffPartType !== pattern[i]) { - return false; - } - } - - return true; -} From 4cc1cdd68adb9582b90bb596eb7a063f0d4705b9 Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Sat, 8 Feb 2025 00:00:06 +0100 Subject: [PATCH 3/4] fix formatting issues --- .../util/completionTestUtils.test.ts | 53 ++++++++++--------- core/autocomplete/util/completionTestUtils.ts | 10 ++-- .../util/processSingleLineCompletion.test.ts | 44 +++++++-------- .../util/processSingleLineCompletion.ts | 2 +- .../src/autocomplete/completionProvider.ts | 11 +--- 5 files changed, 58 insertions(+), 62 deletions(-) diff --git a/core/autocomplete/util/completionTestUtils.test.ts b/core/autocomplete/util/completionTestUtils.test.ts index 7ef76eba0..bdf4e209a 100644 --- a/core/autocomplete/util/completionTestUtils.test.ts +++ b/core/autocomplete/util/completionTestUtils.test.ts @@ -1,61 +1,64 @@ -import { processTestCase, type CompletionTestCase } from './completionTestUtils'; +import { + processTestCase, + type CompletionTestCase, +} from "./completionTestUtils"; -describe('processTestCase utility', () => { - it('processes simple console.log completion', () => { +describe("processTestCase utility", () => { + it("processes simple console.log completion", () => { const testCase: CompletionTestCase = { - original: 'console.log(|cur||till|)', - completion: '"foo,", bar', + original: "console.log(|cur||till|)", + completion: "\"foo,\", bar", }; expect(processTestCase(testCase)).toEqual({ input: { - lastLineOfCompletionText: '"foo,", bar', - currentText: ')', - cursorPosition: 'console.log('.length, + lastLineOfCompletionText: "\"foo,\", bar", + currentText: ")", + cursorPosition: "console.log(".length, }, expectedResult: { - completionText: '"foo,", bar', + completionText: "\"foo,\", bar", }, }); }); - it('processes simple console.log completion with overwriting', () => { + it("processes simple console.log completion with overwriting", () => { const testCase: CompletionTestCase = { - original: 'console.log(|cur|)|till|', - completion: '"foo,", bar);', + original: "console.log(|cur|)|till|", + completion: "\"foo,\", bar);", }; expect(processTestCase(testCase)).toEqual({ input: { - lastLineOfCompletionText: '"foo,", bar);', - currentText: ')', - cursorPosition: 'console.log('.length, + lastLineOfCompletionText: "\"foo,\", bar);", + currentText: ")", + cursorPosition: "console.log(".length, }, expectedResult: { - completionText: '"foo,", bar);', + completionText: "\"foo,\", bar);", range: { - start: 'console.log('.length, - end: 'console.log()'.length + start: "console.log(".length, + end: "console.log()".length, }, }, }); }); - it('partially applying completion', () => { + it("partially applying completion", () => { const testCase: CompletionTestCase = { - original: '|cur||till|fetch("https://example.com");', - completion: 'await fetch("https://example.com");', - appliedCompletion: 'await ' + original: "|cur||till|fetch(\"https://example.com\");", + completion: "await fetch(\"https://example.com\");", + appliedCompletion: "await ", }; expect(processTestCase(testCase)).toEqual({ input: { - lastLineOfCompletionText: 'await fetch("https://example.com");', - currentText: 'fetch("https://example.com");', + lastLineOfCompletionText: "await fetch(\"https://example.com\");", + currentText: "fetch(\"https://example.com\");", cursorPosition: 0, }, expectedResult: { - completionText: 'await ' + completionText: "await ", }, }); }); diff --git a/core/autocomplete/util/completionTestUtils.ts b/core/autocomplete/util/completionTestUtils.ts index 4ca10d739..414b15627 100644 --- a/core/autocomplete/util/completionTestUtils.ts +++ b/core/autocomplete/util/completionTestUtils.ts @@ -25,22 +25,22 @@ export function processTestCase({ original, completion, appliedCompletion = null, - cursorMarker = '|cur|', - tillMarker = '|till|', + cursorMarker = "|cur|", + tillMarker = "|till|", }: CompletionTestCase): ProcessedTestCase { // Validate cursor marker if (!original.includes(cursorMarker)) { - throw new Error('Cursor marker not found in original text'); + throw new Error("Cursor marker not found in original text"); } const cursorPos = original.indexOf(cursorMarker); - original = original.replace(cursorMarker, ''); + original = original.replace(cursorMarker, ""); let tillPos = original.indexOf(tillMarker); if (tillPos < 0) { tillPos = cursorPos; } else { - original = original.replace(tillMarker, '') + original = original.replace(tillMarker, "") } // Calculate currentText based on what's between cursor and till marker diff --git a/core/autocomplete/util/processSingleLineCompletion.test.ts b/core/autocomplete/util/processSingleLineCompletion.test.ts index 7384ab949..8081ac20c 100644 --- a/core/autocomplete/util/processSingleLineCompletion.test.ts +++ b/core/autocomplete/util/processSingleLineCompletion.test.ts @@ -1,12 +1,12 @@ -import { processTestCase } from './completionTestUtils'; -import { processSingleLineCompletion } from './processSingleLineCompletion'; +import { processTestCase } from "./completionTestUtils"; +import { processSingleLineCompletion } from "./processSingleLineCompletion"; -describe('processSingleLineCompletion', () => { +describe("processSingleLineCompletion", () => { - it('should handle simple end of line completion', () => { + it("should handle simple end of line completion", () => { const testCase = processTestCase({ - original: 'console.log(|cur|', - completion: '"Hello, world!")', + original: "console.log(|cur|", + completion: "\"Hello, world!\")", }); const result = processSingleLineCompletion( @@ -18,10 +18,10 @@ describe('processSingleLineCompletion', () => { expect(result).toEqual(testCase.expectedResult); }); - it('should handle midline insert repeating the end of line', () => { + it("should handle midline insert repeating the end of line", () => { const testCase = processTestCase({ - original: 'console.log(|cur|);|till|', - completion: '"Hello, world!");', + original: "console.log(|cur|);|till|", + completion: "\"Hello, world!\");", }); const result = processSingleLineCompletion( @@ -33,10 +33,10 @@ describe('processSingleLineCompletion', () => { expect(result).toEqual(testCase.expectedResult); }); - it('should handle midline insert repeating the end of line plus adding a semicolon', () => { + it("should handle midline insert repeating the end of line plus adding a semicolon", () => { const testCase = processTestCase({ - original: 'console.log(|cur|)|till|', - completion: '"Hello, world!");', + original: "console.log(|cur|)|till|", + completion: "\"Hello, world!\");", }); const result = processSingleLineCompletion( @@ -48,10 +48,10 @@ describe('processSingleLineCompletion', () => { expect(result).toEqual(testCase.expectedResult); }); - it('should handle simple midline insert', () => { + it("should handle simple midline insert", () => { const testCase = processTestCase({ - original: 'console.log(|cur|)', - completion: '"Hello, world!"', + original: "console.log(|cur|)", + completion: "\"Hello, world!\"", }); const result = processSingleLineCompletion( @@ -63,11 +63,11 @@ describe('processSingleLineCompletion', () => { expect(result).toEqual(testCase.expectedResult); }); - it('should handle complex dif with addition in the beginning', () => { + it("should handle complex dif with addition in the beginning", () => { const testCase = processTestCase({ - original: 'console.log(|cur||till|, "param1", )', // TODO - completion: '"Hello world!", "param1", param1);', - appliedCompletion: '"Hello world!"' + original: "console.log(|cur||till|, \"param1\", )", // TODO + completion: "\"Hello world!\", \"param1\", param1);", + appliedCompletion: "\"Hello world!\"" }); const result = processSingleLineCompletion( @@ -79,10 +79,10 @@ describe('processSingleLineCompletion', () => { expect(result).toEqual(testCase.expectedResult); }); - it('should handle simple insertion even with random equality', () => { + it("should handle simple insertion even with random equality", () => { const testCase = processTestCase({ - original: 'print(f"Foobar length: |cur||till|")', - completion: '{len(foobar)}', + original: "print(f\"Foobar length: |cur||till|\")", + completion: "{len(foobar)}", }); const result = processSingleLineCompletion( diff --git a/core/autocomplete/util/processSingleLineCompletion.ts b/core/autocomplete/util/processSingleLineCompletion.ts index cc9da95d0..3dc093411 100644 --- a/core/autocomplete/util/processSingleLineCompletion.ts +++ b/core/autocomplete/util/processSingleLineCompletion.ts @@ -1,4 +1,4 @@ -import * as Diff from "diff" +import * as Diff from "diff"; interface SingleLineCompletionResult { completionText: string; diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index 7a1816256..ded488fff 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -1,9 +1,11 @@ import { CompletionProvider } from "core/autocomplete/CompletionProvider"; +import { processSingleLineCompletion } from "core/autocomplete/util/processSingleLineCompletion"; import { type AutocompleteInput, type AutocompleteOutcome, } from "core/autocomplete/util/types"; import { ConfigHandler } from "core/config/ConfigHandler"; +import { startLocalOllama } from "core/util/ollamaHelper"; import * as URI from "uri-js"; import { v4 as uuidv4 } from "uuid"; import * as vscode from "vscode"; @@ -22,16 +24,7 @@ import { } from "./statusBar"; import type { TabAutocompleteModel } from "../util/loadAutocompleteModel"; -import { startLocalOllama } from "core/util/ollamaHelper"; import type { IDE } from "core"; -import { processSingleLineCompletion } from "core/autocomplete/util/processSingleLineCompletion"; - -interface DiffType { - count: number; - added: boolean; - removed: boolean; - value: string; -} interface VsCodeCompletionInput { document: vscode.TextDocument; From f013c9e01998113e070b0556f693559b3a66e0ee Mon Sep 17 00:00:00 2001 From: ferenci84 Date: Fri, 28 Feb 2025 11:29:06 +0100 Subject: [PATCH 4/4] Add documentation for processTestCase function in completionTestUtils.ts --- core/autocomplete/util/completionTestUtils.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/core/autocomplete/util/completionTestUtils.ts b/core/autocomplete/util/completionTestUtils.ts index 414b15627..a686d59e8 100644 --- a/core/autocomplete/util/completionTestUtils.ts +++ b/core/autocomplete/util/completionTestUtils.ts @@ -21,6 +21,34 @@ export interface ProcessedTestCase { }; } +/** + * Transforms human-readable test case into input and expected results. + * + * - `original`: Your original text with |cur| marking where the cursor is before completion, + * and |till| marking where the cursor should be after accepting completion (and this is + * the end of the actual applied completion) + * - `completion`: LLM completion output + * - `appliedCompletion` (optional): part of the LLM completion output that is actually applied + * (written between |cur| and |till| in the original) + * + * For example, you have this line: + * + * console.log(""); + * + * and expect it to be completed this way: + * + * console.log("foo: ", bar); + * + * with your completion coming from LLM being: `'foo: ", bar);'` + * + * Your input to this function should be: + * - original: `'console.log("|cur|"|till|);'` + * - completion: `'foo: ", bar);'` + * - appliedCompletion: `'foo: ", bar'` + * + * Output: input and expected output of {@link core/autocomplete/util/processSingleLineCompletion/processSingleLineCompletion|processSingleLineCompletion()} + * + */ export function processTestCase({ original, completion,