| 1 | import * as fs from "fs/promises"; |
| 2 | import Parser from "web-tree-sitter"; |
| 3 | import { getFullLanguageName, getQueryForFile } from "../../../util/treeSitter"; |
| 4 | import { getAst } from "../../util/ast"; |
| 5 | |
| 6 | export interface TypeDeclarationResult { |
| 7 | name: string; |
| 8 | fullText: string; |
| 9 | startLine: number; |
| 10 | startColumn: number; |
| 11 | endLine: number; |
| 12 | endColumn: number; |
| 13 | kind: string; |
| 14 | } |
| 15 | |
| 16 | export function findEnclosingTypeDeclaration( |
| 17 | sourceCode: string, |
| 18 | cursorLine: number, |
| 19 | cursorColumn: number, |
| 20 | ast: Parser.Tree, |
| 21 | ): TypeDeclarationResult | null { |
| 22 | const point = { row: cursorLine, column: cursorColumn }; |
| 23 | let node = ast.rootNode.descendantForPosition(point); |
| 24 | |
| 25 | while ( |
| 26 | node && |
| 27 | ![ |
| 28 | "type_alias_declaration", |
| 29 | "interface_declaration", |
| 30 | "enum_declaration", |
| 31 | ].includes(node.type) |
| 32 | ) { |
| 33 | if (!node.parent) return null; |
| 34 | node = node.parent; |
| 35 | } |
| 36 | |
| 37 | if (!node) return null; |
| 38 | |
| 39 | const nameNode = node.childForFieldName("name"); |
| 40 | const name = nameNode?.text ?? "<anonymous>"; |
| 41 | const fullText = sourceCode.slice(node.startIndex, node.endIndex); |
| 42 | |
| 43 | return { |
| 44 | name, |
| 45 | fullText, |
| 46 | startLine: node.startPosition.row, |
| 47 | startColumn: node.startPosition.column, |
| 48 | endLine: node.endPosition.row, |
| 49 | endColumn: node.endPosition.column, |
| 50 | kind: node.type, |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | export async function extractTopLevelDecls( |
| 55 | currentFile: string, |
| 56 | givenParser?: Parser, |
| 57 | ) { |
| 58 | const ast = await getAst(currentFile, await fs.readFile(currentFile, "utf8")); |
| 59 | if (!ast) { |
| 60 | throw new Error(`failed to get ast for file ${currentFile}`); |
| 61 | } |
| 62 | let language; |
| 63 | if (givenParser) { |
| 64 | language = givenParser.getLanguage(); |
| 65 | } else { |
| 66 | language = getFullLanguageName(currentFile); |
| 67 | } |
| 68 | |
| 69 | const query = await getQueryForFile( |
| 70 | currentFile, |
| 71 | `static-context-queries/relevant-headers-queries/${language}-get-toplevel-headers.scm`, |
| 72 | ); |
| 73 | if (!query) { |
| 74 | throw new Error( |
| 75 | `failed to get query for file ${currentFile} and language ${language}`, |
| 76 | ); |
| 77 | } |
| 78 | return query.matches(ast.rootNode); |
| 79 | } |
| 80 | |
| 81 | export async function extractTopLevelDeclsWithFormatting( |
| 82 | currentFile: string, |
| 83 | givenParser?: Parser, |
| 84 | ) { |
| 85 | const ast = await getAst(currentFile, await fs.readFile(currentFile, "utf8")); |
| 86 | if (!ast) { |
| 87 | throw new Error(`failed to get ast for file ${currentFile}`); |
| 88 | } |
| 89 | let language; |
| 90 | if (givenParser) { |
| 91 | language = givenParser.getLanguage(); |
| 92 | } else { |
| 93 | language = getFullLanguageName(currentFile); |
| 94 | } |
| 95 | |
| 96 | const query = await getQueryForFile( |
| 97 | currentFile, |
| 98 | `static-context-queries/relevant-headers-queries/${language}-get-toplevel-headers.scm`, |
| 99 | ); |
| 100 | if (!query) { |
| 101 | throw new Error( |
| 102 | `failed to get query for file ${currentFile} and language ${language}`, |
| 103 | ); |
| 104 | } |
| 105 | const matches = query.matches(ast.rootNode); |
| 106 | |
| 107 | const results = []; |
| 108 | |
| 109 | for (const match of matches) { |
| 110 | const item: { |
| 111 | declaration: string; |
| 112 | nodeType: string; |
| 113 | name: string; |
| 114 | declaredType: string; |
| 115 | returnType?: string; |
| 116 | } = { |
| 117 | declaration: "", |
| 118 | nodeType: "", |
| 119 | name: "", |
| 120 | declaredType: "", |
| 121 | }; |
| 122 | |
| 123 | for (const { name, node } of match.captures) { |
| 124 | if (name === "top.var.decl") { |
| 125 | item.nodeType = "variable"; |
| 126 | item.declaration = node.text; |
| 127 | |
| 128 | // Attempt to get the declared type (e.g., const x: string = ...) |
| 129 | const typeNode = node.descendantsOfType("type_annotation")[0]; |
| 130 | if (typeNode) { |
| 131 | item.declaredType = typeNode.text.replace(/^:\s*/, ""); |
| 132 | } |
| 133 | } else if (name === "top.var.name" || name === "top.fn.name") { |
| 134 | item.name = node.text; |
| 135 | } else if (name === "top.fn.decl") { |
| 136 | item.nodeType = "function"; |
| 137 | item.declaration = node.text; |
| 138 | |
| 139 | // Get the return type (e.g., function foo(): string) |
| 140 | const returnTypeNode = node.childForFieldName("return_type"); |
| 141 | if (returnTypeNode) { |
| 142 | item.returnType = returnTypeNode.text.replace(/^:\s*/, ""); |
| 143 | } |
| 144 | |
| 145 | // Get declaredType if needed (TypeScript style) |
| 146 | const nameNode = node.childForFieldName("name"); |
| 147 | if (nameNode && nameNode.nextSibling?.type === "type_annotation") { |
| 148 | item.declaredType = nameNode.nextSibling.text.replace(/^:\s*/, ""); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if (item.name && item.declaration) { |
| 154 | results.push(item); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return results; |
| 159 | } |
| 160 | |
| 161 | export function extractFunctionTypeFromDecl(match: Parser.QueryMatch): string { |
| 162 | let paramsNode: Parser.SyntaxNode | undefined = undefined; |
| 163 | let returnNode: Parser.SyntaxNode | undefined = undefined; |
| 164 | |
| 165 | for (const capture of match.captures) { |
| 166 | if (capture.name === "top.fn.param.type") { |
| 167 | paramsNode = capture.node; |
| 168 | } else if (capture.name === "top.fn.type") { |
| 169 | returnNode = capture.node; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (!paramsNode) { |
| 174 | console.error( |
| 175 | `extractFunctionTypeFromDecl: paramsNode ${paramsNode} not found`, |
| 176 | ); |
| 177 | throw new Error( |
| 178 | `extractFunctionTypeFromDecl: paramsNode ${paramsNode} not found`, |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | if (!returnNode) { |
| 183 | console.error( |
| 184 | `extractFunctionTypeFromDecl: returnNode ${returnNode} not found`, |
| 185 | ); |
| 186 | throw new Error( |
| 187 | `extractFunctionTypeFromDecl: returnNode ${returnNode} not found`, |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | return `(${paramsNode!.text}) => ${returnNode!.text}`; |
| 192 | } |
| 193 | |
| 194 | export function unwrapToBaseType(node: Parser.SyntaxNode): Parser.SyntaxNode { |
| 195 | if ( |
| 196 | [ |
| 197 | "function_type", |
| 198 | "tuple_type", |
| 199 | "type_identifier", |
| 200 | "predefined_type", |
| 201 | ].includes(node.type) |
| 202 | ) { |
| 203 | return node; |
| 204 | } |
| 205 | |
| 206 | for (const child of node.namedChildren) { |
| 207 | const unwrapped = unwrapToBaseType(child!); |
| 208 | if ( |
| 209 | unwrapped !== child || |
| 210 | [ |
| 211 | "function_type", |
| 212 | "tuple_type", |
| 213 | "type_identifier", |
| 214 | "predefined_type", |
| 215 | ].includes(unwrapped.type) |
| 216 | ) { |
| 217 | return unwrapped; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return node; |
| 222 | } |