diff --git a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts index 180c339e6..1850729f1 100644 --- a/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts +++ b/core/autocomplete/filtering/streamTransforms/StreamTransformPipeline.ts @@ -16,6 +16,8 @@ import { streamWithNewLines, } from "./lineStream"; +const STOP_AT_PATTERNS = ["diff --git"]; + export class StreamTransformPipeline { async *transform( generator: AsyncGenerator, @@ -28,7 +30,10 @@ export class StreamTransformPipeline { ): AsyncGenerator { let charGenerator = generator; - charGenerator = stopAtStopTokens(generator, stopTokens); + charGenerator = stopAtStopTokens(generator, [ + ...stopTokens, + ...STOP_AT_PATTERNS, + ]); charGenerator = stopAtStartOf(charGenerator, suffix); for (const charFilter of helper.lang.charFilters ?? []) { charGenerator = charFilter({ diff --git a/core/autocomplete/filtering/test/util.ts b/core/autocomplete/filtering/test/util.ts index db8d99131..d1b94575e 100644 --- a/core/autocomplete/filtering/test/util.ts +++ b/core/autocomplete/filtering/test/util.ts @@ -54,6 +54,7 @@ export async function testAutocompleteFiltering( const line = prefix.split("\n").length - 1; const character = prefix.split("\n")[line].length; const autocompleteInput: AutocompleteInput = { + isUntitledFile: false, completionId: "test-completion-id", filepath, pos: { diff --git a/core/autocomplete/prefiltering/index.ts b/core/autocomplete/prefiltering/index.ts index 40ffa5384..a9c198a51 100644 --- a/core/autocomplete/prefiltering/index.ts +++ b/core/autocomplete/prefiltering/index.ts @@ -70,6 +70,14 @@ export async function shouldPrefilter( return true; } + // Don't offer completions when we have no information (untitled file and no file contents) + if ( + helper.filepath.includes("Untitled") && + helper.fileContents.trim() === "" + ) { + return true; + } + // if ( // helper.options.transform && // (await shouldLanguageSpecificPrefilter(helper)) diff --git a/core/autocomplete/templating/index.ts b/core/autocomplete/templating/index.ts index 8aaa98248..5715e300b 100644 --- a/core/autocomplete/templating/index.ts +++ b/core/autocomplete/templating/index.ts @@ -5,14 +5,14 @@ import { getBasename } from "../../util"; import { AutocompleteLanguageInfo } from "../constants/AutocompleteLanguageInfo"; import { HelperVars } from "../util/HelperVars"; +import { SnippetPayload } from "../snippets"; import { AutocompleteTemplate, getTemplateForModel, } from "./AutocompleteTemplate"; -import { getStopTokens } from "./getStopTokens"; -import { SnippetPayload } from "../snippets"; -import { formatSnippets } from "./formatting"; import { getSnippets } from "./filtering"; +import { formatSnippets } from "./formatting"; +import { getStopTokens } from "./getStopTokens"; function getTemplate(helper: HelperVars): AutocompleteTemplate { if (helper.options.template) { @@ -62,6 +62,9 @@ export function renderPrompt({ // If prefix is manually passed let prefix = helper.input.manuallyPassPrefix || helper.prunedPrefix; let suffix = helper.input.manuallyPassPrefix ? "" : helper.prunedSuffix; + if (suffix === "") { + suffix = "\n"; + } const reponame = getBasename(workspaceDirs[0] ?? "myproject"); diff --git a/core/autocomplete/util/types.ts b/core/autocomplete/util/types.ts index d54d57bd2..c11d8c2d9 100644 --- a/core/autocomplete/util/types.ts +++ b/core/autocomplete/util/types.ts @@ -13,6 +13,7 @@ export type RecentlyEditedRange = RangeInFile & { }; export interface AutocompleteInput { + isUntitledFile: boolean; completionId: string; filepath: string; pos: Position; diff --git a/core/util/findLast.ts b/core/util/findLast.ts new file mode 100644 index 000000000..fb39829e7 --- /dev/null +++ b/core/util/findLast.ts @@ -0,0 +1,14 @@ +export function findLastIndex( + arr: any[], + criterion: (item: any) => boolean, +): number { + let lastIndex = -1; + + for (let i = arr.length - 1; i >= 0; i--) { + if (criterion(arr[i])) { + lastIndex = i; + break; + } + } + return lastIndex; +} diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 04349b1f1..3e213c999 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -1,12 +1,12 @@ { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "continue", - "version": "0.9.243", + "version": "0.9.244", "license": "Apache-2.0", "dependencies": { "@continuedev/fetch": "^1.0.3", @@ -20,6 +20,7 @@ "core": "file:../../core", "cors": "^2.8.5", "dbinfoz": "^0.14.0", + "diff": "^7.0.0", "downshift": "^7.6.0", "esbuild": "^0.17.19", "express": "^4.18.2", @@ -4981,9 +4982,9 @@ "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==" }, "node_modules/diff": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", - "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-7.0.0.tgz", + "integrity": "sha512-PJWHUb1RFevKCwaFA9RlG5tCd+FO5iRh9A8HEtkmBH2Li03iJriB6m6JIN4rGz3K3JLawI7/veA1xzRKP6ISBw==", "engines": { "node": ">=0.3.1" } @@ -8858,6 +8859,15 @@ "wrap-ansi": "^7.0.0" } }, + "node_modules/mocha/node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/mocha/node_modules/minimatch": { "version": "5.1.6", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-5.1.6.tgz", @@ -13165,6 +13175,14 @@ "node": ">=8" } }, + "node_modules/uvu/node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/v8-to-istanbul": { "version": "9.3.0", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.3.0.tgz", diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index 24b8ebf1f..917f7d597 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -678,6 +678,7 @@ "core": "file:../../core", "cors": "^2.8.5", "dbinfoz": "^0.14.0", + "diff": "^7.0.0", "downshift": "^7.6.0", "esbuild": "^0.17.19", "express": "^4.18.2", diff --git a/extensions/vscode/src/VsCodeIde.ts b/extensions/vscode/src/VsCodeIde.ts index 5138ba490..9e237350e 100644 --- a/extensions/vscode/src/VsCodeIde.ts +++ b/extensions/vscode/src/VsCodeIde.ts @@ -480,7 +480,6 @@ class VsCodeIde implements IDE { const contents = new TextDecoder().decode(truncatedBytes); return contents; } catch (e) { - console.warn("Error reading file", e); return ""; } } diff --git a/extensions/vscode/src/autocomplete/completionProvider.ts b/extensions/vscode/src/autocomplete/completionProvider.ts index f9d397388..2a2f41a88 100644 --- a/extensions/vscode/src/autocomplete/completionProvider.ts +++ b/extensions/vscode/src/autocomplete/completionProvider.ts @@ -6,6 +6,7 @@ import { import { ConfigHandler } from "core/config/ConfigHandler"; import { v4 as uuidv4 } from "uuid"; import * as vscode from "vscode"; +const Diff = require("diff"); import { showFreeTrialLoginMessage } from "../util/messages"; import { VsCodeWebviewProtocol } from "../webviewProtocol"; @@ -22,6 +23,13 @@ import { import type { IDE } from "core"; import type { TabAutocompleteModel } from "../util/loadAutocompleteModel"; +interface DiffType { + count: number; + added: boolean; + removed: boolean; + value: string; +} + interface VsCodeCompletionInput { document: vscode.TextDocument; position: vscode.Position; @@ -105,6 +113,12 @@ export class ContinueCompletionProvider return null; } + // Don't autocomplete with multi-cursor + const editor = vscode.window.activeTextEditor; + if (editor && editor.selections.length > 1) { + return null; + } + // If the text at the range isn't a prefix of the intellisense text, // no completion will be displayed, regardless of what we return if ( @@ -190,12 +204,20 @@ export class ContinueCompletionProvider } } } + + // Manually pass file contents for unsaved, untitled files + let filepath = document.uri.fsPath; + if (document.isUntitled) { + manuallyPassFileContents = document.getText(); + } + // Handle commit message input box let manuallyPassPrefix: string | undefined = undefined; const input: AutocompleteInput = { + isUntitledFile: document.isUntitled, completionId: uuidv4(), - filepath: document.uri.fsPath, + filepath, pos, recentlyEditedFiles: [], recentlyEditedRanges: @@ -249,13 +271,67 @@ export class ContinueCompletionProvider // Construct the range/text to show const startPos = selectedCompletionInfo?.range.start ?? position; - const completionRange = new vscode.Range( - startPos, - startPos.translate(0, outcome.completion.length), - ); + let range = new vscode.Range(startPos, startPos); + let completionText = outcome.completion; + const isSingleLineCompletion = outcome.completion.split("\n").length <= 1; + + if (isSingleLineCompletion) { + const lastLineOfCompletionText = completionText.split("\n").pop(); + const currentText = document + .lineAt(startPos) + .text.substring(startPos.character); + const diffs: DiffType[] = Diff.diffWords( + currentText, + lastLineOfCompletionText, + ); + + 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; + } + } + } else { + // Extend the range to the end of the line for multiline completions + range = new vscode.Range(startPos, document.lineAt(startPos).range.end); + } + const completionItem = new vscode.InlineCompletionItem( - outcome.completion, - completionRange, + completionText, + range, { title: "Log Autocomplete Outcome", command: "continue.logAutocompleteOutcome", @@ -294,3 +370,26 @@ 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; +}