| 1 | import { |
| 2 | ContextItem, |
| 3 | ContextProviderDescription, |
| 4 | ContextProviderExtras, |
| 5 | } from "../../index.js"; |
| 6 | import { getUriDescription } from "../../util/uri.js"; |
| 7 | import { BaseContextProvider } from "../index.js"; |
| 8 | |
| 9 | class OpenFilesContextProvider extends BaseContextProvider { |
| 10 | static description: ContextProviderDescription = { |
| 11 | title: "open", |
| 12 | displayTitle: "Open Files", |
| 13 | description: "Reference the current open files", |
| 14 | type: "normal", |
| 15 | renderInlineAs: "", |
| 16 | }; |
| 17 | |
| 18 | async getContextItems( |
| 19 | query: string, |
| 20 | extras: ContextProviderExtras, |
| 21 | ): Promise<ContextItem[]> { |
| 22 | const ide = extras.ide; |
| 23 | const openFiles = this.options?.onlyPinned |
| 24 | ? await ide.getPinnedFiles() |
| 25 | : await ide.getOpenFiles(); |
| 26 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 27 | |
| 28 | return await Promise.all( |
| 29 | openFiles.map(async (filepath: string) => { |
| 30 | const content = await ide.readFile(filepath); |
| 31 | const { relativePathOrBasename, last2Parts, baseName } = |
| 32 | getUriDescription(filepath, workspaceDirs); |
| 33 | |
| 34 | return { |
| 35 | description: last2Parts, |
| 36 | content: `\`\`\`${relativePathOrBasename}\n${content}\n\`\`\``, |
| 37 | name: baseName, |
| 38 | uri: { |
| 39 | type: "file", |
| 40 | value: filepath, |
| 41 | }, |
| 42 | }; |
| 43 | }), |
| 44 | ); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | export default OpenFilesContextProvider; |