ast-outline 1.1.0

Fast, AST-based structural outline for source files. Built for LLM coding agents and humans.
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
use super::base::{collapse_ws, count_parse_errors, field_text, LanguageAdapter};
use crate::core::{Declaration, DeclarationKind, ParseResult};
use ast_grep_core::{Doc, Node};
use std::path::Path;

pub struct RustAdapter;

impl LanguageAdapter for RustAdapter {
    fn language_name(&self) -> &'static str {
        "rust"
    }

    fn parse<'a, D: Doc>(&self, path: &Path, source: &[u8], root: Node<'a, D>) -> ParseResult {
        let mut decls = Vec::new();
        _walk_mod(&root, source, &mut decls);
        ParseResult {
            path: path.to_path_buf(),
            language: self.language_name(),
            source: source.to_vec(),
            line_count: source.iter().filter(|&&b| b == b'\n').count() + 1,
            declarations: decls,
            error_count: count_parse_errors(root.clone()),
        }
    }
}

/// Walk a module (or the file root) in two passes:
/// 1. Emit every top-level decl as today, EXCEPT `impl_item` which is
///    held aside in `pending_impls`.
/// 2. Distribute each pending impl into its target type's `bases` /
///    `children`. Impls whose target isn't declared in this scope (e.g.
///    `impl Display for Foo` where Foo lives in another crate) fall
///    through as a synthesized top-level decl, matching the pre-rewrite
///    behaviour so we never lose info.
///
/// `ast-outline implements Trait` now finds the *struct*, not a
/// synthetic `impl_Foo` shadow.
fn _walk_mod<'a, D: Doc>(node: &Node<'a, D>, src: &[u8], out: &mut Vec<Declaration>) {
    let mut pending_impls: Vec<Declaration> = Vec::new();

    for child in node.children() {
        if !child.is_named() {
            continue;
        }
        if child.kind() == "impl_item" {
            pending_impls.push(_impl_to_decl(&child, src));
        } else if let Some(decl) = _node_to_decl(&child, src) {
            out.push(decl);
        }
    }

    for impl_decl in pending_impls {
        // `_impl_to_decl` synthesises a name like `impl_Foo`; the real
        // target is the suffix.
        let target_name = impl_decl
            .name
            .strip_prefix("impl_")
            .unwrap_or(&impl_decl.name)
            .to_string();

        if let Some(target) = out
            .iter_mut()
            .find(|d| d.name == target_name && _is_regroup_target(&d.kind))
        {
            // Trait impl: lift the trait into the target's `bases` so
            // `find_implementations` traverses Foo, not impl_Foo.
            for b in impl_decl.bases {
                if !target.bases.contains(&b) {
                    target.bases.push(b);
                }
            }
            // Inherent or trait impl: methods become members of the type.
            target.children.extend(impl_decl.children);
        } else {
            // Target type lives elsewhere (cross-crate / foreign type).
            // Keep the synthesized decl so the methods aren't lost.
            out.push(impl_decl);
        }
    }
}

fn _is_regroup_target(kind: &DeclarationKind) -> bool {
    matches!(
        kind,
        DeclarationKind::Struct
            | DeclarationKind::Enum
            | DeclarationKind::Interface
            | DeclarationKind::Class
            | DeclarationKind::Record
    )
}

fn _node_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Option<Declaration> {
    let kind = node.kind();

    if kind == "struct_item" {
        return Some(_struct_to_decl(node, src));
    }
    if kind == "enum_item" {
        return Some(_enum_to_decl(node, src));
    }
    if kind == "trait_item" {
        return Some(_trait_to_decl(node, src));
    }
    if kind == "function_item" {
        return Some(_function_to_decl(node, src, false));
    }
    if kind == "mod_item" {
        return Some(_mod_to_decl(node, src));
    }
    if kind == "macro_definition" {
        return Some(_macro_to_decl(node, src));
    }
    if kind == "foreign_mod_item" {
        return Some(_foreign_mod_to_decl(node, src));
    }
    if kind == "union_item" {
        // Treated as a struct for outline purposes — same shape as far
        // as users navigating an outline care.
        return Some(_struct_to_decl(node, src));
    }

    None
}

fn _struct_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    if let Some(body) = node.field("body") {
        match body.kind().as_ref() {
            "field_declaration_list" => {
                for field in body.children() {
                    if field.kind() == "field_declaration" {
                        if let Some(fd) = _field_to_decl(&field, src) {
                            children.push(fd);
                        }
                    }
                }
            }
            "ordered_field_declaration_list" => {
                // Tuple struct: tree-sitter renders the body as a flat
                // sequence of `visibility_modifier?` + type nodes (no
                // `field_declaration` wrapper). Track the running visibility
                // and emit one Field per type, with synthetic name "0", "1",…
                // so users can navigate `pair.0` style.
                let mut pending_vis = String::new();
                let mut pending_attrs: Vec<String> = Vec::new();
                let mut idx = 0usize;
                for c in body.children() {
                    if !c.is_named() {
                        continue;
                    }
                    let k = c.kind();
                    if k == "visibility_modifier" {
                        pending_vis = collapse_ws(&c.text());
                        continue;
                    }
                    if k == "attribute_item" {
                        pending_attrs.push(collapse_ws(&c.text()));
                        continue;
                    }
                    children.push(_positional_field_to_decl(
                        &c,
                        src,
                        idx,
                        std::mem::take(&mut pending_vis),
                        std::mem::take(&mut pending_attrs),
                    ));
                    idx += 1;
                }
            }
            _ => {}
        }
    }
    // Unit structs (`struct Foo;`) have no body field — children stays empty,
    // which is the correct outline.

    let sig_end = node
        .field("body")
        .map(|b| b.range().start)
        .unwrap_or(node.range().end);
    let sig = collapse_ws(&String::from_utf8_lossy(&src[node.range().start..sig_end]))
        .trim_end_matches(&[' ', '{', ';'][..])
        .to_string();

    Declaration {
        kind: DeclarationKind::Struct,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _enum_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    if let Some(body) = node.field("body") {
        for variant in body.children() {
            if variant.kind() == "enum_variant" {
                let vname = field_text(&variant, "name").unwrap_or_else(|| "?".to_string());
                let vr = variant.range();
                children.push(Declaration {
                    kind: DeclarationKind::EnumMember,
                    name: vname.clone(),
                    signature: vname,
                    bases: Vec::new(),
                    attrs: Vec::new(),
                    docs: Vec::new(),
                    docs_inside: false,
                    visibility: String::new(),
                    start_line: variant.start_pos().line() + 1,
                    end_line: variant.end_pos().line() + 1,
                    start_byte: vr.start,
                    end_byte: vr.end,
                    doc_start_byte: vr.start,
                    native_kind: None,
                    modifiers: Vec::new(),
                    deprecated: false,
                    children: Vec::new(),
                });
            }
        }
    }

    let sig_end = node
        .field("body")
        .map(|b| b.range().start)
        .unwrap_or(node.range().end);
    let sig = collapse_ws(&String::from_utf8_lossy(&src[node.range().start..sig_end]))
        .trim_end_matches(&[' ', '{'][..])
        .to_string();

    Declaration {
        kind: DeclarationKind::Enum,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _trait_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    if let Some(body) = node.field("body") {
        for item in body.children() {
            match item.kind().as_ref() {
                "function_signature_item" | "function_item" => {
                    children.push(_function_to_decl(&item, src, true));
                }
                "associated_type" => {
                    if let Some(d) = _associated_type_to_decl(&item, src) {
                        children.push(d);
                    }
                }
                "const_item" => {
                    if let Some(d) = _const_or_static_to_field(&item, src) {
                        children.push(d);
                    }
                }
                _ => {}
            }
        }
    }

    let sig_end = node
        .field("body")
        .map(|b| b.range().start)
        .unwrap_or(node.range().end);
    let sig = collapse_ws(&String::from_utf8_lossy(&src[node.range().start..sig_end]))
        .trim_end_matches(&[' ', '{'][..])
        .to_string();

    Declaration {
        kind: DeclarationKind::Interface,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _impl_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "type").unwrap_or_else(|| "?".to_string());
    let trait_node = node.field("trait");
    let trait_name = trait_node.map(|t| collapse_ws(&t.text()));

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    if let Some(body) = node.field("body") {
        for item in body.children() {
            if item.kind() == "function_item" {
                children.push(_function_to_decl(&item, src, true));
            }
        }
    }

    let mut sig = "impl ".to_string();
    if let Some(t) = &trait_name {
        sig.push_str(t);
        sig.push_str(" for ");
    }
    sig.push_str(&name);

    Declaration {
        kind: DeclarationKind::Class,
        name: format!("impl_{}", name),
        signature: sig,
        bases: trait_name.into_iter().collect(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: String::new(),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _function_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8], is_method: bool) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let sig_end = node
        .field("body")
        .map(|b| b.range().start)
        .unwrap_or(node.range().end);
    let sig = collapse_ws(&String::from_utf8_lossy(&src[node.range().start..sig_end]))
        .trim_end_matches(&[' ', '{', ';'][..])
        .to_string();

    Declaration {
        kind: if is_method {
            DeclarationKind::Method
        } else {
            DeclarationKind::Function
        },
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    }
}

fn _mod_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    if let Some(body) = node.field("body") {
        _walk_mod(&body, src, &mut children);
    }

    let sig_end = node
        .field("body")
        .map(|b| b.range().start)
        .unwrap_or(node.range().end);
    let sig = collapse_ws(&String::from_utf8_lossy(&src[node.range().start..sig_end]))
        .trim_end_matches(&[' ', '{', ';'][..])
        .to_string();

    Declaration {
        kind: DeclarationKind::Namespace,
        name: name.clone(),
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _macro_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    let name = field_text(node, "name").unwrap_or_else(|| "?".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let visibility = if attrs.iter().any(|a| a.contains("macro_export")) {
        "pub".to_string()
    } else {
        String::new()
    };

    let sig = format!("macro_rules! {}", name);

    Declaration {
        kind: DeclarationKind::Delegate,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility,
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    }
}

/// `extern "C" { fn foo(...); static BAR: T; }` — surface the FFI block
/// as a Namespace named after the ABI string, with each foreign item as
/// a child function/field.
fn _foreign_mod_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Declaration {
    // `extern_modifier` is the `extern "C"` (or `extern "system"`, …) prefix.
    let abi = node
        .children()
        .find(|c| c.kind() == "extern_modifier")
        .map(|n| collapse_ws(&n.text()))
        .unwrap_or_else(|| "extern".to_string());

    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let mut children = Vec::new();
    // The body is a `declaration_list` direct child of `foreign_mod_item`.
    for body in node.children().filter(|c| c.kind() == "declaration_list") {
        for item in body.children() {
            match item.kind().as_ref() {
                "function_signature_item" => {
                    children.push(_function_to_decl(&item, src, false));
                }
                "static_item" => {
                    if let Some(d) = _const_or_static_to_field(&item, src) {
                        children.push(d);
                    }
                }
                _ => {}
            }
        }
    }

    Declaration {
        kind: DeclarationKind::Namespace,
        name: abi.clone(),
        signature: abi,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children,
    }
}

fn _associated_type_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Option<Declaration> {
    let name = field_text(node, "name")?;
    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let sig = collapse_ws(&String::from_utf8_lossy(
        &src[node.range().start..node.range().end],
    ))
    .trim_end_matches(';')
    .to_string();

    Some(Declaration {
        kind: DeclarationKind::Field,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    })
}

fn _const_or_static_to_field<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Option<Declaration> {
    let name = field_text(node, "name")?;
    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let sig = collapse_ws(&String::from_utf8_lossy(
        &src[node.range().start..node.range().end],
    ))
    .trim_end_matches(';')
    .to_string();

    Some(Declaration {
        kind: DeclarationKind::Field,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    })
}

fn _field_to_decl<'a, D: Doc>(node: &Node<'a, D>, src: &[u8]) -> Option<Declaration> {
    let name = field_text(node, "name")?;
    let mut attrs = Vec::new();
    let mut docs = Vec::new();
    _extract_attrs_and_docs(node, src, &mut attrs, &mut docs);

    let sig = collapse_ws(&String::from_utf8_lossy(
        &src[node.range().start..node.range().end],
    ))
    .trim_end_matches(',')
    .to_string();

    Some(Declaration {
        kind: DeclarationKind::Field,
        name,
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs,
        docs_inside: false,
        visibility: _visibility(node, src),
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: _doc_start(node),
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    })
}

/// Tuple-struct positional field. Tree-sitter doesn't wrap these in
/// `field_declaration` nodes — `pub struct Pair(pub u8, i32)` parses as
/// alternating `visibility_modifier` + type nodes. Caller hands us the
/// type node, the running visibility, and any preceding attrs.
fn _positional_field_to_decl<'a, D: Doc>(
    node: &Node<'a, D>,
    src: &[u8],
    idx: usize,
    visibility: String,
    attrs: Vec<String>,
) -> Declaration {
    let type_text = collapse_ws(&String::from_utf8_lossy(
        &src[node.range().start..node.range().end],
    ));
    // Prefix the index so the outline renderer (which renders fields by
    // signature, not name) shows `0: pub u8` instead of just `pub u8`.
    let sig = if !visibility.is_empty() {
        format!("{}: {} {}", idx, visibility, type_text)
    } else {
        format!("{}: {}", idx, type_text)
    };

    Declaration {
        kind: DeclarationKind::Field,
        name: idx.to_string(),
        signature: sig,
        bases: Vec::new(),
        deprecated: false,
        attrs,
        docs: Vec::new(),
        docs_inside: false,
        visibility,
        start_line: node.start_pos().line() + 1,
        end_line: node.end_pos().line() + 1,
        start_byte: node.range().start,
        end_byte: node.range().end,
        doc_start_byte: node.range().start,
        native_kind: None,
        modifiers: Vec::new(),
        children: Vec::new(),
    }
}

fn _extract_attrs_and_docs<'a, D: Doc>(
    node: &Node<'a, D>,
    _src: &[u8],
    attrs: &mut Vec<String>,
    docs: &mut Vec<String>,
) {
    let mut current = node.prev();
    let mut nodes = Vec::new();
    while let Some(prev) = current {
        if prev.kind() == "line_comment"
            || prev.kind() == "block_comment"
            || prev.kind() == "attribute_item"
        {
            nodes.push(prev.clone());
            current = prev.prev();
        } else {
            break;
        }
    }
    nodes.reverse();
    for n in nodes {
        if n.kind() == "attribute_item" {
            attrs.push(collapse_ws(&n.text()));
        } else {
            let t = n.text().into_owned();
            if t.starts_with("///") || t.starts_with("/**") {
                docs.push(t);
            }
        }
    }
}

fn _doc_start<'a, D: Doc>(node: &Node<'a, D>) -> usize {
    let mut start = node.range().start;
    let mut current = node.prev();
    while let Some(prev) = current {
        if prev.kind() == "line_comment"
            || prev.kind() == "block_comment"
            || prev.kind() == "attribute_item"
        {
            start = prev.range().start;
            current = prev.prev();
        } else {
            break;
        }
    }
    start
}

fn _visibility<'a, D: Doc>(node: &Node<'a, D>, _src: &[u8]) -> String {
    for c in node.children() {
        if c.kind() == "visibility_modifier" {
            return collapse_ws(&c.text());
        }
    }
    String::new() // Rust default is private
}