Merge pull request #3347 from continuedev/nate/vsc-completion-diff

WIP better VS Code completion display
This commit is contained in:
Nate Sesti 2024-12-13 09:11:09 -08:00 • committed by GitHub
commit 05796ea3cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 166 additions and 17 deletions

View file

@ -16,6 +16,8 @@ import {
streamWithNewLines,
} from "./lineStream";
const STOP_AT_PATTERNS = ["diff --git"];
export class StreamTransformPipeline {
async *transform(
generator: AsyncGenerator<string>,
@ -28,7 +30,10 @@ export class StreamTransformPipeline {
): AsyncGenerator<string> {
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({

View file

@ -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: {

View file

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

View file

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

View file

@ -13,6 +13,7 @@ export type RecentlyEditedRange = RangeInFile & {
};
export interface AutocompleteInput {
isUntitledFile: boolean;
completionId: string;
filepath: string;
pos: Position;

14
core/util/findLast.ts Normal file
View file

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

View file

@ -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",

View file

@ -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",

View file

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

View file

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