Elffuss BraidHub

kikocis/continue public

dfc814e4318e · 3.1 KiBRawPermalink
1import { IDE } from "../..";
2import {
3 AutocompleteCodeSnippet,
4 AutocompleteSnippetType,
5 AutocompleteStaticSnippet,
6} from "../snippets/types";
7import { HelperVars } from "../util/HelperVars";
8
9import { ImportDefinitionsService } from "./ImportDefinitionsService";
10import { getSymbolsForSnippet } from "./ranking";
11import { RootPathContextService } from "./root-path-context/RootPathContextService";
12import { StaticContextService } from "./static-context/StaticContextService";
13
14export class ContextRetrievalService {
15 private importDefinitionsService: ImportDefinitionsService;
16 private rootPathContextService: RootPathContextService;
17 private staticContextService: StaticContextService;
18
19 constructor(private readonly ide: IDE) {
20 this.importDefinitionsService = new ImportDefinitionsService(this.ide);
21 this.rootPathContextService = new RootPathContextService(
22 this.importDefinitionsService,
23 this.ide,
24 );
25 this.staticContextService = new StaticContextService(this.ide);
26 }
27
28 public async getSnippetsFromImportDefinitions(
29 helper: HelperVars,
30 ): Promise<AutocompleteCodeSnippet[]> {
31 if (helper.options.useImports === false) {
32 return [];
33 }
34
35 const importSnippets: AutocompleteCodeSnippet[] = [];
36 const fileInfo = this.importDefinitionsService.get(helper.filepath);
37 if (fileInfo) {
38 const { imports } = fileInfo;
39 // Look for imports of any symbols around the current range
40 const textAroundCursor =
41 helper.fullPrefix.split("\n").slice(-5).join("\n") +
42 helper.fullSuffix.split("\n").slice(0, 3).join("\n");
43 const symbols = Array.from(getSymbolsForSnippet(textAroundCursor)).filter(
44 (symbol) => !helper.lang.topLevelKeywords.includes(symbol),
45 );
46 for (const symbol of symbols) {
47 const rifs = imports[symbol];
48 if (Array.isArray(rifs)) {
49 const snippets: AutocompleteCodeSnippet[] = rifs.map((rif) => {
50 return {
51 filepath: rif.filepath,
52 content: rif.contents,
53 type: AutocompleteSnippetType.Code,
54 };
55 });
56
57 importSnippets.push(...snippets);
58 }
59 }
60 }
61
62 return importSnippets;
63 }
64
65 public async getRootPathSnippets(
66 helper: HelperVars,
67 ): Promise<AutocompleteCodeSnippet[]> {
68 if (!helper.treePath) {
69 return [];
70 }
71
72 return this.rootPathContextService.getContextForPath(
73 helper.filepath,
74 helper.treePath,
75 );
76 }
77
78 public async getStaticContextSnippets(
79 helper: HelperVars,
80 ): Promise<AutocompleteStaticSnippet[]> {
81 return this.staticContextService.getContext(helper);
82 }
83
84 /**
85 * Initialize the import definitions cache for a file.
86 * This is normally done automatically when the active text editor changes,
87 * but needs to be called manually when using context fetching outside the normal flow.
88 */
89 public async initializeForFile(filepath: string): Promise<void> {
90 try {
91 await (this.importDefinitionsService as any).cache.initKey(filepath);
92 } catch (e) {
93 console.warn(
94 `Failed to initialize import definitions cache for ${filepath}:`,
95 e,
96 );
97 }
98 }
99}