From ac2e8056f4553eb573cd1b7d11f728f6b9392fca Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 23 Feb 2024 08:12:42 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20recently=20edited=20snippe?= =?UTF-8?q?ts=20for=20autocomplete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/ast.ts | 76 +++++++++++++++++++ core/autocomplete/constructPrompt.ts | 58 +++++--------- core/autocomplete/recentlyEdited.ts | 5 ++ core/autocomplete/retrieval.ts | 24 ++++++ .../src/autocomplete/getTabCompletion.ts | 5 +- .../vscode/src/autocomplete/recentlyEdited.ts | 65 ++++++++++++++++ 6 files changed, 192 insertions(+), 41 deletions(-) create mode 100644 core/autocomplete/ast.ts create mode 100644 core/autocomplete/recentlyEdited.ts create mode 100644 core/autocomplete/retrieval.ts create mode 100644 extensions/vscode/src/autocomplete/recentlyEdited.ts diff --git a/core/autocomplete/ast.ts b/core/autocomplete/ast.ts new file mode 100644 index 000000000..223a5ae5d --- /dev/null +++ b/core/autocomplete/ast.ts @@ -0,0 +1,76 @@ +import Parser from "web-tree-sitter"; +import { RangeInFileWithContents } from "../commands/util"; +import { getParserForFile } from "../util/treeSitter"; + +export async function getAst( + filepath: string, + fileContents: string +): Promise { + const parser = await getParserForFile(filepath); + + if (!parser) { + return undefined; + } + + const ast = parser.parse(fileContents); + return ast; +} + +export async function getTreePathAtCursor( + ast: Parser.Tree, + cursorIndex: number +): Promise { + const path = [ast.rootNode]; + while (path[path.length - 1].childCount > 0) { + let foundChild = false; + for (let child of path[path.length - 1].children) { + if (child.startIndex <= cursorIndex && child.endIndex >= cursorIndex) { + path.push(child); + foundChild = true; + break; + } + } + + if (!foundChild) { + break; + } + } + + return path; +} + +export async function getScopeAroundRange( + range: RangeInFileWithContents +): Promise { + const ast = await getAst(range.filepath, range.contents); + if (!ast) { + return undefined; + } + + const { start: s, end: e } = range.range; + const lines = range.contents.split("\n"); + const startIndex = + lines.slice(0, s.line).join("\n").length + + lines[s.line].slice(s.character).length; + const endIndex = + lines.slice(0, e.line).join("\n").length + + lines[e.line].slice(0, e.character).length; + + let node = ast.rootNode; + while (node.childCount > 0) { + let foundChild = false; + for (let child of node.children) { + if (child.startIndex < startIndex && child.endIndex > endIndex) { + node = child; + foundChild = true; + break; + } + } + + if (!foundChild) { + break; + } + } + + return node.text; +} diff --git a/core/autocomplete/constructPrompt.ts b/core/autocomplete/constructPrompt.ts index f9f28704c..71a3b5168 100644 --- a/core/autocomplete/constructPrompt.ts +++ b/core/autocomplete/constructPrompt.ts @@ -1,12 +1,14 @@ import Parser from "web-tree-sitter"; import { TabAutocompleteOptions } from ".."; +import { RangeInFileWithContents } from "../commands/util"; import { countTokens, pruneLinesFromBottom, pruneLinesFromTop, } from "../llm/countTokens"; import { getBasename } from "../util"; -import { getParserForFile } from "../util/treeSitter"; + +import { getAst, getScopeAroundRange, getTreePathAtCursor } from "./ast"; import { AutocompleteLanguageInfo, LANGUAGES, Typescript } from "./languages"; export function languageForFilepath( @@ -29,43 +31,6 @@ function formatExternalSnippet( return lines.join("\n"); } -async function getAst( - filepath: string, - fileContents: string -): Promise { - const parser = await getParserForFile(filepath); - - if (!parser) { - return undefined; - } - - const ast = parser.parse(fileContents); - return ast; -} - -async function getTreePathAtCursor( - ast: Parser.Tree, - cursorIndex: number -): Promise { - const path = [ast.rootNode]; - while (path[path.length - 1].childCount > 0) { - let foundChild = false; - for (let child of path[path.length - 1].children) { - if (child.startIndex <= cursorIndex && child.endIndex >= cursorIndex) { - path.push(child); - foundChild = true; - break; - } - } - - if (!foundChild) { - break; - } - } - - return path; -} - export interface AutocompleteSnippet { filepath: string; content: string; @@ -111,7 +76,8 @@ export async function constructAutocompletePrompt( line: number, character: number ) => Promise, - options: TabAutocompleteOptions + options: TabAutocompleteOptions, + recentlyEditedRanges: RangeInFileWithContents[] ): Promise<{ prefix: string; suffix: string; @@ -119,7 +85,19 @@ export async function constructAutocompletePrompt( completeMultiline: boolean; }> { // Find external snippets - const snippets: AutocompleteSnippet[] = []; + const snippets: AutocompleteSnippet[] = (await Promise.all( + recentlyEditedRanges + .map(async (r) => { + const scope = await getScopeAroundRange(r); + if (!scope) return null; + + return { + filepath: r.filepath, + content: r.contents, + }; + }) + .filter((s) => !!s) + )) as any; let treePath: Parser.SyntaxNode[] | undefined; try { diff --git a/core/autocomplete/recentlyEdited.ts b/core/autocomplete/recentlyEdited.ts new file mode 100644 index 000000000..d809066a4 --- /dev/null +++ b/core/autocomplete/recentlyEdited.ts @@ -0,0 +1,5 @@ +import { RangeInFileWithContents } from "../commands/util"; + +export type RecentlyEditedRange = RangeInFileWithContents & { + timestamp: number; +}; diff --git a/core/autocomplete/retrieval.ts b/core/autocomplete/retrieval.ts new file mode 100644 index 000000000..2beb3b44d --- /dev/null +++ b/core/autocomplete/retrieval.ts @@ -0,0 +1,24 @@ +import { Chunk } from ".."; +import { FullTextSearchCodebaseIndex } from "../indexing/FullTextSearch"; +import { IndexTag } from "../indexing/types"; + +export async function fullTextRetrieve( + prefix: string, + suffix: string, + indexTag: IndexTag +): Promise { + const index = new FullTextSearchCodebaseIndex(); + const searchStrings = prefix.split("\n").slice(-3); + const results: Chunk[] = []; + searchStrings.forEach(async (searchString) => { + const chunks = await index.retrieve( + [indexTag], + searchString, + 3, + undefined, + undefined + ); + results.push(...chunks); + }); + return results; +} diff --git a/extensions/vscode/src/autocomplete/getTabCompletion.ts b/extensions/vscode/src/autocomplete/getTabCompletion.ts index 4220772b7..b729cbdc8 100644 --- a/extensions/vscode/src/autocomplete/getTabCompletion.ts +++ b/extensions/vscode/src/autocomplete/getTabCompletion.ts @@ -19,6 +19,7 @@ import * as vscode from "vscode"; import { ideProtocolClient } from "../activation/activate"; import { TabAutocompleteModel } from "../loadConfig"; import { ContinueCompletionProvider } from "./completionProvider"; +import { RecentlyEditedTracker } from "./recentlyEdited"; import { setupStatusBar, stopStatusBarLoading } from "./statusBar"; async function getDefinition( @@ -57,6 +58,7 @@ export interface AutocompleteOutcome { } const autocompleteCache = AutocompleteLruCache.get(); +const recentlyEditedTracker = new RecentlyEditedTracker(); export async function getTabCompletion( document: vscode.TextDocument, @@ -105,7 +107,8 @@ export async function getTabCompletion( clipboardText, lang, getDefinition, - options + options, + await recentlyEditedTracker.getRecentlyEditedRanges() ); const { template, completionOptions } = options.template diff --git a/extensions/vscode/src/autocomplete/recentlyEdited.ts b/extensions/vscode/src/autocomplete/recentlyEdited.ts new file mode 100644 index 000000000..f66f26d55 --- /dev/null +++ b/extensions/vscode/src/autocomplete/recentlyEdited.ts @@ -0,0 +1,65 @@ +import { RecentlyEditedRange } from "core/autocomplete/recentlyEdited"; +import * as vscode from "vscode"; + +interface VsCodeRecentlyEditedRange { + timestamp: number; + uri: vscode.Uri; + range: vscode.Range; +} + +export class RecentlyEditedTracker { + private static staleTime = 1000 * 60 * 2; + private static maxRecentlyEditedRanges = 3; + private recentlyEditedRanges: VsCodeRecentlyEditedRange[] = []; + + constructor() { + vscode.workspace.onDidChangeTextDocument((event) => { + event.contentChanges.forEach((change) => { + const editedRange = { + uri: event.document.uri, + range: change.range, + timestamp: Date.now(), + }; + const newLength = this.recentlyEditedRanges.unshift(editedRange); + if (newLength >= RecentlyEditedTracker.maxRecentlyEditedRanges) { + this.recentlyEditedRanges = this.recentlyEditedRanges.slice( + 0, + RecentlyEditedTracker.maxRecentlyEditedRanges + ); + } + }); + }); + + setInterval(() => { + this.removeOldEntries(); + }, 1000 * 15); + } + + private removeOldEntries() { + this.recentlyEditedRanges = this.recentlyEditedRanges.filter( + (entry) => entry.timestamp > Date.now() - RecentlyEditedTracker.staleTime + ); + } + + public async getRecentlyEditedRanges(): Promise { + return Promise.all( + this.recentlyEditedRanges.map(async (entry) => ({ + timestamp: entry.timestamp, + filepath: entry.uri.fsPath, + contents: await vscode.workspace.fs + .readFile(entry.uri) + .then((content) => content.toString()), + range: { + start: { + line: entry.range.start.line, + character: entry.range.start.character, + }, + end: { + line: entry.range.end.line, + character: entry.range.end.character, + }, + }, + })) + ); + } +}