From d597e6fe5ea632f2bdf762ad2555c056fd736ba8 Mon Sep 17 00:00:00 2001 From: Test Date: Thu, 26 Dec 2024 15:53:35 -0600 Subject: [PATCH] feat: create RecentFilesService --- .../context/ContextRetrievalService.ts | 7 ++ .../context/RecentFilesService.ts | 119 ++++++++++++++++++ core/autocomplete/snippets/getAllSnippets.ts | 5 + 3 files changed, 131 insertions(+) create mode 100644 core/autocomplete/context/RecentFilesService.ts diff --git a/core/autocomplete/context/ContextRetrievalService.ts b/core/autocomplete/context/ContextRetrievalService.ts index 72e061fed..f372456be 100644 --- a/core/autocomplete/context/ContextRetrievalService.ts +++ b/core/autocomplete/context/ContextRetrievalService.ts @@ -7,11 +7,13 @@ import { HelperVars } from "../util/HelperVars"; import { ImportDefinitionsService } from "./ImportDefinitionsService"; import { getSymbolsForSnippet } from "./ranking"; +import { RecentFilesService } from "./RecentFilesService"; import { RootPathContextService } from "./root-path-context/RootPathContextService"; export class ContextRetrievalService { private importDefinitionsService: ImportDefinitionsService; private rootPathContextService: RootPathContextService; + private recentFilesService: RecentFilesService; constructor(private readonly ide: IDE) { this.importDefinitionsService = new ImportDefinitionsService(this.ide); @@ -19,6 +21,7 @@ export class ContextRetrievalService { this.importDefinitionsService, this.ide, ); + this.recentFilesService = new RecentFilesService(this.ide); } public async getSnippetsFromImportDefinitions( @@ -70,4 +73,8 @@ export class ContextRetrievalService { helper.treePath, ); } + + public async getRecentFileSnippets(): Promise { + return this.recentFilesService.getSnippets(); + } } diff --git a/core/autocomplete/context/RecentFilesService.ts b/core/autocomplete/context/RecentFilesService.ts new file mode 100644 index 000000000..b5a8f34c6 --- /dev/null +++ b/core/autocomplete/context/RecentFilesService.ts @@ -0,0 +1,119 @@ +import { LRUCache } from "lru-cache"; +import Parser from "web-tree-sitter"; + +import { IDE } from "../.."; +import { + getFullLanguageName, + getParserForFile, + getQueryForFile, +} from "../../util/treeSitter"; +import { + AutocompleteCodeSnippet, + AutocompleteSnippetType, +} from "../snippets/types"; + +const DEFAULT_NUM_RECENT_FILES = 5; + +export class RecentFilesService { + private cache: LRUCache; + private numRecentFiles: number; + + constructor( + private readonly ide: IDE, + numRecentFiles = DEFAULT_NUM_RECENT_FILES, + ) { + this.numRecentFiles = numRecentFiles; + this.cache = new LRUCache({ + max: this.numRecentFiles, + }); + + this.ide.onDidChangeActiveTextEditor((filepath) => { + void this.onFileOpened(filepath); + }); + } + + private async onFileOpened(filepath: string) { + // Remove from cache if already present to update its position + if (this.cache.has(filepath)) { + this.cache.delete(filepath); + } + + // Get function signatures from the file + const snippets = await this.getFunctionSignatures(filepath); + + if (snippets.length > 0) { + // Store in cache + this.cache.set(filepath, snippets); + } + } + + private async getFunctionSignatures( + filepath: string, + ): Promise { + const snippets: AutocompleteCodeSnippet[] = []; + + const parser = await getParserForFile(filepath); + if (!parser) { + return snippets; + } + + let fileContents: string; + try { + fileContents = await this.ide.readFile(filepath); + } catch (err) { + // File might have been deleted or inaccessible + return snippets; + } + + const ast = parser.parse(fileContents); + const language = getFullLanguageName(filepath); + const query = await getQueryForFile( + filepath, + `code-snippet-queries/${language}.scm`, + ); + + if (!query) { + return snippets; + } + + const matches = query.matches(ast.rootNode); + if (!matches) { + return snippets; + } + + return matches + .map((match) => this.getSnippetFromMatch(match, filepath)) + .filter( + (snippet): snippet is AutocompleteCodeSnippet => snippet !== null, + ); + } + + private getSnippetFromMatch( + match: Parser.QueryMatch, + filepath: string, + ): AutocompleteCodeSnippet | null { + // We only care about the function signature/declaration, not the body + for (const { name, node } of match.captures) { + // Skip function bodies, we only want the signature + // if (name.includes("body") || node.type.includes("body")) { + // continue; + // } + + return { + filepath, + content: node.text.trim(), + type: AutocompleteSnippetType.Code, + }; + } + + return null; + } + + public getSnippets(): AutocompleteCodeSnippet[] { + const allSnippets: AutocompleteCodeSnippet[] = []; + for (const snippets of this.cache.values()) { + allSnippets.push(...snippets); + } + return allSnippets; + } +} diff --git a/core/autocomplete/snippets/getAllSnippets.ts b/core/autocomplete/snippets/getAllSnippets.ts index 5ff4a7d60..a663c0dde 100644 --- a/core/autocomplete/snippets/getAllSnippets.ts +++ b/core/autocomplete/snippets/getAllSnippets.ts @@ -3,6 +3,7 @@ import { findUriInDirs } from "../../util/uri"; import { ContextRetrievalService } from "../context/ContextRetrievalService"; import { GetLspDefinitionsFunction } from "../types"; import { HelperVars } from "../util/HelperVars"; + import { AutocompleteClipboardSnippet, AutocompleteCodeSnippet, @@ -17,6 +18,7 @@ export interface SnippetPayload { recentlyEditedRangeSnippets: AutocompleteCodeSnippet[]; diffSnippets: AutocompleteDiffSnippet[]; clipboardSnippets: AutocompleteClipboardSnippet[]; + recentFileSnippets: AutocompleteCodeSnippet[]; } function racePromise(promise: Promise): Promise { @@ -118,6 +120,7 @@ export const getAllSnippets = async ({ ideSnippets, diffSnippets, clipboardSnippets, + recentFileSnippets, ] = await Promise.all([ racePromise(contextRetrievalService.getRootPathSnippets(helper)), racePromise( @@ -126,6 +129,7 @@ export const getAllSnippets = async ({ racePromise(getIdeSnippets(helper, ide, getDefinitionsFromLsp)), racePromise(getDiffSnippets(ide)), racePromise(getClipboardSnippets(ide)), + racePromise(contextRetrievalService.getRecentFileSnippets()), ]); return { @@ -135,5 +139,6 @@ export const getAllSnippets = async ({ recentlyEditedRangeSnippets, diffSnippets, clipboardSnippets, + recentFileSnippets, }; };