| 1 | import { createHash } from "crypto"; |
| 2 | |
| 3 | import { LRUCache } from "lru-cache"; |
| 4 | import Parser from "web-tree-sitter"; |
| 5 | |
| 6 | import { IDE } from "../../.."; |
| 7 | import { |
| 8 | getFullLanguageName, |
| 9 | getQueryForFile, |
| 10 | IGNORE_PATH_PATTERNS, |
| 11 | LanguageName, |
| 12 | } from "../../../util/treeSitter"; |
| 13 | import { |
| 14 | AutocompleteCodeSnippet, |
| 15 | AutocompleteSnippetType, |
| 16 | } from "../../snippets/types"; |
| 17 | import { AutocompleteSnippetDeprecated } from "../../types"; |
| 18 | import { AstPath } from "../../util/ast"; |
| 19 | import { ImportDefinitionsService } from "../ImportDefinitionsService"; |
| 20 | |
| 21 | // function getSyntaxTreeString( |
| 22 | // node: Parser.SyntaxNode, |
| 23 | // indent: string = "", |
| 24 | // ): string { |
| 25 | // let result = ""; |
| 26 | // const nodeInfo = `${node.type} [${node.startPosition.row}:${node.startPosition.column} - ${node.endPosition.row}:${node.endPosition.column}]`; |
| 27 | // result += `${indent}${nodeInfo}\n`; |
| 28 | |
| 29 | // for (const child of node.children) { |
| 30 | // result += getSyntaxTreeString(child, indent + " "); |
| 31 | // } |
| 32 | |
| 33 | // return result; |
| 34 | // } |
| 35 | |
| 36 | export class RootPathContextService { |
| 37 | private cache = new LRUCache<string, AutocompleteSnippetDeprecated[]>({ |
| 38 | max: 100, |
| 39 | }); |
| 40 | |
| 41 | constructor( |
| 42 | private readonly importDefinitionsService: ImportDefinitionsService, |
| 43 | private readonly ide: IDE, |
| 44 | ) {} |
| 45 | |
| 46 | private static getNodeId(node: Parser.SyntaxNode): string { |
| 47 | return `${node.startIndex}`; |
| 48 | } |
| 49 | |
| 50 | private static TYPES_TO_USE = new Set([ |
| 51 | "arrow_function", |
| 52 | "generator_function_declaration", |
| 53 | "program", |
| 54 | "function_declaration", |
| 55 | "function_definition", |
| 56 | "method_definition", |
| 57 | "method_declaration", |
| 58 | "class_declaration", |
| 59 | "class_definition", |
| 60 | ]); |
| 61 | |
| 62 | /** |
| 63 | * Key comes from hash of parent key and node type and node id. |
| 64 | */ |
| 65 | private static keyFromNode( |
| 66 | parentKey: string, |
| 67 | astNode: Parser.SyntaxNode, |
| 68 | ): string { |
| 69 | return createHash("sha256") |
| 70 | .update(parentKey) |
| 71 | .update(astNode.type) |
| 72 | .update(RootPathContextService.getNodeId(astNode)) |
| 73 | .digest("hex"); |
| 74 | } |
| 75 | |
| 76 | private async getSnippetsForNode( |
| 77 | filepath: string, |
| 78 | node: Parser.SyntaxNode, |
| 79 | ): Promise<AutocompleteSnippetDeprecated[]> { |
| 80 | const snippets: AutocompleteSnippetDeprecated[] = []; |
| 81 | const language = getFullLanguageName(filepath); |
| 82 | |
| 83 | let query: Parser.Query | undefined; |
| 84 | switch (node.type) { |
| 85 | case "program": |
| 86 | this.importDefinitionsService.get(filepath); |
| 87 | break; |
| 88 | default: |
| 89 | // const type = node.type; |
| 90 | // console.log(getSyntaxTreeString(node)); |
| 91 | |
| 92 | query = await getQueryForFile( |
| 93 | filepath, |
| 94 | `root-path-context-queries/${language}/${node.type}.scm`, |
| 95 | ); |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | if (!query) { |
| 100 | return snippets; |
| 101 | } |
| 102 | |
| 103 | const queries = query.matches(node).map(async (match) => { |
| 104 | for (const item of match.captures) { |
| 105 | try { |
| 106 | const endPosition = item.node.endPosition; |
| 107 | const newSnippets = await this.getSnippets( |
| 108 | filepath, |
| 109 | endPosition, |
| 110 | language, |
| 111 | ); |
| 112 | snippets.push(...newSnippets); |
| 113 | } catch (e) { |
| 114 | throw e; |
| 115 | } |
| 116 | } |
| 117 | }); |
| 118 | |
| 119 | await Promise.all(queries); |
| 120 | |
| 121 | return snippets; |
| 122 | } |
| 123 | |
| 124 | private async getSnippets( |
| 125 | filepath: string, |
| 126 | endPosition: Parser.Point, |
| 127 | language: LanguageName, |
| 128 | ): Promise<AutocompleteSnippetDeprecated[]> { |
| 129 | const definitions = await this.ide.gotoDefinition({ |
| 130 | filepath, |
| 131 | position: { |
| 132 | line: endPosition.row, |
| 133 | character: endPosition.column, |
| 134 | }, |
| 135 | }); |
| 136 | const newSnippets = await Promise.all( |
| 137 | definitions |
| 138 | .filter((definition) => { |
| 139 | const isIgnoredPath = IGNORE_PATH_PATTERNS[language]?.some( |
| 140 | (pattern) => pattern.test(definition.filepath), |
| 141 | ); |
| 142 | |
| 143 | return !isIgnoredPath; |
| 144 | }) |
| 145 | .map(async (def) => ({ |
| 146 | ...def, |
| 147 | contents: await this.ide.readRangeInFile(def.filepath, def.range), |
| 148 | })), |
| 149 | ); |
| 150 | |
| 151 | return newSnippets; |
| 152 | } |
| 153 | |
| 154 | async getContextForPath( |
| 155 | filepath: string, |
| 156 | astPath: AstPath, |
| 157 | // cursorIndex: number, |
| 158 | ): Promise<AutocompleteCodeSnippet[]> { |
| 159 | const snippets: AutocompleteCodeSnippet[] = []; |
| 160 | |
| 161 | let parentKey = filepath; |
| 162 | for (const astNode of astPath.filter((node) => |
| 163 | RootPathContextService.TYPES_TO_USE.has(node.type), |
| 164 | )) { |
| 165 | const key = RootPathContextService.keyFromNode(parentKey, astNode); |
| 166 | // const type = astNode.type; |
| 167 | |
| 168 | const foundInCache = this.cache.get(key); |
| 169 | const newSnippets = |
| 170 | foundInCache ?? (await this.getSnippetsForNode(filepath, astNode)); |
| 171 | |
| 172 | const formattedSnippets: AutocompleteCodeSnippet[] = newSnippets.map( |
| 173 | (item) => ({ |
| 174 | filepath: item.filepath, |
| 175 | content: item.contents, |
| 176 | type: AutocompleteSnippetType.Code, |
| 177 | }), |
| 178 | ); |
| 179 | |
| 180 | snippets.push(...formattedSnippets); |
| 181 | |
| 182 | if (!foundInCache) { |
| 183 | this.cache.set(key, newSnippets); |
| 184 | } |
| 185 | |
| 186 | parentKey = key; |
| 187 | } |
| 188 | |
| 189 | return snippets; |
| 190 | } |
| 191 | } |