cairo_lang_syntax/node/
helpers.rs

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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use cairo_lang_utils::LookupIntern;
use smol_str::SmolStr;

use super::ast::{
    self, FunctionDeclaration, FunctionDeclarationGreen, FunctionWithBody, FunctionWithBodyPtr,
    ImplItem, ItemConstant, ItemEnum, ItemExternFunction, ItemExternFunctionPtr, ItemExternType,
    ItemImpl, ItemImplAlias, ItemInlineMacro, ItemModule, ItemStruct, ItemTrait, ItemTypeAlias,
    ItemUse, Member, Modifier, ModuleItem, OptionArgListParenthesized, Statement, StatementBreak,
    StatementContinue, StatementExpr, StatementLet, StatementReturn, TerminalIdentifierGreen,
    TokenIdentifierGreen, TraitItem, TraitItemConstant, TraitItemFunction, TraitItemFunctionPtr,
    TraitItemImpl, TraitItemType, Variant, WrappedArgList,
};
use super::db::SyntaxGroup;
use super::ids::SyntaxStablePtrId;
use super::kind::SyntaxKind;
use super::{SyntaxNode, Terminal, TypedSyntaxNode};
use crate::node::ast::{Attribute, AttributeList};
use crate::node::green::GreenNodeDetails;

#[cfg(test)]
#[path = "helpers_test.rs"]
mod test;

pub trait GetIdentifier {
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr;
}
impl ast::UsePathLeafPtr {
    pub fn name_green(&self, _syntax_db: &dyn SyntaxGroup) -> Self {
        *self
    }
}
impl GetIdentifier for ast::UsePathLeafPtr {
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        let alias_clause_green = self.alias_clause_green(db).0;
        let green_node = alias_clause_green.lookup_intern(db);
        let children = match &green_node.details {
            GreenNodeDetails::Node { children, width: _ } => children,
            _ => panic!("Unexpected token"),
        };
        if !children.is_empty() {
            return ast::TerminalIdentifierGreen(children[ast::AliasClause::INDEX_ALIAS])
                .identifier(db);
        }
        let ident_green = self.ident_green(db);
        ident_green.identifier(db)
    }
}
impl GetIdentifier for ast::PathSegmentGreen {
    /// Retrieves the text of the last identifier in the path.
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        let green_node = self.0.lookup_intern(db);
        let children = match &green_node.details {
            GreenNodeDetails::Node { children, width: _ } => children,
            _ => panic!("Unexpected token"),
        };
        let identifier = ast::TerminalIdentifierGreen(children[0]);
        identifier.identifier(db)
    }
}
impl GetIdentifier for ast::ExprPathGreen {
    /// Retrieves the text of the last identifier in the path.
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        let green_node = self.0.lookup_intern(db);
        let children = match &green_node.details {
            GreenNodeDetails::Node { children, width: _ } => children,
            _ => panic!("Unexpected token"),
        };
        assert_eq!(children.len() & 1, 1, "Expected an odd number of elements in the path.");
        let segment_green = ast::PathSegmentGreen(*children.last().unwrap());
        segment_green.identifier(db)
    }
}
impl GetIdentifier for ast::TerminalIdentifierGreen {
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        match &self.0.lookup_intern(db).details {
            GreenNodeDetails::Token(_) => "Unexpected token".into(),
            GreenNodeDetails::Node { children, width: _ } => {
                TokenIdentifierGreen(children[1]).text(db)
            }
        }
    }
}
impl GetIdentifier for ast::ExprPath {
    /// Retrieves the identifier of the last segment of the path.
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        self.elements(db).last().cloned().unwrap().identifier(db)
    }
}

/// Helper trait for ast::PathSegment.
pub trait PathSegmentEx {
    fn identifier_ast(&self, db: &dyn SyntaxGroup) -> ast::TerminalIdentifier;
    fn generic_args(&self, db: &dyn SyntaxGroup) -> Option<Vec<ast::GenericArg>>;
}
impl PathSegmentEx for ast::PathSegment {
    /// Retrieves the identifier ast of a path segment.
    fn identifier_ast(&self, db: &dyn SyntaxGroup) -> ast::TerminalIdentifier {
        match self {
            ast::PathSegment::Simple(segment) => segment.ident(db),
            ast::PathSegment::WithGenericArgs(segment) => segment.ident(db),
        }
    }
    fn generic_args(&self, db: &dyn SyntaxGroup) -> Option<Vec<ast::GenericArg>> {
        match self {
            ast::PathSegment::Simple(_) => None,
            ast::PathSegment::WithGenericArgs(segment) => {
                Some(segment.generic_args(db).generic_args(db).elements(db))
            }
        }
    }
}
impl GetIdentifier for ast::PathSegment {
    /// Retrieves the text of the segment (without the generic args).
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        self.identifier_ast(db).text(db)
    }
}
impl GetIdentifier for ast::Modifier {
    fn identifier(&self, db: &dyn SyntaxGroup) -> SmolStr {
        match self {
            Modifier::Ref(r) => r.text(db),
            Modifier::Mut(m) => m.text(db),
        }
    }
}

/// Trait for ast object with a name terminal.
pub trait NameGreen {
    /// Returns the TerminalIdentifierGreen of the `name` node.
    fn name_green(self, db: &dyn SyntaxGroup) -> TerminalIdentifierGreen;
}

impl NameGreen for FunctionDeclarationGreen {
    fn name_green(self, db: &dyn SyntaxGroup) -> TerminalIdentifierGreen {
        TerminalIdentifierGreen(
            self.0.lookup_intern(db).children()[FunctionDeclaration::INDEX_NAME],
        )
    }
}

impl NameGreen for FunctionWithBodyPtr {
    fn name_green(self, db: &dyn SyntaxGroup) -> TerminalIdentifierGreen {
        self.declaration_green(db).name_green(db)
    }
}

impl NameGreen for ItemExternFunctionPtr {
    fn name_green(self, db: &dyn SyntaxGroup) -> TerminalIdentifierGreen {
        self.declaration_green(db).name_green(db)
    }
}

impl NameGreen for TraitItemFunctionPtr {
    fn name_green(self, db: &dyn SyntaxGroup) -> TerminalIdentifierGreen {
        self.declaration_green(db).name_green(db)
    }
}

pub trait GenericParamEx {
    /// Returns the name of a generic param if one exists.
    fn name(&self, db: &dyn SyntaxGroup) -> Option<ast::TerminalIdentifier>;
}
impl GenericParamEx for ast::GenericParam {
    fn name(&self, db: &dyn SyntaxGroup) -> Option<ast::TerminalIdentifier> {
        match self {
            ast::GenericParam::Type(t) => Some(t.name(db)),
            ast::GenericParam::Const(c) => Some(c.name(db)),
            ast::GenericParam::ImplNamed(i) => Some(i.name(db)),
            ast::GenericParam::ImplAnonymous(_) => None,
            ast::GenericParam::NegativeImpl(_) => None,
        }
    }
}

/// Checks if the given attribute has a single argument with the given name.
pub fn is_single_arg_attr(db: &dyn SyntaxGroup, attr: &Attribute, arg_name: &str) -> bool {
    match attr.arguments(db) {
        OptionArgListParenthesized::ArgListParenthesized(args) => {
            matches!(&args.arguments(db).elements(db)[..],
                    [arg] if arg.as_syntax_node().get_text_without_trivia(db) == arg_name)
        }
        OptionArgListParenthesized::Empty(_) => false,
    }
}

/// Trait for querying attributes of AST items.
pub trait QueryAttrs {
    /// Generic call `self.attributes(db).elements(db)`.
    ///
    /// Implementation detail, should not be used by this trait users.
    #[doc(hidden)]
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute>;

    /// Collect all attributes named exactly `attr` attached to this node.
    fn query_attr(&self, db: &dyn SyntaxGroup, attr: &str) -> Vec<Attribute> {
        self.attributes_elements(db)
            .into_iter()
            .filter(|a| a.attr(db).as_syntax_node().get_text_without_trivia(db) == attr)
            .collect()
    }

    /// Find first attribute named exactly `attr` attached do this node.
    fn find_attr(&self, db: &dyn SyntaxGroup, attr: &str) -> Option<Attribute> {
        self.query_attr(db, attr).into_iter().next()
    }

    /// Check if this node has an attribute named exactly `attr`.
    fn has_attr(&self, db: &dyn SyntaxGroup, attr: &str) -> bool {
        self.find_attr(db, attr).is_some()
    }

    /// Checks if the given object has an attribute with the given name and argument.
    fn has_attr_with_arg(&self, db: &dyn SyntaxGroup, attr_name: &str, arg_name: &str) -> bool {
        self.query_attr(db, attr_name).iter().any(|attr| is_single_arg_attr(db, attr, arg_name))
    }
}

impl QueryAttrs for ItemConstant {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemModule {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for FunctionWithBody {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemUse {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemExternFunction {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemExternType {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemTrait {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemImpl {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemImplAlias {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemStruct {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemEnum {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for ItemTypeAlias {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for TraitItemFunction {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for TraitItemType {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for TraitItemConstant {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for TraitItemImpl {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}
impl QueryAttrs for TraitItem {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        match self {
            TraitItem::Function(item) => item.attributes_elements(db),
            TraitItem::Type(item) => item.attributes_elements(db),
            TraitItem::Constant(item) => item.attributes_elements(db),
            TraitItem::Impl(item) => item.attributes_elements(db),
            TraitItem::Missing(_) => vec![],
        }
    }
}

impl QueryAttrs for ItemInlineMacro {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for ModuleItem {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        match self {
            ModuleItem::Constant(item) => item.attributes_elements(db),
            ModuleItem::Module(item) => item.attributes_elements(db),
            ModuleItem::FreeFunction(item) => item.attributes_elements(db),
            ModuleItem::Use(item) => item.attributes_elements(db),
            ModuleItem::ExternFunction(item) => item.attributes_elements(db),
            ModuleItem::ExternType(item) => item.attributes_elements(db),
            ModuleItem::Trait(item) => item.attributes_elements(db),
            ModuleItem::Impl(item) => item.attributes_elements(db),
            ModuleItem::ImplAlias(item) => item.attributes_elements(db),
            ModuleItem::Struct(item) => item.attributes_elements(db),
            ModuleItem::Enum(item) => item.attributes_elements(db),
            ModuleItem::TypeAlias(item) => item.attributes_elements(db),
            ModuleItem::InlineMacro(item) => item.attributes_elements(db),
            ModuleItem::Missing(_) => vec![],
            ModuleItem::HeaderDoc(_) => vec![],
        }
    }
}

impl QueryAttrs for ImplItem {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        match self {
            ImplItem::Function(item) => item.attributes_elements(db),
            ImplItem::Type(item) => item.attributes_elements(db),
            ImplItem::Constant(item) => item.attributes_elements(db),
            ImplItem::Impl(item) => item.attributes_elements(db),
            ImplItem::Module(item) => item.attributes_elements(db),
            ImplItem::Use(item) => item.attributes_elements(db),
            ImplItem::ExternFunction(item) => item.attributes_elements(db),
            ImplItem::ExternType(item) => item.attributes_elements(db),
            ImplItem::Trait(item) => item.attributes_elements(db),
            ImplItem::Struct(item) => item.attributes_elements(db),
            ImplItem::Enum(item) => item.attributes_elements(db),
            ImplItem::Missing(_) => vec![],
        }
    }
}

impl QueryAttrs for AttributeList {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.elements(db)
    }
}
impl QueryAttrs for Member {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for Variant {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for StatementBreak {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for StatementContinue {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for StatementReturn {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for StatementLet {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

impl QueryAttrs for StatementExpr {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        self.attributes(db).elements(db)
    }
}

/// Allows querying attributes of a syntax node, any typed node which QueryAttrs is implemented for
/// should be added here.
impl QueryAttrs for SyntaxNode {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        match self.kind(db) {
            SyntaxKind::ItemConstant => {
                ast::ItemConstant::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemModule => {
                ast::ItemModule::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::FunctionWithBody => {
                ast::FunctionWithBody::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemUse => {
                ast::ItemUse::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemExternFunction => {
                ast::ItemExternFunction::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemExternType => {
                ast::ItemExternType::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemTrait => {
                ast::ItemTrait::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemImpl => {
                ast::ItemImpl::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemImplAlias => {
                ast::ItemImplAlias::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemStruct => {
                ast::ItemStruct::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemEnum => {
                ast::ItemEnum::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemTypeAlias => {
                ast::ItemTypeAlias::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::TraitItemFunction => {
                ast::TraitItemFunction::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::ItemInlineMacro => {
                ast::ItemInlineMacro::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::AttributeList => {
                ast::AttributeList::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::Member => {
                ast::Member::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::Variant => {
                ast::Variant::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::StatementBreak => {
                ast::StatementBreak::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::StatementContinue => {
                ast::StatementContinue::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::StatementReturn => {
                ast::StatementReturn::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::StatementLet => {
                ast::StatementLet::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            SyntaxKind::StatementExpr => {
                ast::StatementExpr::from_syntax_node(db, self.clone()).attributes_elements(db)
            }
            _ => vec![],
        }
    }
}

impl QueryAttrs for Statement {
    fn attributes_elements(&self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
        match self {
            Statement::Break(statement) => statement.attributes_elements(db),
            Statement::Continue(statement) => statement.attributes_elements(db),
            Statement::Return(statement) => statement.attributes_elements(db),
            Statement::Let(statement) => statement.attributes_elements(db),
            Statement::Expr(statement) => statement.attributes_elements(db),
            Statement::Item(statement) => statement.item(db).attributes_elements(db),
            Statement::Missing(_) => vec![],
        }
    }
}
pub trait WrappedArgListHelper {
    /// Pills the wrapping brackets to get the argument list. Returns None if `self` is `Missing`.
    fn arg_list(&self, db: &dyn SyntaxGroup) -> Option<ast::ArgList>;
    /// Gets the syntax node of the right wrapping bracket.
    fn right_bracket_syntax_node(&self, db: &dyn SyntaxGroup) -> SyntaxNode;
    /// Gets the syntax node of the left wrapping bracket.
    fn left_bracket_syntax_node(&self, db: &dyn SyntaxGroup) -> SyntaxNode;
    /// Gets a stable pointer to the left wrapping bracket.
    fn left_bracket_stable_ptr(&self, db: &dyn SyntaxGroup) -> SyntaxStablePtrId;
}
impl WrappedArgListHelper for WrappedArgList {
    fn arg_list(&self, db: &dyn SyntaxGroup) -> Option<ast::ArgList> {
        match self {
            WrappedArgList::ParenthesizedArgList(args) => Some(args.arguments(db)),
            WrappedArgList::BracketedArgList(args) => Some(args.arguments(db)),
            WrappedArgList::BracedArgList(args) => Some(args.arguments(db)),
            WrappedArgList::Missing(_) => None,
        }
    }

    fn right_bracket_syntax_node(&self, db: &dyn SyntaxGroup) -> SyntaxNode {
        match self {
            WrappedArgList::ParenthesizedArgList(args) => args.rparen(db).as_syntax_node(),
            WrappedArgList::BracketedArgList(args) => args.rbrack(db).as_syntax_node(),
            WrappedArgList::BracedArgList(args) => args.rbrace(db).as_syntax_node(),
            WrappedArgList::Missing(_) => self.as_syntax_node(),
        }
    }

    fn left_bracket_syntax_node(&self, db: &dyn SyntaxGroup) -> SyntaxNode {
        match self {
            WrappedArgList::ParenthesizedArgList(args) => args.lparen(db).as_syntax_node(),
            WrappedArgList::BracketedArgList(args) => args.lbrack(db).as_syntax_node(),
            WrappedArgList::BracedArgList(args) => args.lbrace(db).as_syntax_node(),
            WrappedArgList::Missing(_) => self.as_syntax_node(),
        }
    }

    fn left_bracket_stable_ptr(&self, db: &dyn SyntaxGroup) -> SyntaxStablePtrId {
        match self {
            WrappedArgList::ParenthesizedArgList(args) => (&args.lparen(db)).into(),
            WrappedArgList::BracketedArgList(args) => (&args.lbrack(db)).into(),
            WrappedArgList::BracedArgList(args) => (&args.lbrace(db)).into(),
            WrappedArgList::Missing(_) => self.into(),
        }
    }
}

pub trait WrappedGenericParamListHelper {
    /// Checks whether there are 0 generic parameters
    fn is_empty(&self, db: &dyn SyntaxGroup) -> bool;
}
impl WrappedGenericParamListHelper for ast::WrappedGenericParamList {
    fn is_empty(&self, db: &dyn SyntaxGroup) -> bool {
        self.generic_params(db).elements(db).is_empty()
    }
}

pub trait OptionWrappedGenericParamListHelper {
    /// Checks whether there are 0 generic parameters. True either when the generic params clause
    /// doesn't exist or when it's empty
    fn is_empty(&self, db: &dyn SyntaxGroup) -> bool;
}
impl OptionWrappedGenericParamListHelper for ast::OptionWrappedGenericParamList {
    fn is_empty(&self, db: &dyn SyntaxGroup) -> bool {
        match self {
            ast::OptionWrappedGenericParamList::Empty(_) => true,
            ast::OptionWrappedGenericParamList::WrappedGenericParamList(
                wrapped_generic_param_list,
            ) => wrapped_generic_param_list.is_empty(db),
        }
    }
}

/// Trait for getting the items of a body-item (an item that contains items), as a vector.
pub trait BodyItems {
    /// The type of an Item.
    type Item;
    /// Returns the items of the body-item as a vector.
    /// Use with caution, as this includes items that may be filtered out by plugins.
    /// Do note that plugins that directly run on this body-item without going/checking up on the
    /// syntax tree may assume that e.g. out-of-config items were already filtered out.
    /// Don't use on an item that is not the original plugin's context, unless you are sure that
    /// while traversing the AST to get to it from the original plugin's context, you did not go
    /// through another submodule.
    fn items_vec(&self, db: &dyn SyntaxGroup) -> Vec<Self::Item>;
}

impl BodyItems for ast::ModuleBody {
    type Item = ModuleItem;
    fn items_vec(&self, db: &dyn SyntaxGroup) -> Vec<ModuleItem> {
        self.items(db).elements(db)
    }
}

impl BodyItems for ast::TraitBody {
    type Item = TraitItem;
    fn items_vec(&self, db: &dyn SyntaxGroup) -> Vec<TraitItem> {
        self.items(db).elements(db)
    }
}

impl BodyItems for ast::ImplBody {
    type Item = ImplItem;
    fn items_vec(&self, db: &dyn SyntaxGroup) -> Vec<ImplItem> {
        self.items(db).elements(db)
    }
}

/// Helper trait for ast::UsePath.
pub trait UsePathEx {
    /// Retrieves the item of a use path.
    fn get_item(&self, db: &dyn SyntaxGroup) -> ast::ItemUse;
}
impl UsePathEx for ast::UsePath {
    fn get_item(&self, db: &dyn SyntaxGroup) -> ast::ItemUse {
        let mut node = self.as_syntax_node();
        loop {
            let Some(parent) = node.parent() else {
                unreachable!("UsePath is not under an ItemUse.");
            };
            match parent.kind(db) {
                SyntaxKind::ItemUse => {
                    break ast::ItemUse::from_syntax_node(db, parent);
                }
                _ => node = parent,
            }
        }
    }
}