feat: create RecentFilesService
This commit is contained in:
parent
ec5fa382ad
commit
d597e6fe5e
3 changed files with 131 additions and 0 deletions
|
|
@ -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<AutocompleteCodeSnippet[]> {
|
||||
return this.recentFilesService.getSnippets();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
119
core/autocomplete/context/RecentFilesService.ts
Normal file
119
core/autocomplete/context/RecentFilesService.ts
Normal file
|
|
@ -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<string, AutocompleteCodeSnippet[]>;
|
||||
private numRecentFiles: number;
|
||||
|
||||
constructor(
|
||||
private readonly ide: IDE,
|
||||
numRecentFiles = DEFAULT_NUM_RECENT_FILES,
|
||||
) {
|
||||
this.numRecentFiles = numRecentFiles;
|
||||
this.cache = new LRUCache<string, AutocompleteCodeSnippet[]>({
|
||||
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<AutocompleteCodeSnippet[]> {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<T>(promise: Promise<T[]>): Promise<T[]> {
|
||||
|
|
@ -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,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue