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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::fmt::Debug;
use clap::ValueEnum;
use const_format::formatcp;
use super::{
Find, IGNORE, LanguageScoper, QuerySource, TSLanguage, TSQuery, TSQueryError, TreeSitterRegex,
};
/// A compiled query for the Rust language.
#[derive(Debug)]
pub struct CompiledQuery(super::CompiledQuery);
impl TryFrom<QuerySource> for CompiledQuery {
type Error = TSQueryError;
/// Create a new compiled query for the Rust language.
///
/// # Errors
///
/// See the concrete type of the [`TSQueryError`](tree_sitter::QueryError) variant for when this method errors.
fn try_from(query: QuerySource) -> Result<Self, Self::Error> {
let q = super::CompiledQuery::from_source(&tree_sitter_rust::LANGUAGE.into(), &query)?;
Ok(Self(q))
}
}
impl From<PreparedQuery> for CompiledQuery {
fn from(query: PreparedQuery) -> Self {
Self(super::CompiledQuery::from_prepared_query(
&tree_sitter_rust::LANGUAGE.into(),
&query.as_string(),
))
}
}
/// Prepared tree-sitter queries for Rust.
#[derive(Debug, Clone, ValueEnum)]
pub enum PreparedQuery {
/// Comments (line and block styles; excluding doc comments; comment chars incl.).
Comments,
/// Doc comments (comment chars included).
DocComments,
/// Use statements (paths only; excl. `use`/`as`/`*`).
Uses,
/// Strings (regular, raw, byte; includes interpolation parts in format strings!).
///
/// There is currently no support for an 'interpolation' type node in
/// tree-sitter-rust (like there is in TypeScript and Python, for example).
Strings,
/// Attributes like `#[attr]`.
Attribute,
/// `struct` definitions.
Struct,
/// `struct` definitions, where the struct name matches the provided pattern.
#[value(skip)] // Non-unit enum variants need: https://github.com/clap-rs/clap/issues/2621
StructNamed(TreeSitterRegex),
/// `struct` definitions not marked `pub`.
PrivStruct,
/// `struct` definitions marked `pub`.
PubStruct,
/// `struct` definitions marked `pub(crate)`.
PubCrateStruct,
/// `struct` definitions marked `pub(self)`.
PubSelfStruct,
/// `struct` definitions marked `pub(super)`.
PubSuperStruct,
/// `enum` definitions.
Enum,
/// `enum` definitions, where the enum name matches the provided pattern.
#[value(skip)] // Non-unit enum variants need: https://github.com/clap-rs/clap/issues/2621
EnumNamed(TreeSitterRegex),
/// `enum` definitions not marked `pub`.
PrivEnum,
/// `enum` definitions marked `pub`.
PubEnum,
/// `enum` definitions marked `pub(crate)`.
PubCrateEnum,
/// `enum` definitions marked `pub(self)`.
PubSelfEnum,
/// `enum` definitions marked `pub(super)`.
PubSuperEnum,
/// Variant members of `enum` definitions
EnumVariant,
/// Function definitions.
Fn,
/// Function definitions, where the function name matches the provided pattern.
#[value(skip)] // Non-unit enum variants need: https://github.com/clap-rs/clap/issues/2621
FnNamed(TreeSitterRegex),
/// Function definitions inside `impl` blocks (associated functions/methods).
ImplFn,
/// Function definitions not marked `pub`.
PrivFn,
/// Function definitions marked `pub`.
PubFn,
/// Function definitions marked `pub(crate)`.
PubCrateFn,
/// Function definitions marked `pub(self)`.
PubSelfFn,
/// Function definitions marked `pub(super)`.
PubSuperFn,
/// Function definitions marked `const`
ConstFn,
/// Function definitions marked `async`
AsyncFn,
/// Function definitions marked `unsafe`
UnsafeFn,
/// Function definitions marked `extern`
ExternFn,
/// Function definitions with attributes containing `test` (`#[test]`, `#[rstest]`,
/// ...).
TestFn,
/// `trait` definitions.
Trait,
/// `trait` definitions, where the trait name matches the provided pattern.
#[value(skip)] // Non-unit enum variants need: https://github.com/clap-rs/clap/issues/2621
TraitNamed(TreeSitterRegex),
/// `impl` blocks.
Impl,
/// `impl` blocks for types (`impl SomeType {}`).
ImplType,
/// `impl` blocks for traits on types (`impl SomeTrait for SomeType {}`).
ImplTrait,
/// `mod` blocks.
Mod,
/// `mod` blocks, where the module name matches the provided pattern.
#[value(skip)] // Non-unit enum variants need: https://github.com/clap-rs/clap/issues/2621
ModNamed(TreeSitterRegex),
/// `mod tests` blocks.
ModTests,
/// Type definitions (`struct`, `enum`, `union`).
TypeDef,
/// Identifiers.
Identifier,
/// Identifiers for types.
TypeIdentifier,
/// Closure definitions.
Closure,
/// `unsafe` keyword usages (`unsafe fn`, `unsafe` blocks, `unsafe Trait`, `unsafe
/// impl Trait`).
Unsafe,
}
impl PreparedQuery {
#[expect(clippy::too_many_lines)]
fn as_string(&self) -> String {
match self {
Self::Comments => {
r#"
[
(line_comment)+ @line
(block_comment)
(#not-match? @line "^///")
]
@comment
"#
}
.into(),
Self::DocComments => {
r#"
(
(line_comment)+ @line
(#match? @line "^//(/|!)")
)
"#
}
.into(),
Self::Uses => {
// Match any (wildcard `_`) `argument`, which includes:
//
// - `scoped_identifier`
// - `scoped_use_list`
// - `use_wildcard`
// - `use_as_clause`
//
// all at once.
r"
[
(use_declaration
argument: (_) @use
)
]
"
}
.into(),
Self::Strings => "(string_content) @string".into(),
Self::Attribute => "(attribute) @attribute".into(),
Self::Struct => "(struct_item) @struct_item".into(),
Self::StructNamed(pattern) => {
format!(
r#"(
(struct_item
name: _ @name
)
(#match? @name "{pattern}")
) @struct_item"#,
)
}
Self::PrivStruct => {
r"(struct_item
.
name: (type_identifier)
) @struct_item_without_visibility_modifier"
}
.into(),
Self::PubStruct => {
r#"(struct_item
(visibility_modifier) @vis
(#eq? @vis "pub")
) @struct_item"#
}
.into(),
Self::PubCrateStruct => {
r"(struct_item
(visibility_modifier (crate))
) @struct_item"
}
.into(),
Self::PubSelfStruct => {
r"(struct_item
(visibility_modifier (self))
) @struct_item"
}
.into(),
Self::PubSuperStruct => {
r"(struct_item
(visibility_modifier (super))
) @struct_item"
}
.into(),
Self::Enum => "(enum_item) @enum_item".into(),
Self::EnumNamed(pattern) => {
format!(
r#"(
(enum_item
name: _ @name
)
(#match? @name "{pattern}")
) @enum_item"#,
)
}
Self::PrivEnum => {
r"(enum_item
.
name: (type_identifier)
) @enum_item_without_visibility_modifier"
}
.into(),
Self::PubEnum => {
r#"(enum_item
(visibility_modifier) @vis
(#eq? @vis "pub")
) @enum_item"#
}
.into(),
Self::PubCrateEnum => {
r"(enum_item
(visibility_modifier (crate))
) @enum_item"
}
.into(),
Self::PubSelfEnum => {
r"(enum_item
(visibility_modifier (self))
) @enum_item"
}
.into(),
Self::PubSuperEnum => {
r"(enum_item
(visibility_modifier (super))
) @enum_item"
}
.into(),
Self::EnumVariant => "(enum_variant) @enum_variant".into(),
Self::Fn => "(function_item) @function_item".into(),
Self::FnNamed(pattern) => {
format!(
r#"(
(function_item
name: _ @name
)
(#match? @name "{pattern}")
) @function_item"#,
)
}
Self::ImplFn => {
r"(impl_item
body: (_ (function_item) @function)
)"
}
.into(),
Self::PrivFn => {
r"(function_item
.
name: (identifier)
) @function_item_without_visibility_modifier"
}
.into(),
Self::PubFn => {
r#"(function_item
(visibility_modifier) @vis
(#eq? @vis "pub")
) @function_item"#
}
.into(),
Self::PubCrateFn => {
r"(function_item
(visibility_modifier (crate))
) @function_item"
}
.into(),
Self::PubSelfFn => {
r"(function_item
(visibility_modifier (self))
) @function_item"
}
.into(),
Self::PubSuperFn => {
r"(function_item
(visibility_modifier (super))
) @function_item"
}
.into(),
Self::ConstFn => {
r#"(function_item
(function_modifiers) @funcmods
(#match? @funcmods "const")
) @function_item"#
}
.into(),
Self::AsyncFn => {
r#"(function_item
(function_modifiers) @funcmods
(#match? @funcmods "async")
) @function_item"#
}
.into(),
Self::UnsafeFn => {
r#"(function_item
(function_modifiers) @funcmods
(#match? @funcmods "unsafe")
) @function_item"#
}
.into(),
Self::ExternFn => {
r"(function_item
(function_modifiers (extern_modifier))
) @extern_function"
}
.into(),
Self::TestFn => {
// Any attribute which matches aka contains `test`, preceded or
// followed by more attributes, eventually preceded by a function.
// The anchors of `.` ensure nothing but the items we're after occur
// in between.
formatcp!(
"
(
(attribute_item)*
.
(attribute_item (attribute) @{0}.attr (#match? @{0}.attr \"test\"))
.
(attribute_item)*
.
(function_item) @func
)",
IGNORE
)
}
.into(),
Self::Trait => "(trait_item) @trait_item".into(),
Self::TraitNamed(pattern) => {
format!(
r#"(
(trait_item
name: _ @name
)
(#match? @name "{pattern}")
) @trait_item"#,
)
}
Self::Impl => "(impl_item) @impl_item".into(),
Self::ImplType => {
r"(impl_item
type: (_)
!trait
) @impl_item"
}
.into(),
Self::ImplTrait => {
r"(impl_item
trait: (_)
.
type: (_)
) @impl_item"
}
.into(),
Self::Mod => "(mod_item) @mod_item".into(),
Self::ModNamed(pattern) => {
format!(
r#"(
(mod_item
name: _ @name
)
(#match? @name "{pattern}")
) @mod_item"#,
)
}
Self::ModTests => {
r#"(mod_item
name: (identifier) @mod_name
(#eq? @mod_name "tests")
) @mod_tests
"#
}
.into(),
Self::TypeDef => {
r"
[
(struct_item)
(enum_item)
(union_item)
]
@typedef
"
}
.into(),
Self::Identifier => "(identifier) @identifier".into(),
Self::TypeIdentifier => "(type_identifier) @identifier".into(),
Self::Closure => "(closure_expression) @closure".into(),
Self::Unsafe => {
r#"
[
(
(trait_item) @ti (#match? @ti "^unsafe")
)
(
(impl_item) @ii (#match? @ii "^unsafe")
)
(function_item
(function_modifiers) @funcmods
(#match? @funcmods "unsafe")
) @function_item
(function_signature_item
(function_modifiers) @funcmods
(#match? @funcmods "unsafe")
) @function_signature_item
(unsafe_block) @block
] @unsafe
"#
}
.into(),
}
}
}
impl LanguageScoper for CompiledQuery {
fn lang() -> TSLanguage {
tree_sitter_rust::LANGUAGE.into()
}
fn pos_query(&self) -> &TSQuery {
&self.0.positive_query
}
fn neg_query(&self) -> Option<&TSQuery> {
self.0.negative_query.as_ref()
}
}
impl Find for CompiledQuery {
fn extensions(&self) -> &'static [&'static str] {
&["rs"]
}
}