⚡️ don't complete copy of line below cursor

This commit is contained in:
Nate Sesti 2024-02-05 21:34:26 -08:00
parent a02c4bd534
commit 0f935c682a
5 changed files with 47 additions and 26 deletions

View file

@ -0,0 +1,16 @@
import { distance } from "fastest-levenshtein";
import { LineStream } from "../diff/util";
export async function* stopAtSimilarLine(
stream: LineStream,
line: string
): AsyncGenerator<string> {
line = line.trim();
for await (const nextLine of stream) {
const dist = distance(nextLine.trim(), line);
if (nextLine.length > 4 && line.length > 4 && dist / line.length < 0.1) {
break;
}
yield nextLine;
}
}

View file

@ -56,7 +56,9 @@ export function matchLine(
/**
* Convert a stream of arbitrary chunks to a stream of lines
*/
export async function* streamLines(streamCompletion: LineStream): LineStream {
export async function* streamLines(
streamCompletion: AsyncGenerator<string>
): LineStream {
let buffer = "";
for await (const chunk of streamCompletion) {
buffer += chunk;

View file

@ -17,7 +17,7 @@
"fastest-levenshtein": "^1.0.16",
"handlebars": "^4.7.8",
"js-tiktoken": "^1.0.8",
"llm-code-highlighter": "^0.0.1",
"llm-code-highlighter": "^0.0.3",
"node-fetch": "^3.3.2",
"node-html-markdown": "^1.3.0",
"openai": "^4.20.1",
@ -4770,9 +4770,9 @@
"dev": true
},
"node_modules/llm-code-highlighter": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.1.tgz",
"integrity": "sha512-26PExkQxeh5EiBNroldpV82pZDrjC6C5v4PLcm5ZpGIKExrHs9Hqw36B+1Fg+dxXGZMnwlCZ+6EbC2FEVocmcw==",
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.3.tgz",
"integrity": "sha512-l/R+m6ngXRTYxzoCzqpBk5TiSVpS4Ek9fKx/PIy25HV6XDbCC6iOfVITPPSTGkh7NlI+tN5vWjdqUfN3YW+HwQ==",
"dependencies": {
"graphology": "^0.25.4",
"graphology-metrics": "^2.2.0",

View file

@ -2695,10 +2695,10 @@ lines-and-columns@^1.1.6:
resolved "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz"
integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==
llm-code-highlighter@^0.0.1:
version "0.0.1"
resolved "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.1.tgz"
integrity sha512-26PExkQxeh5EiBNroldpV82pZDrjC6C5v4PLcm5ZpGIKExrHs9Hqw36B+1Fg+dxXGZMnwlCZ+6EbC2FEVocmcw==
llm-code-highlighter@^0.0.3:
version "0.0.3"
resolved "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.3.tgz"
integrity sha512-l/R+m6ngXRTYxzoCzqpBk5TiSVpS4Ek9fKx/PIy25HV6XDbCC6iOfVITPPSTGkh7NlI+tN5vWjdqUfN3YW+HwQ==
dependencies:
graphology "^0.25.4"
graphology-metrics "^2.2.0"

View file

@ -6,7 +6,9 @@ import {
languageForFilepath,
} from "core/autocomplete/constructPrompt";
import { DEFAULT_AUTOCOMPLETE_OPTS } from "core/autocomplete/parameters";
import { stopAtSimilarLine } from "core/autocomplete/streaming";
import { getTemplateForModel } from "core/autocomplete/templates";
import { streamLines } from "core/diff/util";
import Handlebars from "handlebars";
import { v4 as uuidv4 } from "uuid";
import * as vscode from "vscode";
@ -259,6 +261,7 @@ async function getTabCompletion(
new vscode.Position(document.lineCount, Number.MAX_SAFE_INTEGER)
)
);
const lineBelowCursor = document.lineAt(pos.line + 1).text;
const clipboardText = await vscode.env.clipboard.readText();
const { prefix, suffix } = await constructAutocompletePrompt(
document.fileName,
@ -306,25 +309,25 @@ async function getTabCompletion(
);
// LLM
for await (const update of generator) {
completion += update;
if (token.isCancellationRequested) {
stopStatusBarLoading();
return undefined;
}
let foundEndLine = false;
for (const end of lang.endOfLine) {
if (completion.includes(end + "\n")) {
// completion =
// completion.slice(0, completion.indexOf(end + "\n")) + end;
// foundEndLine = true;
// break;
let cancelled = false;
const generatorWithCancellation = async function* () {
for await (const update of generator) {
if (token.isCancellationRequested) {
stopStatusBarLoading();
cancelled = true;
return undefined;
}
yield update;
}
if (foundEndLine) {
break;
}
};
const lineGenerator = streamLines(generatorWithCancellation());
const finalGenerator = stopAtSimilarLine(lineGenerator, lineBelowCursor);
for await (const update of finalGenerator) {
completion += update;
}
if (cancelled) {
return undefined;
}
// Don't return empty