Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/node/treeSitterQueries.ts
13401 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { SyntaxNode } from 'web-tree-sitter';
7
import { WASMLanguage } from './treeSitterLanguages';
8
9
/**
10
* IF YOU WANT LINTING FOR YOUR TREE-SITTER QUERIES:
11
*
12
* Annotating a query template string with `treeSitterQuery.<WASMLanguage>` (see example below)
13
* will allow the vscode-tree-sitter-query extension to provide linting (diagnostics) for this query.
14
*
15
* @example
16
* ```ts
17
* treeSitterQuery.typescript`(function_declaration) @function`
18
* ```
19
*
20
* @remark don't forget to install the `vscode-tree-sitter-query` extension (and activate it, e.g., by opening a .scm file)
21
*/
22
const treeSitterQuery =
23
(() => {
24
/** default template string behavior */
25
function defaultBehavior(query: TemplateStringsArray, ...values: any[]) {
26
return query.length === 1 // no interpolations
27
? query[0]
28
: query.reduce((result, string, i) => `${result}${string}${values[i] || ''}`, '');
29
}
30
return {
31
typescript: defaultBehavior,
32
javascript: defaultBehavior,
33
python: defaultBehavior,
34
go: defaultBehavior,
35
ruby: defaultBehavior,
36
csharp: defaultBehavior,
37
cpp: defaultBehavior,
38
java: defaultBehavior,
39
rust: defaultBehavior,
40
};
41
})();
42
43
44
function forLanguages<Lang extends WASMLanguage, T>(languages: readonly Lang[], query: T) {
45
return Object.fromEntries(languages.map(language => [language, query])) as { [k in Lang]: T };
46
}
47
48
type LanguageQueryMap = { [wasmLanguage in WASMLanguage]: string[]; };
49
50
export const allKnownQueries: LanguageQueryMap = {
51
[WASMLanguage.JavaScript]: [],
52
[WASMLanguage.TypeScript]: [],
53
[WASMLanguage.TypeScriptTsx]: [],
54
[WASMLanguage.Python]: [],
55
[WASMLanguage.Csharp]: [],
56
[WASMLanguage.Go]: [],
57
[WASMLanguage.Java]: [],
58
[WASMLanguage.Ruby]: [],
59
[WASMLanguage.Cpp]: [],
60
[WASMLanguage.Rust]: [],
61
};
62
/**
63
* register queries
64
*/
65
function q<T extends Partial<LanguageQueryMap>>(queryMap: T): T {
66
for (const key in queryMap) {
67
const queries = queryMap[key as WASMLanguage]!;
68
allKnownQueries[key as WASMLanguage].push(...queries);
69
}
70
return queryMap;
71
}
72
73
export const callExpressionQuery: LanguageQueryMap = q({
74
...forLanguages([WASMLanguage.JavaScript, WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
75
`[
76
(call_expression
77
function: (identifier) @identifier)
78
(call_expression
79
function: (member_expression
80
(property_identifier) @identifier))
81
] @call_expression`
82
]),
83
[WASMLanguage.Python]: [
84
`[
85
(call
86
function: (identifier) @identifier)
87
(call
88
function: (attribute
89
attribute: (identifier) @identifier))
90
] @call_expression`
91
],
92
[WASMLanguage.Csharp]: [
93
`[
94
(invocation_expression
95
function: (identifier) @identifier)
96
(invocation_expression
97
function: (member_access_expression
98
name: (identifier) @identifier))
99
] @call_expression`
100
],
101
[WASMLanguage.Go]: [
102
`[
103
(call_expression
104
((selector_expression
105
(field_identifier) @identifier)))
106
(call_expression
107
(identifier) @identifier)
108
] @call_expression`
109
],
110
[WASMLanguage.Java]: [
111
`[
112
(method_invocation
113
name: (identifier) @identifier)
114
] @call_expression`
115
],
116
[WASMLanguage.Ruby]: [
117
/**
118
* TODO@joyceerhl figure out how to support matching
119
* direct method calls i.e.
120
* ```
121
* def say_hello
122
* puts "Hello, world!"
123
* end
124
* say_hello
125
* ```
126
* which is matchable only by the `identifier` syntax node
127
* and could have performance implications
128
*/
129
`[
130
(call (identifier) @identifier
131
(#not-match? @identifier "new|send|public_send|method"))
132
(call
133
receiver: (identifier)
134
method: (identifier) @method
135
(#match? @method "^(send|public_send|method)")
136
arguments: (argument_list
137
(simple_symbol) @symbol))
138
] @call_expression`
139
],
140
[WASMLanguage.Cpp]: [
141
`[
142
(function_declarator
143
(identifier) @identifier)
144
(function_declarator
145
(field_identifier) @identifier)
146
(call_expression (identifier) @identifier)
147
(call_expression
148
(field_expression
149
field: (field_identifier) @identifier))
150
(call_expression
151
(call_expression
152
(primitive_type)
153
(argument_list
154
(pointer_expression
155
(identifier) @identifier))))
156
] @call_expression`
157
],
158
[WASMLanguage.Rust]: [
159
`[
160
(call_expression (identifier) @identifier)
161
(call_expression (field_expression (identifier) (field_identifier) @identifier))
162
(call_expression (scoped_identifier (identifier) (identifier) @identifier (#not-match? @identifier "new")))
163
] @call_expression`
164
]
165
});
166
167
export const classDeclarationQuery: LanguageQueryMap = q({
168
...forLanguages([WASMLanguage.JavaScript, WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
169
`(class_declaration) @class_declaration`
170
]),
171
[WASMLanguage.Java]: [
172
`(class_declaration) @class_declaration`
173
],
174
[WASMLanguage.Csharp]: [
175
`(class_declaration) @class_declaration`
176
],
177
[WASMLanguage.Python]: [
178
`(class_definition) @class_declaration`
179
],
180
[WASMLanguage.Cpp]: [
181
`(class_specifier) @class_declaration`
182
],
183
[WASMLanguage.Ruby]: [
184
`(class) @class_declaration`
185
],
186
[WASMLanguage.Go]: [
187
`(type_declaration
188
(type_spec
189
(type_identifier) @type_identifier)) @class_declaration`
190
],
191
[WASMLanguage.Rust]: [
192
`(impl_item (type_identifier) @type_identifier) @class_declaration`
193
]
194
});
195
196
export const typeDeclarationQuery: { [language: string]: string[] } = q({
197
// No types in JavaScript
198
[WASMLanguage.TypeScript]: [
199
`[
200
(interface_declaration)
201
(type_alias_declaration)
202
] @type_declaration`
203
],
204
[WASMLanguage.Csharp]: [
205
`(interface_declaration
206
(identifier) @type_identifier) @type_declaration`
207
],
208
[WASMLanguage.Cpp]: [
209
`[
210
(struct_specifier
211
(type_identifier) @type_identifier)
212
(union_specifier
213
(type_identifier) @type_identifier)
214
(enum_specifier
215
(type_identifier) @type_identifier)
216
] @type_declaration`
217
],
218
[WASMLanguage.Java]: [
219
`(interface_declaration
220
(identifier) @type_identifier) @type_declaration`
221
],
222
[WASMLanguage.Go]: [
223
`(type_declaration
224
(type_spec
225
(type_identifier) @type_identifier)) @type_declaration`
226
],
227
[WASMLanguage.Ruby]: [
228
`((constant) @type_identifier) @type_declaration`
229
],
230
[WASMLanguage.Python]: [
231
`(class_definition
232
(identifier) @type_identifier) @type_declaration`
233
],
234
});
235
236
export const typeReferenceQuery: { [language: string]: string[] } = q({
237
// No types in JavaScript
238
[WASMLanguage.TypeScript]: [
239
`(type_identifier) @type_identifier`
240
],
241
[WASMLanguage.Go]: [
242
`(type_identifier) @type_identifier`
243
],
244
[WASMLanguage.Ruby]: [
245
`(constant) @type_identifier`
246
],
247
[WASMLanguage.Csharp]: [
248
`[
249
(base_list
250
(identifier) @type_identifier)
251
(variable_declaration
252
(identifier) @type_identifier)
253
]`
254
],
255
[WASMLanguage.Cpp]: [
256
`(type_identifier) @type_identifier`
257
],
258
[WASMLanguage.Java]: [
259
`(type_identifier) @type_identifier`
260
],
261
[WASMLanguage.Python]: [
262
`[
263
(type (identifier) @type_identifier)
264
(argument_list
265
(identifier) @type_identifier)
266
]`
267
]
268
});
269
270
export const classReferenceQuery: LanguageQueryMap = q({
271
...forLanguages([WASMLanguage.JavaScript, WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
272
`(new_expression
273
constructor: (identifier) @new_expression)`
274
]),
275
[WASMLanguage.Python]: [
276
`(call
277
function: (identifier) @new_expression)`
278
],
279
[WASMLanguage.Csharp]: [
280
`(object_creation_expression
281
(identifier) @new_expression)`
282
],
283
[WASMLanguage.Java]: [
284
`(object_creation_expression
285
(type_identifier) @new_expression)`
286
],
287
[WASMLanguage.Cpp]: [
288
`[
289
(declaration
290
(type_identifier) @new_expression)
291
(class_specifier
292
(type_identifier) @new_expression)
293
]`
294
],
295
[WASMLanguage.Go]: [
296
`(composite_literal (type_identifier) @new_expression)`
297
],
298
[WASMLanguage.Ruby]: [
299
`((call
300
receiver: ((constant) @new_expression)
301
method: (identifier) @method)
302
(#eq? @method "new"))`
303
],
304
[WASMLanguage.Rust]: [
305
`(call_expression
306
(scoped_identifier
307
(identifier) @new_expression
308
(identifier) @identifier
309
(#eq? @identifier "new")))`
310
],
311
});
312
313
export const functionQuery: LanguageQueryMap = q({
314
python: [
315
// `(function_definition)` is defined in python grammar:
316
// https://github.com/tree-sitter/tree-sitter-python/blob/c4282ba411d990d313c5f8e7850bcaaf46fbf7da/grammar.js#L325-L338
317
// docstring is represented in grammar as an optional `(initial expression_statement (string))`
318
// at the start of the body block
319
`[
320
(function_definition
321
name: (identifier) @identifier
322
body: (block
323
(expression_statement (string))? @docstring) @body)
324
(assignment
325
left: (identifier) @identifier
326
right: (lambda) @body)
327
] @function`,
328
329
// handle malformed defs - no trailing semicolon or no body
330
`(ERROR ("def" (identifier) (parameters))) @function`,
331
],
332
...forLanguages([WASMLanguage.JavaScript, WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
333
// function patterns defined in javascript grammar which is shared by ts
334
// https://github.com/tree-sitter/tree-sitter-javascript/blob/3d9fe9786ee74fa5067577f138e1a7129f80fb41/grammar.js#L595-L629
335
// include `arrow_function` as well
336
`[
337
(function_expression
338
name: (identifier)? @identifier
339
body: (statement_block) @body)
340
(function_declaration
341
name: (identifier)? @identifier
342
body: (statement_block) @body)
343
(generator_function
344
name: (identifier)? @identifier
345
body: (statement_block) @body)
346
(generator_function_declaration
347
name: (identifier)? @identifier
348
body: (statement_block) @body)
349
(method_definition
350
name: (property_identifier)? @identifier
351
body: (statement_block) @body)
352
(arrow_function
353
body: (statement_block) @body)
354
] @function`,
355
]),
356
go: [
357
// function patterns defined in go grammar:
358
// https://github.com/tree-sitter/tree-sitter-go/blob/b0c78230146705e867034e49a5ece20245b33490/grammar.js#L194-L209
359
`[
360
(function_declaration
361
name: (identifier) @identifier
362
body: (block) @body)
363
(method_declaration
364
name: (field_identifier) @identifier
365
body: (block) @body)
366
] @function`,
367
],
368
ruby: [
369
// function patterns defined in ruby grammar:
370
// https://github.com/tree-sitter/tree-sitter-ruby/blob/master/grammar.js
371
// NOTE: Use a @params label for optional parameters to avoid capturing as
372
// part of @body if parameters are present.
373
`[
374
(method
375
name: (_) @identifier
376
parameters: (method_parameters)? @params
377
[(_)+ "end"] @body)
378
(singleton_method
379
name: (_) @identifier
380
parameters: (method_parameters)? @params
381
[(_)+ "end"] @body)
382
] @function`,
383
],
384
csharp: [
385
// function patterns defined in csharp grammar:
386
// https://github.com/tree-sitter/tree-sitter-c-sharp/blob/master/grammar.js
387
`[
388
(constructor_declaration
389
(identifier) @identifier
390
(block) @body)
391
(destructor_declaration
392
(identifier) @identifier
393
(block) @body)
394
(operator_declaration
395
(block) @body)
396
(method_declaration
397
(identifier) @identifier
398
(block) @body)
399
(local_function_statement
400
(identifier) @identifier
401
(block) @body)
402
] @function`,
403
],
404
cpp: [
405
// function patterns defined in cpp grammar:
406
// https://github.com/tree-sitter/tree-sitter-cpp/blob/master/grammar.js
407
`[
408
(function_definition
409
(_
410
(identifier) @identifier)
411
(compound_statement) @body)
412
(function_definition
413
(function_declarator
414
(qualified_identifier
415
(identifier) @identifier))
416
(compound_statement) @body)
417
] @function`,
418
],
419
java: [
420
`[
421
(constructor_declaration
422
name: (identifier) @identifier
423
body: (constructor_body) @body)
424
(method_declaration
425
name: (_) @identifier
426
body: (block) @body)
427
(lambda_expression
428
body: (block) @body)
429
] @function`
430
],
431
rust: [
432
`[
433
(function_item (identifier) @identifier)
434
(let_declaration (identifier) @identifier)
435
] @function`
436
]
437
});
438
439
export const docCommentQueries: LanguageQueryMap = q({
440
[WASMLanguage.JavaScript]: [
441
treeSitterQuery.javascript`((comment) @comment
442
(#match? @comment "^\\\\/\\\\*\\\\*")) @docComment`
443
],
444
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
445
treeSitterQuery.typescript`((comment) @comment
446
(#match? @comment "^\\\\/\\\\*\\\\*")) @docComment`
447
]),
448
[WASMLanguage.Java]: [
449
treeSitterQuery.java`((block_comment) @block_comment
450
(#match? @block_comment "^\\\\/\\\\*\\\\*")) @docComment`
451
],
452
[WASMLanguage.Cpp]: [
453
treeSitterQuery.cpp`((comment) @comment
454
(#match? @comment "^\\\\/\\\\*\\\\*")) @docComment`
455
],
456
[WASMLanguage.Csharp]: [
457
treeSitterQuery.csharp`(
458
((comment) @c
459
(#match? @c "^\\\\/\\\\/\\\\/"))+
460
) @docComment`
461
],
462
[WASMLanguage.Rust]: [
463
treeSitterQuery.rust`((line_comment) @comment
464
(#match? @comment "^\/\/\/|^\/\/!"))+ @docComment`
465
],
466
// note: golang & ruby have same prefix for a doc comment and line comment
467
[WASMLanguage.Go]: [
468
treeSitterQuery.go`((comment)+) @docComment`
469
],
470
[WASMLanguage.Ruby]: [
471
treeSitterQuery.ruby`((comment)+) @docComment`
472
],
473
474
// NOT yet supported:
475
476
// we don't support python with this yet because of its placement of a docstring (under signature)
477
[WASMLanguage.Python]: [
478
`(expression_statement
479
(string) @docComment)`
480
],
481
});
482
483
export const testableNodeQueries: LanguageQueryMap = q({
484
[WASMLanguage.JavaScript]: [
485
treeSitterQuery.javascript`[
486
(function_declaration
487
(identifier) @function.identifier
488
) @function
489
490
(generator_function_declaration
491
name: (identifier) @generator_function.identifier
492
) @generator_function
493
494
(class_declaration
495
name: (identifier) @class.identifier ;; note: (type_identifier) in typescript
496
body: (class_body
497
(method_definition
498
name: (property_identifier) @method.identifier
499
) @method
500
)
501
) @class
502
]`
503
],
504
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx],
505
[treeSitterQuery.typescript`[
506
(function_declaration
507
(identifier) @function.identifier
508
) @function
509
510
(generator_function_declaration
511
name: (identifier) @generator_function.identifier
512
) @generator_function
513
514
(class_declaration
515
name: (type_identifier) @class.identifier
516
body: (class_body
517
(method_definition
518
(accessibility_modifier)? @method.accessibility_modifier
519
name: (property_identifier) @method.identifier
520
(#not-eq? @method.accessibility_modifier "private")
521
) @method
522
)
523
) @class
524
]`]
525
),
526
[WASMLanguage.Python]: [
527
treeSitterQuery.python`[
528
(function_definition
529
name: (identifier) @function.identifier
530
) @function
531
]`
532
],
533
[WASMLanguage.Go]: [
534
treeSitterQuery.go`[
535
(function_declaration
536
name: (identifier) @function.identifier
537
) @function
538
539
(method_declaration
540
name: (field_identifier) @method.identifier
541
) @method
542
]`
543
],
544
[WASMLanguage.Ruby]: [
545
treeSitterQuery.ruby`[
546
(method
547
name: (identifier) @method.identifier
548
) @method
549
550
(singleton_method
551
name: (_) @singleton_method.identifier
552
) @singleton_method
553
]`
554
],
555
[WASMLanguage.Csharp]: [
556
treeSitterQuery.csharp`[
557
(constructor_declaration
558
(identifier) @constructor.identifier
559
) @constructor
560
561
(destructor_declaration
562
(identifier) @destructor.identifier
563
) @destructor
564
565
(method_declaration
566
(identifier) @method.identifier
567
) @method
568
569
(local_function_statement
570
(identifier) @local_function.identifier
571
) @local_function
572
]`
573
],
574
[WASMLanguage.Cpp]: [ // FIXME@ulugbekna: #7769 enrich with class/methods
575
treeSitterQuery.cpp`[
576
(function_definition
577
(_
578
(identifier) @identifier)
579
) @function
580
]`
581
],
582
[WASMLanguage.Java]: [
583
treeSitterQuery.java`(class_declaration
584
name: (_) @class.identifier
585
body: (_
586
[
587
(constructor_declaration
588
(modifiers)? @constructor.modifiers
589
(#not-eq? @constructor.modifiers "private")
590
name: (identifier) @constructor.identifier
591
) @constructor
592
593
(method_declaration
594
(modifiers)? @method.modifiers
595
(#not-eq? @method.modifiers "private")
596
name: (identifier) @method.identifier
597
) @method
598
]
599
)
600
) @class`
601
],
602
[WASMLanguage.Rust]: [
603
treeSitterQuery.rust`[
604
(function_item
605
(identifier) @function.identifier
606
) @function
607
]`
608
]
609
});
610
611
export const symbolQueries: LanguageQueryMap = q({
612
[WASMLanguage.JavaScript]: [
613
treeSitterQuery.javascript`[
614
(identifier) @symbol
615
(property_identifier) @symbol
616
(private_property_identifier) @symbol
617
]`
618
],
619
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
620
treeSitterQuery.typescript`[
621
(identifier) @symbol
622
(type_identifier) @symbol
623
(property_identifier) @symbol
624
(private_property_identifier) @symbol
625
]`
626
]),
627
[WASMLanguage.Cpp]: [
628
treeSitterQuery.cpp`[
629
(identifier) @symbol
630
(type_identifier) @symbol
631
]`
632
],
633
[WASMLanguage.Csharp]: [
634
treeSitterQuery.csharp`[
635
(identifier) @symbol
636
]`
637
],
638
[WASMLanguage.Go]: [
639
treeSitterQuery.go`[
640
(identifier) @symbol
641
]`
642
],
643
[WASMLanguage.Java]: [
644
treeSitterQuery.java`[
645
(identifier) @symbol
646
]`
647
],
648
[WASMLanguage.Python]: [
649
treeSitterQuery.python`[
650
(identifier) @symbol
651
]`
652
],
653
[WASMLanguage.Ruby]: [
654
treeSitterQuery.ruby`[
655
(identifier) @symbol
656
]`
657
],
658
[WASMLanguage.Rust]: [
659
treeSitterQuery.rust`[
660
(identifier) @symbol
661
]`
662
],
663
});
664
665
export const syntacticallyValidAtoms: LanguageQueryMap = q({
666
[WASMLanguage.TypeScript]: [
667
treeSitterQuery.typescript`
668
[
669
(comment) @comment ;; split into multiple comment kinds?
670
671
(declaration) @declaration
672
673
;; class declaration related
674
(public_field_definition) @public_field_definition
675
(method_definition) @method_definition
676
(class_declaration (_ (method_signature) @method_signature))
677
(abstract_method_signature) @abstract_method_signature
678
679
;; enum declaration related
680
(enum_assignment) @enum_assignment
681
682
;; interface declaration related
683
(interface_declaration (_ (method_signature) @method_signature))
684
(interface_declaration (_ (property_signature) @property_signature))
685
686
;; statements
687
688
(import_statement) @import_statement
689
(export_statement) @export_statement
690
691
(expression_statement) @expression_statement
692
693
(for_in_statement) @for_in_statement
694
;; exclude any children found in the for loop condition
695
(for_statement condition: (_) @for_statement.exclude_captures ) @for_statement
696
(break_statement) @break_statement
697
(continue_statement) @continue_statement
698
(do_statement) @do_statement
699
(if_statement) @if_statement
700
(if_statement
701
consequence: [
702
(expression_statement)
703
(if_statement)
704
] @if_statement.exclude_captures)
705
(else_clause
706
[
707
(expression_statement)
708
(if_statement) ; for if-else chains
709
] @else_clause.exclude_captures)
710
(switch_statement) @switch_statement
711
(switch_case) @switch_case
712
(try_statement) @try_statement
713
(throw_statement) @throw_statement
714
(debugger_statement) @debugger_statement
715
(return_statement) @return_statement
716
]
717
`
718
],
719
[WASMLanguage.TypeScriptTsx]: [
720
treeSitterQuery.typescript`
721
[
722
(comment) @comment ;; split into multiple comment kinds?
723
724
(declaration) @declaration
725
726
;; class declaration related
727
(public_field_definition) @public_field_definition
728
(method_definition) @method_definition
729
(class_declaration (_ (method_signature) @method_signature))
730
(abstract_method_signature) @abstract_method_signature
731
732
;; enum declaration related
733
(enum_assignment) @enum_assignment
734
735
;; interface declaration related
736
(interface_declaration (_ (method_signature) @method_signature))
737
(interface_declaration (_ (property_signature) @property_signature))
738
739
;; statements
740
741
(import_statement) @import_statement
742
(export_statement) @export_statement
743
744
(expression_statement) @expression_statement
745
746
(for_in_statement) @for_in_statement
747
;; exclude any children found in the for loop condition
748
(for_statement condition: (_) @for_statement.exclude_captures ) @for_statement
749
(break_statement) @break_statement
750
(continue_statement) @continue_statement
751
(do_statement) @do_statement
752
(if_statement) @if_statement
753
(if_statement
754
consequence: [
755
(expression_statement)
756
(if_statement)
757
] @if_statement.exclude_captures)
758
(else_clause
759
[
760
(expression_statement)
761
(if_statement) ; for if-else chains
762
] @else_clause.exclude_captures)
763
(switch_statement) @switch_statement
764
(switch_case) @switch_case
765
(try_statement) @try_statement
766
(throw_statement) @throw_statement
767
(debugger_statement) @debugger_statement
768
(return_statement) @return_statement
769
770
;; jsx
771
(jsx_element) @jsx_element
772
(jsx_element (_ (jsx_expression) @jsx_expression))
773
]
774
`
775
],
776
[WASMLanguage.Python]: [
777
treeSitterQuery.python`
778
[
779
(comment) @comment
780
781
;; simple statements
782
(assert_statement) @assert_statement
783
(break_statement) @break_statement
784
(continue_statement) @continue_statement
785
(delete_statement) @delete_statement
786
(exec_statement) @exec_statement
787
(expression_statement) @expression_statement
788
(future_import_statement) @future_import_statement
789
(global_statement) @global_statement
790
(import_from_statement) @import_from_statement
791
(import_statement) @import_statement
792
(nonlocal_statement) @nonlocal_statement
793
(pass_statement) @pass_statement
794
(print_statement) @print_statement
795
(raise_statement) @raise_statement
796
(return_statement) @return_statement
797
(type_alias_statement) @type_alias_statement
798
799
800
;; compound statements
801
802
(class_definition) @class_definition
803
(decorated_definition) @decorated_definition
804
(for_statement) @for_statement
805
(function_definition) @function_definition
806
(if_statement) @if_statement
807
(try_statement) @try_statement
808
(while_statement) @while_statement
809
(with_statement) @with_statement
810
811
812
;; expressions
813
814
(expression_list) @expression_list
815
(expression_statement) @expression_statement
816
]
817
`
818
],
819
[WASMLanguage.JavaScript]: [
820
treeSitterQuery.javascript`
821
[
822
(comment) @comment ;; split into multiple comment kinds?
823
824
(declaration) @declaration
825
826
;; class declaration related
827
828
(field_definition) @field_definition
829
(method_definition) @method_definition
830
831
;; statements
832
833
(import_statement) @import_statement
834
(export_statement) @export_statement
835
836
(expression_statement) @expression_statement
837
838
(for_in_statement) @for_in_statement
839
;; exclude any children found in the for loop condition
840
(for_statement condition: (_) @for_statement.exclude_captures ) @for_statement
841
(break_statement) @break_statement
842
(continue_statement) @continue_statement
843
(do_statement) @do_statement
844
(if_statement) @if_statement
845
(switch_statement) @switch_statement
846
(switch_case) @switch_case
847
(try_statement) @try_statement
848
(throw_statement) @throw_statement
849
(debugger_statement) @debugger_statement
850
(return_statement) @return_statement
851
]`
852
],
853
[WASMLanguage.Go]: [
854
treeSitterQuery.go`
855
[
856
(_statement) @statement
857
(function_declaration) @function_declaration
858
(import_declaration) @import_declaration
859
(method_declaration) @method_declaration
860
(package_clause) @package_clause
861
862
(if_statement
863
initializer: (_) @for_statement.exclude_captures) @for_statement
864
865
(expression_case) @expression_case ;; e.g., case 0:
866
]
867
`
868
],
869
[WASMLanguage.Ruby]: [
870
treeSitterQuery.ruby`
871
[
872
(comment) @comment
873
874
(assignment) @assignment
875
876
(if) @if
877
878
(call) @call
879
880
(case) @case
881
882
(when) @when
883
884
(while) @while
885
886
(for) @for
887
888
(method) @method
889
890
(class) @class
891
892
(module) @module
893
894
(begin) @begin
895
]
896
`
897
],
898
[WASMLanguage.Csharp]: [
899
treeSitterQuery.csharp`
900
[
901
(comment) @comment
902
903
(class_declaration) @class_declaration
904
(constructor_declaration) @constructor_declaration
905
(method_declaration) @method_declaration
906
(delegate_declaration) @delegate_declaration
907
(enum_declaration) @enum_declaration
908
(extern_alias_directive) @extern_alias_directive
909
(file_scoped_namespace_declaration) @file_scoped_namespace_declaration
910
(global_attribute) @global_attribute
911
(global_statement) @global_statement
912
(interface_declaration) @interface_declaration
913
(namespace_declaration) @namespace_declaration
914
(record_declaration) @record_declaration
915
(struct_declaration) @struct_declaration
916
(using_directive) @using_directive
917
918
(local_declaration_statement) @local_declaration_statement
919
(expression_statement) @expression_statement
920
(for_statement) @for_statement
921
(foreach_statement) @foreach_statement
922
(continue_statement) @continue_statement
923
(break_statement) @break_statement
924
(throw_statement) @throw_statement
925
(return_statement) @return_statement
926
(try_statement) @try_statement
927
]
928
`
929
],
930
[WASMLanguage.Cpp]: [
931
treeSitterQuery.cpp`
932
[
933
(preproc_ifdef) @preproc_ifdef
934
(preproc_call) @preproc_call
935
(preproc_def) @preproc_def
936
(type_definition) @type_definition
937
(type_definition
938
type:(_) @type_definition.exclude_captures) @type_definition
939
940
(declaration) @declaration
941
942
(expression_statement) @expression_statement
943
944
(comment) @comment
945
946
(preproc_include) @preproc_include
947
948
(namespace_definition) @namespace_definition
949
950
(enum_specifier) @enum_specifier
951
952
(struct_specifier) @struct_specifier
953
954
(template_declaration) @template_declaration
955
956
(function_definition) @function_definition
957
958
(return_statement) @return_statement
959
960
(class_specifier) @class_specifier
961
962
(try_statement) @try_statement
963
964
(throw_statement) @throw_statement
965
966
(for_statement) @for_statement
967
(for_statement
968
initializer:(_) @for_statement.exclude_captures) @for_statement
969
970
(for_range_loop) @for_range_loop
971
972
(while_statement) @while_statement
973
(do_statement) @do_statement
974
(if_statement) @if_statement
975
976
(labeled_statement) @labeled_statement
977
(goto_statement) @goto_statement
978
979
(break_statement) @break_statement
980
]
981
`
982
],
983
[WASMLanguage.Java]: [
984
treeSitterQuery.java`
985
[
986
(statement) @statement ;; @ulugbekna: this includes (declaration); but somehow it can't capture inner classes
987
988
(line_comment) @line_comment
989
(block_comment) @block_comment
990
991
(for_statement
992
init: (_) @for_statement.exclude_captures)
993
994
(block) @block.exclude_captures
995
996
(class_declaration) @class_declaration
997
998
(constructor_declaration) @constructor_declaration
999
1000
(field_declaration) @field_declaration
1001
1002
(method_declaration) @method_declaration
1003
]
1004
`
1005
],
1006
[WASMLanguage.Rust]: [
1007
// treeSitterQuery.rust`
1008
// [
1009
// (line_comment) @line_comment
1010
1011
// (let_declaration) @let_declaration
1012
// (extern_crate_declaration) @extern_crate_declaration
1013
// (use_declaration) @use_declaration
1014
1015
// (attribute_item) @attribute_item
1016
// (const_item) @const_item
1017
// (enum_item) @enum_item
1018
// (foreign_mod_item) @foreign_mod_item
1019
// (function_item) @function_item
1020
// (function_signature_item) @function_signature_item
1021
// (impl_item) @impl_item
1022
// (inner_attribute_item) @inner_attribute_item
1023
// (mod_item) @mod_item
1024
// (static_item) @static_item
1025
// (struct_item) @struct_item
1026
// (trait_item) @trait_item
1027
// (type_item) @type_item
1028
// (union_item) @union_item
1029
1030
// (macro_definition) @macro_definition
1031
1032
// (empty_statement) @empty_statement
1033
1034
// (compound_assignment_expr) @compound_assignment_expr
1035
// (generic_function) @generic_function
1036
// (metavariable) @metavariable
1037
1038
// (match_arm) @match_arm
1039
1040
// (async_block) @async_block
1041
// (const_block) @const_block
1042
// (unsafe_block) @unsafe_block
1043
1044
// (block) @block.exclude_captures
1045
// ]
1046
// `
1047
]
1048
});
1049
1050
export const coarseScopeTypes: { [wasmLanguage in WASMLanguage]: string[] } = {
1051
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
1052
'program',
1053
'interface_declaration',
1054
'class_declaration',
1055
'function_declaration',
1056
'function_expression',
1057
'type_alias_declaration',
1058
'method_definition',
1059
]),
1060
[WASMLanguage.JavaScript]: [
1061
'program',
1062
'class_declaration',
1063
'function_declaration',
1064
'function_expression',
1065
'method_definition',
1066
],
1067
[WASMLanguage.Java]: [
1068
'program',
1069
'class_declaration',
1070
'interface_declaration',
1071
'method_declaration',
1072
],
1073
[WASMLanguage.Cpp]: [
1074
'translation_unit',
1075
'class_specifier',
1076
'function_definition',
1077
],
1078
[WASMLanguage.Csharp]: [
1079
'compilation_unit',
1080
'class_declaration',
1081
'interface_declaration',
1082
'method_declaration',
1083
],
1084
[WASMLanguage.Python]: [
1085
'module',
1086
'class_definition',
1087
'function_definition',
1088
],
1089
[WASMLanguage.Go]: [
1090
'source_file',
1091
'type_declaration',
1092
'function_declaration',
1093
'method_declaration',
1094
],
1095
[WASMLanguage.Ruby]: [
1096
'program',
1097
'method',
1098
'class',
1099
'method',
1100
],
1101
[WASMLanguage.Rust]: [
1102
'source_file',
1103
'function_item',
1104
'impl_item',
1105
'let_declaration',
1106
],
1107
};
1108
1109
export const coarseScopesQuery: LanguageQueryMap = q({
1110
[WASMLanguage.TypeScript]: [
1111
coarseScopesQueryForLanguage(WASMLanguage.TypeScript)
1112
],
1113
[WASMLanguage.TypeScriptTsx]: [
1114
coarseScopesQueryForLanguage(WASMLanguage.TypeScriptTsx)
1115
],
1116
[WASMLanguage.JavaScript]: [
1117
coarseScopesQueryForLanguage(WASMLanguage.JavaScript)
1118
],
1119
[WASMLanguage.Java]: [
1120
coarseScopesQueryForLanguage(WASMLanguage.Java)
1121
],
1122
[WASMLanguage.Cpp]: [
1123
coarseScopesQueryForLanguage(WASMLanguage.Cpp)
1124
],
1125
[WASMLanguage.Csharp]: [
1126
coarseScopesQueryForLanguage(WASMLanguage.Csharp)
1127
],
1128
[WASMLanguage.Python]: [
1129
coarseScopesQueryForLanguage(WASMLanguage.Python)
1130
],
1131
[WASMLanguage.Go]: [
1132
coarseScopesQueryForLanguage(WASMLanguage.Go)
1133
],
1134
[WASMLanguage.Ruby]: [
1135
coarseScopesQueryForLanguage(WASMLanguage.Ruby)
1136
],
1137
[WASMLanguage.Rust]: [
1138
coarseScopesQueryForLanguage(WASMLanguage.Rust)
1139
],
1140
});
1141
1142
export const fineScopeTypes: { [wasmLanguage in WASMLanguage]: string[] } = {
1143
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx, WASMLanguage.JavaScript], [
1144
'for_in_statement',
1145
'for_statement',
1146
'if_statement',
1147
'while_statement',
1148
'do_statement',
1149
'try_statement',
1150
'switch_statement'
1151
]),
1152
[WASMLanguage.Java]: [
1153
'for_statement',
1154
'enhanced_for_statement',
1155
'if_statement',
1156
'while_statement',
1157
'do_statement',
1158
'try_statement',
1159
'switch_expression'
1160
],
1161
[WASMLanguage.Cpp]: [
1162
'for_statement',
1163
'for_range_loop',
1164
'if_statement',
1165
'while_statement',
1166
'do_statement',
1167
'try_statement',
1168
'switch_statement'
1169
],
1170
[WASMLanguage.Csharp]: [
1171
'for_statement',
1172
'for_each_statement',
1173
'if_statement',
1174
'while_statement',
1175
'do_statement',
1176
'try_statement',
1177
'switch_expression'
1178
],
1179
[WASMLanguage.Python]: [
1180
'for_statement',
1181
'if_statement',
1182
'while_statement',
1183
'try_statement'
1184
],
1185
[WASMLanguage.Go]: [
1186
'for_statement',
1187
'if_statement',
1188
'type_switch_statement'
1189
],
1190
[WASMLanguage.Ruby]: [
1191
'while',
1192
'for',
1193
'if',
1194
'case'
1195
],
1196
[WASMLanguage.Rust]: [
1197
'for_statement',
1198
'if_statement',
1199
'while_statement',
1200
'loop_statement',
1201
'match_expression',
1202
],
1203
};
1204
1205
export const statementTypes: { [wasmLanguage in WASMLanguage]: string[] } = {
1206
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
1207
'lexical_declaration',
1208
'expression_statement',
1209
'public_field_definition',
1210
]),
1211
[WASMLanguage.JavaScript]: [
1212
'call_expression',
1213
'expression_statement',
1214
'variable_declaration',
1215
'public_field_definition'
1216
],
1217
[WASMLanguage.Java]: [
1218
'expression_statement',
1219
'local_variable_declaration',
1220
'field_declaration'
1221
],
1222
[WASMLanguage.Cpp]: [
1223
'field_declaration',
1224
'expression_statement',
1225
'declaration'
1226
],
1227
[WASMLanguage.Csharp]: [
1228
'field_declaration',
1229
'expression_statement'
1230
],
1231
[WASMLanguage.Python]: [
1232
'expression_statement'
1233
],
1234
[WASMLanguage.Go]: [
1235
'short_var_declaration',
1236
'call_expression'
1237
],
1238
[WASMLanguage.Ruby]: [
1239
'call',
1240
'assignment'
1241
],
1242
[WASMLanguage.Rust]: [
1243
'expression_statement',
1244
'let_declaration',
1245
'use_declaration',
1246
'assignment_expression',
1247
'macro_definition',
1248
'extern_crate_declaration'
1249
],
1250
};
1251
1252
const semanticChunkTargetTypes: { [wasmLanguage in WASMLanguage]: string[] } = {
1253
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
1254
'class_declaration',
1255
'function_declaration',
1256
'generator_function_declaration',
1257
'interface_declaration',
1258
'internal_module',
1259
'method_definition',
1260
'abstract_class_declaration',
1261
'abstract_method_signature',
1262
'enum_declaration'
1263
]),
1264
[WASMLanguage.JavaScript]: [
1265
'class_declaration',
1266
'function_declaration',
1267
'generator_function_declaration',
1268
'method_definition',
1269
],
1270
[WASMLanguage.Java]: [
1271
'class_declaration',
1272
'constructor_declaration',
1273
'enum_declaration',
1274
'interface_declaration',
1275
'method_declaration',
1276
'module_declaration',
1277
],
1278
[WASMLanguage.Cpp]: [
1279
'class_specifier',
1280
'function_definition',
1281
'namespace_definition',
1282
'struct_specifier'
1283
],
1284
[WASMLanguage.Csharp]: [
1285
'class_declaration',
1286
'constructor_declaration',
1287
'destructor_declaration',
1288
'enum_declaration',
1289
'interface_declaration',
1290
'method_declaration',
1291
'namespace_declaration',
1292
'struct_declaration',
1293
],
1294
[WASMLanguage.Python]: [
1295
'function_definition',
1296
'class_definition',
1297
],
1298
[WASMLanguage.Go]: [
1299
'function_declaration',
1300
'method_declaration'
1301
],
1302
[WASMLanguage.Ruby]: [
1303
'class',
1304
'method',
1305
'module'
1306
],
1307
[WASMLanguage.Rust]: [
1308
'function_item',
1309
'impl_item',
1310
'mod_item',
1311
'struct_item',
1312
'trait_item',
1313
'union_item',
1314
],
1315
};
1316
1317
export const semanticChunkingTargetQuery: LanguageQueryMap = q({
1318
[WASMLanguage.TypeScript]: [
1319
semanticChunkingTargetQueryForLanguage(WASMLanguage.TypeScript)
1320
],
1321
[WASMLanguage.TypeScriptTsx]: [
1322
semanticChunkingTargetQueryForLanguage(WASMLanguage.TypeScriptTsx)
1323
],
1324
[WASMLanguage.JavaScript]: [
1325
semanticChunkingTargetQueryForLanguage(WASMLanguage.JavaScript)
1326
],
1327
[WASMLanguage.Java]: [
1328
semanticChunkingTargetQueryForLanguage(WASMLanguage.Java)
1329
],
1330
[WASMLanguage.Cpp]: [
1331
semanticChunkingTargetQueryForLanguage(WASMLanguage.Cpp)
1332
],
1333
[WASMLanguage.Csharp]: [
1334
semanticChunkingTargetQueryForLanguage(WASMLanguage.Csharp)
1335
],
1336
[WASMLanguage.Python]: [
1337
semanticChunkingTargetQueryForLanguage(WASMLanguage.Python)
1338
],
1339
[WASMLanguage.Go]: [
1340
semanticChunkingTargetQueryForLanguage(WASMLanguage.Go)
1341
],
1342
[WASMLanguage.Rust]: [
1343
semanticChunkingTargetQueryForLanguage(WASMLanguage.Rust)
1344
],
1345
[WASMLanguage.Ruby]: [
1346
semanticChunkingTargetQueryForLanguage(WASMLanguage.Ruby)
1347
]
1348
});
1349
1350
1351
function coarseScopesQueryForLanguage(language: WASMLanguage): string {
1352
return coarseScopeTypes[language].map((scope) => `(${scope}) @scope`).join('\n');
1353
}
1354
1355
function semanticChunkingTargetQueryForLanguage(language: WASMLanguage): string {
1356
const blocks = semanticChunkTargetTypes[language].map((blockType) => `(${blockType})`).join('\n');
1357
return `[
1358
${blocks}
1359
] @definition`;
1360
}
1361
1362
export function _isScope(language: WASMLanguage, node: SyntaxNode): boolean {
1363
return coarseScopeTypes[language].includes(node.type) || fineScopeTypes[language].includes(node.type);
1364
}
1365
1366
export function _isFineScope(language: WASMLanguage, node: SyntaxNode): boolean {
1367
return fineScopeTypes[language].includes(node.type);
1368
}
1369
1370
export function _isStatement(language: WASMLanguage, node: SyntaxNode): boolean {
1371
return statementTypes[language].includes(node.type);
1372
}
1373
1374
export const testInSuiteQueries: { [wasmLanguage in WASMLanguage]: string[] } = {
1375
...forLanguages([WASMLanguage.TypeScript, WASMLanguage.TypeScriptTsx], [
1376
treeSitterQuery.typescript`[
1377
(expression_statement
1378
(call_expression
1379
function: (identifier) @fn
1380
(#any-of? @fn "test" "it")
1381
)
1382
) @test
1383
]`
1384
]),
1385
[WASMLanguage.JavaScript]: [
1386
// same as typescript, but we want different tree-sitter query linting to prevent breakages in future
1387
treeSitterQuery.javascript`[
1388
(call_expression
1389
function: (identifier) @fn
1390
(#any-of? @fn "test" "it")
1391
) @test
1392
]`
1393
],
1394
[WASMLanguage.Python]: [
1395
treeSitterQuery.python`[
1396
(function_definition
1397
name: (identifier) @fn
1398
(#match? @fn "^test_")
1399
) @test
1400
]`
1401
],
1402
[WASMLanguage.Java]: [
1403
treeSitterQuery.java`[
1404
(method_declaration
1405
name: (identifier) @fn
1406
(#match? @fn "^test")
1407
) @test
1408
]`
1409
],
1410
[WASMLanguage.Go]: [
1411
treeSitterQuery.go`[
1412
(function_declaration
1413
name: (identifier) @fn
1414
(#match? @fn "^Test")
1415
) @test
1416
]`
1417
],
1418
[WASMLanguage.Ruby]: [],
1419
[WASMLanguage.Csharp]: [],
1420
[WASMLanguage.Cpp]: [],
1421
[WASMLanguage.Rust]: []
1422
};
1423
1424