From 2982d7c8432eccf783bdc3620b1e1fab7d398d2b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 26 Mar 2024 11:57:00 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20add=20filter=20for=20midline=20i?= =?UTF-8?q?mports/top-level=20keywords=20and=20encoding=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/completionProvider.ts | 41 +++++++++++++------------ core/autocomplete/lineStream.ts | 16 ++++++++++ 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/core/autocomplete/completionProvider.ts b/core/autocomplete/completionProvider.ts index 8e0664136..11ccf65af 100644 --- a/core/autocomplete/completionProvider.ts +++ b/core/autocomplete/completionProvider.ts @@ -18,6 +18,7 @@ import { import { AutocompleteLanguageInfo } from "./languages"; import { avoidPathLine, + noTopLevelKeywordsMidline, stopAtLines, stopAtRepeatingLines, stopAtSimilarLine, @@ -53,7 +54,7 @@ const autocompleteCache = AutocompleteLruCache.get(); function formatExternalSnippet( filepath: string, snippet: string, - language: AutocompleteLanguageInfo + language: AutocompleteLanguageInfo, ) { const comment = language.comment; const lines = [ @@ -78,8 +79,8 @@ export async function getTabCompletion( filepath: string, contents: string, cursorIndex: number, - ide: IDE - ) => Promise + ide: IDE, + ) => Promise, ): Promise { const startTime = Date.now(); @@ -129,7 +130,7 @@ export async function getTabCompletion( filepath, fullPrefix + fullSuffix, fullPrefix.length, - ide + ide, ), new Promise((resolve) => { setTimeout(() => resolve([]), 100); @@ -155,7 +156,7 @@ export async function getTabCompletion( recentlyEditedRanges, recentlyEditedFiles, llm.model, - extrasSnippets + extrasSnippets, ); // Template prompt @@ -172,7 +173,7 @@ export async function getTabCompletion( // Format snippets as comments and prepend to prefix const formattedSnippets = snippets .map((snippet) => - formatExternalSnippet(snippet.filepath, snippet.contents, lang) + formatExternalSnippet(snippet.filepath, snippet.contents, lang), ) .join("\n"); if (formattedSnippets.length > 0) { @@ -208,6 +209,7 @@ export async function getTabCompletion( ...(completionOptions?.stop || []), "\n\n", "/src/", + "#- coding: utf-8", "```", ...lang.stopWords, ]; @@ -224,7 +226,7 @@ export async function getTabCompletion( raw: true, stop, }), - multiline + multiline, ); // LLM @@ -241,14 +243,13 @@ export async function getTabCompletion( let chars = generatorWithCancellation(); const gen2 = onlyWhitespaceAfterEndOfLine( noFirstCharNewline(chars), - lang.endOfLine - ); - const lineGenerator = streamWithNewLines( - avoidPathLine( - stopAtRepeatingLines(stopAtLines(streamLines(gen2))), - lang.comment - ) + lang.endOfLine, ); + let lineGenerator = stopAtRepeatingLines(stopAtLines(streamLines(gen2))); + lineGenerator = avoidPathLine(lineGenerator, lang.comment); + lineGenerator = noTopLevelKeywordsMidline(lineGenerator, lang.stopWords); + lineGenerator = streamWithNewLines(lineGenerator); + const finalGenerator = stopAtSimilarLine(lineGenerator, lineBelowCursor); for await (const update of finalGenerator) { completion += update; @@ -293,11 +294,11 @@ export class CompletionProvider { filepath: string, contents: string, cursorIndex: number, - ide: IDE - ) => Promise + ide: IDE, + ) => Promise, ) { this.generatorReuseManager = new GeneratorReuseManager( - this.onError.bind(this) + this.onError.bind(this), ); } @@ -347,7 +348,7 @@ export class CompletionProvider { public async provideInlineCompletionItems( input: AutocompleteInput, - token: AbortSignal | undefined + token: AbortSignal | undefined, ): Promise { // Create abort signal if not given if (!token) { @@ -372,7 +373,7 @@ export class CompletionProvider { const lastUUID = await new Promise((resolve) => setTimeout(() => { resolve(CompletionProvider.lastUUID); - }, options.debounceDelay) + }, options.debounceDelay), ); if (uuid !== lastUUID) { return undefined; @@ -402,7 +403,7 @@ export class CompletionProvider { this.ide, this.generatorReuseManager, input, - this.getDefinitionsFromLsp + this.getDefinitionsFromLsp, ); const completion = outcome?.completion; diff --git a/core/autocomplete/lineStream.ts b/core/autocomplete/lineStream.ts index b24cb1458..735eb9995 100644 --- a/core/autocomplete/lineStream.ts +++ b/core/autocomplete/lineStream.ts @@ -2,6 +2,22 @@ import { distance } from "fastest-levenshtein"; import { DiffLine } from ".."; import { LineStream } from "../diff/util"; +export async function* noTopLevelKeywordsMidline( + lines: LineStream, + topLevelKeywords: string[], +): LineStream { + for await (const line of lines) { + for (const keyword of topLevelKeywords) { + const indexOf = line.indexOf(keyword + " "); + if (indexOf >= 0 && line.slice(indexOf - 1, indexOf).trim() !== "") { + yield line.slice(0, indexOf); + break; + } + } + yield line; + } +} + export async function* avoidPathLine( stream: LineStream, comment: string,