⚡️ recently edited snippets for autocomplete
This commit is contained in:
parent
50dafd6c2e
commit
ac2e8056f4
6 changed files with 192 additions and 41 deletions
76
core/autocomplete/ast.ts
Normal file
76
core/autocomplete/ast.ts
Normal file
|
|
@ -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<Parser.Tree | undefined> {
|
||||
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<Parser.SyntaxNode[] | undefined> {
|
||||
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<string | undefined> {
|
||||
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;
|
||||
}
|
||||
|
|
@ -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<Parser.Tree | undefined> {
|
||||
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<Parser.SyntaxNode[] | undefined> {
|
||||
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<AutocompleteSnippet | undefined>,
|
||||
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 {
|
||||
|
|
|
|||
5
core/autocomplete/recentlyEdited.ts
Normal file
5
core/autocomplete/recentlyEdited.ts
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import { RangeInFileWithContents } from "../commands/util";
|
||||
|
||||
export type RecentlyEditedRange = RangeInFileWithContents & {
|
||||
timestamp: number;
|
||||
};
|
||||
24
core/autocomplete/retrieval.ts
Normal file
24
core/autocomplete/retrieval.ts
Normal file
|
|
@ -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<Chunk[]> {
|
||||
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;
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
65
extensions/vscode/src/autocomplete/recentlyEdited.ts
Normal file
65
extensions/vscode/src/autocomplete/recentlyEdited.ts
Normal file
|
|
@ -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<RecentlyEditedRange[]> {
|
||||
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,
|
||||
},
|
||||
},
|
||||
}))
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue