| 1 | import { IDE, RangeInFileWithContents } from "../.."; |
| 2 | import { PrecalculatedLruCache } from "../../util/LruCache"; |
| 3 | import { |
| 4 | getFullLanguageName, |
| 5 | getParserForFile, |
| 6 | getQueryForFile, |
| 7 | } from "../../util/treeSitter"; |
| 8 | import { findUriInDirs } from "../../util/uri"; |
| 9 | |
| 10 | interface FileInfo { |
| 11 | imports: { [key: string]: RangeInFileWithContents[] }; |
| 12 | } |
| 13 | |
| 14 | export class ImportDefinitionsService { |
| 15 | static N = 10; |
| 16 | |
| 17 | private cache: PrecalculatedLruCache<FileInfo> = |
| 18 | new PrecalculatedLruCache<FileInfo>( |
| 19 | this._getFileInfo.bind(this), |
| 20 | ImportDefinitionsService.N, |
| 21 | ); |
| 22 | |
| 23 | constructor(private readonly ide: IDE) { |
| 24 | ide.onDidChangeActiveTextEditor((filepath) => { |
| 25 | this.cache |
| 26 | .initKey(filepath) |
| 27 | .catch((e) => |
| 28 | console.warn( |
| 29 | `Failed to initialize ImportDefinitionService: ${e.message}`, |
| 30 | ), |
| 31 | ); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | get(filepath: string): FileInfo | undefined { |
| 36 | return this.cache.get(filepath); |
| 37 | } |
| 38 | |
| 39 | private async _getFileInfo(filepath: string): Promise<FileInfo | null> { |
| 40 | if (filepath.endsWith(".ipynb")) { |
| 41 | // Commenting out this line was the solution to https://github.com/continuedev/continue/issues/1463 |
| 42 | return null; |
| 43 | } |
| 44 | |
| 45 | const parser = await getParserForFile(filepath); |
| 46 | if (!parser) { |
| 47 | return { |
| 48 | imports: {}, |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | let fileContents: string | undefined = undefined; |
| 53 | try { |
| 54 | const { foundInDir } = findUriInDirs( |
| 55 | filepath, |
| 56 | await this.ide.getWorkspaceDirs(), |
| 57 | ); |
| 58 | if (!foundInDir) { |
| 59 | return null; |
| 60 | } else { |
| 61 | fileContents = await this.ide.readFile(filepath); |
| 62 | } |
| 63 | } catch (err) { |
| 64 | // File removed |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | const ast = parser.parse(fileContents, undefined, { |
| 69 | includedRanges: [ |
| 70 | { |
| 71 | startIndex: 0, |
| 72 | endIndex: 10_000, |
| 73 | startPosition: { row: 0, column: 0 }, |
| 74 | endPosition: { row: 100, column: 0 }, |
| 75 | }, |
| 76 | ], |
| 77 | }); |
| 78 | const language = getFullLanguageName(filepath); |
| 79 | const query = await getQueryForFile( |
| 80 | filepath, |
| 81 | `import-queries/${language}.scm`, |
| 82 | ); |
| 83 | if (!query) { |
| 84 | return { |
| 85 | imports: {}, |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | const matches = query?.matches(ast.rootNode); |
| 90 | |
| 91 | const fileInfo: FileInfo = { |
| 92 | imports: {}, |
| 93 | }; |
| 94 | for (const match of matches) { |
| 95 | const startPosition = match.captures[0].node.startPosition; |
| 96 | const defs = await this.ide.gotoDefinition({ |
| 97 | filepath, |
| 98 | position: { |
| 99 | line: startPosition.row, |
| 100 | character: startPosition.column, |
| 101 | }, |
| 102 | }); |
| 103 | fileInfo.imports[match.captures[0].node.text] = await Promise.all( |
| 104 | defs.map(async (def) => ({ |
| 105 | ...def, |
| 106 | contents: await this.ide.readRangeInFile(def.filepath, def.range), |
| 107 | })), |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | return fileInfo; |
| 112 | } |
| 113 | } |