-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbols_treesitter.go
More file actions
277 lines (248 loc) · 8.73 KB
/
Copy pathsymbols_treesitter.go
File metadata and controls
277 lines (248 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package contexting
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
ts "github.com/odvcencio/gotreesitter"
"github.com/odvcencio/gotreesitter/grammars"
)
// SymbolsExtractorMode controls which extraction engine is used.
// "auto": tree-sitter first, fall back to regex on any error.
// "treesitter": tree-sitter only, propagate errors.
// "regex": current behavior, regex only.
// Default is "auto".
var SymbolsExtractorMode = "auto"
// Query strings empirically validated against gotreesitter v0.23.1.
// Keep these aligned with the spike in /tmp/ts-spike.
const (
pythonQuery = `
(function_definition name: (identifier) @name)
(class_definition name: (identifier) @name)
`
javascriptQuery = `
(function_declaration name: (identifier) @name)
(class_declaration name: (identifier) @name)
(method_definition name: (property_identifier) @name)
(variable_declarator name: (identifier) @name value: (arrow_function))
`
typescriptQuery = `
(function_declaration name: (identifier) @name)
(method_definition name: (property_identifier) @name)
(class_declaration name: (type_identifier) @name)
(interface_declaration name: (type_identifier) @name)
(type_alias_declaration name: (type_identifier) @name)
(enum_declaration name: (identifier) @name)
(variable_declarator name: (identifier) @name value: (arrow_function))
`
rustQuery = `
(function_item name: (identifier) @name)
(struct_item name: (type_identifier) @name)
(enum_item name: (type_identifier) @name)
(trait_item name: (type_identifier) @name)
(impl_item type: (type_identifier) @name)
`
// importQuery captures the string_fragment (no quotes) from every
// import_statement's `source` field. Works for ESM imports like
// import { x } from "@clerk/nextjs"
// and `import type` lines.
// Empirically validated against gotreesitter v0.23.1's JS and TS grammars.
importQuery = `
(import_statement source: (string (string_fragment) @import))
`
)
// extractSymbolsTreeSitter extracts symbols using tree-sitter grammars.
// Dispatches to a per-language extractor based on file extension.
// Returns an error for unsupported extensions or parse failures so the
// caller (the dispatcher in symbols.go) can fall back to regex.
func extractSymbolsTreeSitter(path string) ([]string, error) {
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".py":
return extractWithQueryForLanguage(path, grammars.PythonLanguage(), pythonQuery)
case ".js", ".mjs", ".jsx":
return extractWithQueryForLanguage(path, grammars.JavascriptLanguage(), javascriptQuery)
case ".ts", ".tsx":
return extractWithQueryForLanguage(path, grammars.TypescriptLanguage(), typescriptQuery)
case ".rs":
return extractWithQueryForLanguage(path, grammars.RustLanguage(), rustQuery)
case ".svelte":
return extractSvelteSymbolsTreeSitter(path)
case ".astro":
return extractAstroSymbolsTreeSitter(path)
default:
return nil, fmt.Errorf("unsupported extension for tree-sitter: %s", ext)
}
}
// extractWithQueryForLanguage is a convenience that reads the file and runs the query.
func extractWithQueryForLanguage(path string, lg *ts.Language, queryStr string) ([]string, error) {
src, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
return extractWithQuery(src, lg, queryStr)
}
// extractWithQuery parses src with the given language and runs the query,
// collecting text from any capture bound to @name. The result is deduplicated
// but otherwise returned in match order.
func extractWithQuery(src []byte, lg *ts.Language, queryStr string) ([]string, error) {
parser := ts.NewParser(lg)
tree, err := parser.Parse(src)
if err != nil {
return nil, fmt.Errorf("parse: %w", err)
}
root := tree.RootNode()
q, err := ts.NewQuery(queryStr, lg)
if err != nil {
return nil, fmt.Errorf("compile query: %w", err)
}
cursor := q.Exec(root, lg, src)
seen := make(map[string]struct{})
var symbols []string
for {
match, ok := cursor.NextMatch()
if !ok {
break
}
for _, cap := range match.Captures {
name := cap.Node.Text(src)
if name == "" {
continue
}
if _, dup := seen[name]; dup {
continue
}
seen[name] = struct{}{}
symbols = append(symbols, name)
}
}
return symbols, nil
}
// extractSvelteSymbolsTreeSitter extracts the inner contents of <script> blocks
// from a Svelte file, then parses them with the JavaScript or TypeScript
// tree-sitter grammar (chosen by lang="ts" on the opening tag).
//
// We don't query the Svelte grammar directly because we want per-declaration
// names, and Svelte's grammar is HTML-shaped — extracting <script> contents
// and reusing the JS/TS queries gives consistent results with the regex path.
func extractSvelteSymbolsTreeSitter(path string) ([]string, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
scriptContent, openingTag, found := extractScriptBlock(string(content), `<script[^>]*>`, `</script>`)
if !found {
return nil, nil
}
isTS := strings.Contains(openingTag, "lang=\"ts\"") || strings.Contains(openingTag, "lang='ts'")
if isTS {
return extractWithQuery([]byte(scriptContent), grammars.TypescriptLanguage(), typescriptQuery)
}
return extractWithQuery([]byte(scriptContent), grammars.JavascriptLanguage(), javascriptQuery)
}
// extractAstroSymbolsTreeSitter extracts the frontmatter (between the leading
// --- delimiters at the top of the file) and parses it with the TypeScript
// grammar. Astro frontmatter is always TS-flavored at the syntax level.
func extractAstroSymbolsTreeSitter(path string) ([]string, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
frontmatter, found := extractAstroFrontmatter(string(content))
if !found {
return nil, nil
}
return extractWithQuery([]byte(frontmatter), grammars.TypescriptLanguage(), typescriptQuery)
}
// extractFileImports reads a JS/TS/Svelte source file and returns its
// ESM import paths (e.g. "@clerk/nextjs", "next/server", "./db") in
// the order they appear, deduplicated.
//
// Supports: .ts, .tsx, .js, .jsx, .mjs, .svelte
// Returns nil for other extensions, for files that don't exist, or
// for parse failures. Callers should treat nil as "no signal" — it
// is never an error.
func extractFileImports(path string) []string {
src, err := os.ReadFile(path)
if err != nil {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
switch ext {
case ".ts", ".tsx":
return extractImportsFromSource(src, grammars.TypescriptLanguage())
case ".js", ".jsx", ".mjs":
return extractImportsFromSource(src, grammars.JavascriptLanguage())
case ".svelte":
// Svelte: extract <script> blocks first, then parse with JS or TS
// based on the `lang` attribute (mirrors extractSvelteSymbolsTreeSitter).
scriptContent, openingTag, found := extractScriptBlock(string(src), `<script[^>]*>`, `</script>`)
if !found {
return nil
}
isTS := strings.Contains(openingTag, "lang=\"ts\"") || strings.Contains(openingTag, "lang='ts'")
if isTS {
return extractImportsFromSource([]byte(scriptContent), grammars.TypescriptLanguage())
}
return extractImportsFromSource([]byte(scriptContent), grammars.JavascriptLanguage())
default:
return nil
}
}
// extractImportsFromSource parses src with the given language and returns
// the import paths captured by importQuery. Returns nil on any failure
// (parse error, query compile error, no imports). Output is deduplicated
// but otherwise in match order.
func extractImportsFromSource(src []byte, lg *ts.Language) []string {
parser := ts.NewParser(lg)
tree, err := parser.Parse(src)
if err != nil {
return nil
}
q, err := ts.NewQuery(importQuery, lg)
if err != nil {
return nil
}
cursor := q.Exec(tree.RootNode(), lg, src)
seen := make(map[string]struct{})
var imports []string
for {
match, ok := cursor.NextMatch()
if !ok {
break
}
for _, cap := range match.Captures {
name := cap.Node.Text(src)
if name == "" {
continue
}
if _, dup := seen[name]; dup {
continue
}
seen[name] = struct{}{}
imports = append(imports, name)
}
}
return imports
}
// extractAstroFrontmatter returns the content between the first two --- lines
// at the top of an Astro file, and whether such a block was found.
func extractAstroFrontmatter(content string) (string, bool) {
dashRe := regexp.MustCompile(`(?m)^---\s*$`)
matches := dashRe.FindAllStringIndex(content, -1)
if len(matches) < 2 {
return "", false
}
// The opening --- must be at the very top of the file (only whitespace before it).
firstStart := matches[0][0]
if firstStart > 0 {
before := strings.TrimSpace(content[:firstStart])
if before != "" {
return "", false
}
}
start := matches[0][1]
end := matches[1][0]
return strings.TrimSpace(content[start:end]), true
}