| 1 | import { |
| 2 | ContextItem, |
| 3 | ContextProviderDescription, |
| 4 | ContextProviderExtras, |
| 5 | ContextSubmenuItem, |
| 6 | LoadSubmenuItemsArgs, |
| 7 | } from "../../index.js"; |
| 8 | import { CodeSnippetsCodebaseIndex } from "../../indexing/CodeSnippetsIndex.js"; |
| 9 | import { BaseContextProvider } from "../index.js"; |
| 10 | |
| 11 | const MAX_SUBMENU_ITEMS = 10_000; |
| 12 | |
| 13 | class CodeContextProvider extends BaseContextProvider { |
| 14 | static description: ContextProviderDescription = { |
| 15 | title: "code", |
| 16 | displayTitle: "Code", |
| 17 | description: "Type to search", |
| 18 | type: "submenu", |
| 19 | dependsOnIndexing: ["chunk", "codeSnippets"], |
| 20 | }; |
| 21 | |
| 22 | async getContextItems( |
| 23 | query: string, |
| 24 | extras: ContextProviderExtras, |
| 25 | ): Promise<ContextItem[]> { |
| 26 | // Assume the query is the id as returned by loadSubmenuItems |
| 27 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 28 | return [ |
| 29 | await CodeSnippetsCodebaseIndex.getForId( |
| 30 | Number.parseInt(query, 10), |
| 31 | workspaceDirs, |
| 32 | ), |
| 33 | ]; |
| 34 | } |
| 35 | |
| 36 | async loadSubmenuItems( |
| 37 | args: LoadSubmenuItemsArgs, |
| 38 | ): Promise<ContextSubmenuItem[]> { |
| 39 | // TODO: Dynamically load submenu items based on the query |
| 40 | // instead of loading everything into memory |
| 41 | const tags = await args.ide.getTags("codeSnippets"); |
| 42 | const snippets = await Promise.all( |
| 43 | tags.map((tag) => CodeSnippetsCodebaseIndex.getAll(tag)), |
| 44 | ); |
| 45 | |
| 46 | const submenuItems: ContextSubmenuItem[] = []; |
| 47 | |
| 48 | for (const snippetList of snippets.slice(-MAX_SUBMENU_ITEMS)) { |
| 49 | submenuItems.push(...snippetList); |
| 50 | } |
| 51 | |
| 52 | return submenuItems; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | export default CodeContextProvider; |