tokensave 7.1.0

Code intelligence tool that builds a semantic knowledge graph from Rust, Go, Java, Scala, TypeScript, Python, C, C++, Kotlin, C#, Swift, and many more codebases
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
//! Tree-sitter based `GDScript` (Godot 4.x) source code extractor.
//!
//! Built on the vendored `tree-sitter-gdscript` grammar (`PrestonKnopp`, ABI
//! 14, external `scanner.c`; key `"gdscript"`). Every `.gd` file is an
//! implicit script class: the file root optionally carries a
//! `class_name_statement` (-> a `Class` node) or falls back to a `Module`
//! node named after the file stem when absent. `extends` (either embedded in
//! `class_name Foo extends Bar` or as a standalone statement) becomes an
//! `Extends` edge from that node.
//!
//! Node kind notes verified against `vendor/tree-sitter-gdscript/src/node-types.json`:
//! - `func _init(...):` parses as a dedicated `constructor_definition` node
//!   (not `function_definition`), so no name-based `_init` sniffing is
//!   needed — it maps directly to `NodeKind::Constructor`.
//! - `class_body` (inner `class X:`) does not accept `constructor_definition`
//!   per the grammar's own node-types (a grammar gap, not ours): `_init`
//!   inside a nested class is only reachable as a `function_definition`
//!   there, which this extractor emits as a plain `Method`.
//! - `@export`/`@onready` vars are their own node kinds
//!   (`export_variable_statement` / `onready_variable_statement`), mapped to
//!   `Field` alongside plain `variable_statement`.
//! - Local `var` inside a function body is never visited as a Field: the
//!   member dispatcher only walks `source` (file root) and `class_body`
//!   children; function bodies are only walked by `extract_call_sites`,
//!   which looks solely for `call`/`attribute_call` nodes.

use std::time::{Instant, SystemTime, UNIX_EPOCH};

use tree_sitter::{Node as TsNode, Parser, Tree};

use crate::extraction::complexity::{count_complexity, GDSCRIPT_COMPLEXITY};
use crate::types::{
    generate_node_id, Edge, EdgeKind, ExtractionResult, Node, NodeKind, UnresolvedRef, Visibility,
};

/// Extracts code graph nodes and edges from `GDScript` source files.
pub struct GdScriptExtractor;

/// Kind of lexical scope on the scope stack. Distinct from the emitted
/// `NodeKind` of the scope-owning node: the script's own File/Class/Module
/// wrapper node still pushes a `Script` scope so its members nest correctly
/// via `Contains`, while `Function`-vs-`Method` classification depends on
/// whether we're directly at that top `Script` scope or inside a nested
/// `class X:` block (`Class` scope) — matching the grammar's own split
/// between `source`/`body`-level statements and `class_body`-level ones.
#[derive(Clone, Copy, PartialEq, Eq)]
enum ScopeKind {
    File,
    Script,
    Class,
}

/// One entry on the scope stack — used to build qualified names and parent
/// `Contains` edges.
struct Scope {
    kind: ScopeKind,
    qual: String,
    id: String,
}

/// Internal state used during AST traversal.
struct ExtractionState {
    nodes: Vec<Node>,
    edges: Vec<Edge>,
    unresolved_refs: Vec<UnresolvedRef>,
    errors: Vec<String>,
    scope_stack: Vec<Scope>,
    file_path: String,
    source: Vec<u8>,
    timestamp: u64,
}

impl ExtractionState {
    fn new(file_path: &str, source: &str) -> Self {
        let timestamp = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        Self {
            nodes: Vec::new(),
            edges: Vec::new(),
            unresolved_refs: Vec::new(),
            errors: Vec::new(),
            scope_stack: Vec::new(),
            file_path: file_path.to_string(),
            source: source.as_bytes().to_vec(),
            timestamp,
        }
    }

    fn node_text(&self, node: TsNode<'_>) -> String {
        node.utf8_text(&self.source)
            .unwrap_or("<invalid utf8>")
            .to_string()
    }

    fn current_scope(&self) -> &Scope {
        match self.scope_stack.last() {
            Some(scope) => scope,
            None => unreachable!("scope stack underflow"),
        }
    }

    fn parent_node_id(&self) -> Option<&str> {
        self.scope_stack.last().map(|s| s.id.as_str())
    }

    /// Build a member's qualified name from the current scope. The outer
    /// `File` scope uses the `path::name` convention shared with the other
    /// extractors; nested (script/class) members use dotted notation.
    fn member_qualified_name(&self, name: &str) -> String {
        let s = self.current_scope();
        match s.kind {
            ScopeKind::File => format!("{}::{}", s.qual, name),
            ScopeKind::Script | ScopeKind::Class => format!("{}.{}", s.qual, name),
        }
    }

    fn push_contains_edge(&mut self, child_id: &str, line: u32) {
        if let Some(parent_id) = self.parent_node_id() {
            self.edges.push(Edge {
                source: parent_id.to_string(),
                target: child_id.to_string(),
                kind: EdgeKind::Contains,
                line: Some(line),
            });
        }
    }

    /// Build and store a node with sane defaults, returning its id.
    #[allow(clippy::too_many_arguments)]
    fn add_node(
        &mut self,
        kind: NodeKind,
        name: &str,
        qualified_name: String,
        node: TsNode<'_>,
        attrs_start_line: u32,
        signature: Option<String>,
        docstring: Option<String>,
        visibility: Visibility,
        metrics: crate::extraction::complexity::ComplexityMetrics,
    ) -> Option<String> {
        if name.is_empty() {
            return None;
        }
        let start_line = node.start_position().row as u32;
        let id = generate_node_id(&self.file_path, &kind, name, start_line);
        let graph_node = Node {
            id: id.clone(),
            kind,
            name: name.to_string(),
            qualified_name,
            file_path: self.file_path.clone(),
            start_line,
            attrs_start_line,
            end_line: node.end_position().row as u32,
            start_column: node.start_position().column as u32,
            end_column: node.end_position().column as u32,
            signature,
            docstring,
            visibility,
            is_async: false,
            branches: metrics.branches,
            loops: metrics.loops,
            returns: metrics.returns,
            max_nesting: metrics.max_nesting,
            unsafe_blocks: metrics.unsafe_blocks,
            unchecked_calls: metrics.unchecked_calls,
            assertions: metrics.assertions,
            cognitive_complexity: metrics.cognitive_complexity,
            distinct_operators: metrics.distinct_operators,
            distinct_operands: metrics.distinct_operands,
            total_operators: metrics.total_operators,
            total_operands: metrics.total_operands,
            updated_at: self.timestamp,
            parent_id: None,
        };
        self.nodes.push(graph_node);
        self.push_contains_edge(&id, start_line);
        Some(id)
    }
}

impl GdScriptExtractor {
    /// Extract code graph nodes and edges from a `GDScript` source file.
    pub fn extract_gdscript(file_path: &str, source: &str) -> ExtractionResult {
        let start = Instant::now();
        let mut state = ExtractionState::new(file_path, source);

        let tree = match Self::parse_source(source) {
            Ok(tree) => tree,
            Err(msg) => {
                state.errors.push(msg);
                return Self::build_result(state, start);
            }
        };
        let root = tree.root_node();
        if root.has_error() {
            // Tree-sitter still produced a (partial) tree; keep going and
            // extract whatever parsed cleanly, but flag it for visibility.
            state
                .errors
                .push(format!("parse errors in {file_path} (partial extraction)"));
        }

        // File root node.
        let file_id = generate_node_id(file_path, &NodeKind::File, file_path, 0);
        state.nodes.push(Node {
            id: file_id.clone(),
            kind: NodeKind::File,
            name: file_path.to_string(),
            qualified_name: file_path.to_string(),
            file_path: file_path.to_string(),
            start_line: 0,
            attrs_start_line: 0,
            end_line: source.lines().count().saturating_sub(1) as u32,
            start_column: 0,
            end_column: 0,
            signature: None,
            docstring: None,
            visibility: Visibility::Pub,
            is_async: false,
            branches: 0,
            loops: 0,
            returns: 0,
            max_nesting: 0,
            unsafe_blocks: 0,
            unchecked_calls: 0,
            assertions: 0,
            cognitive_complexity: 0,
            distinct_operators: 0,
            distinct_operands: 0,
            total_operators: 0,
            total_operands: 0,
            updated_at: state.timestamp,
            parent_id: None,
        });
        state.scope_stack.push(Scope {
            kind: ScopeKind::File,
            qual: file_path.to_string(),
            id: file_id,
        });

        // The script's own identity: `class_name` -> Class, else a Module
        // fallback named after the file stem.
        let class_name_node = Self::find_child_by_kind(root, "class_name_statement");
        let script_name = match class_name_node {
            Some(cn) => cn
                .child_by_field_name("name")
                .map_or_else(String::new, |n| state.node_text(n)),
            None => Self::file_stem(file_path),
        };
        let script_kind = if class_name_node.is_some() {
            NodeKind::Class
        } else {
            NodeKind::Module
        };
        let attrs_start = class_name_node.map_or(0, |n| Self::attrs_start_line(n));
        let docstring = class_name_node.and_then(|n| Self::extract_docstring(&state, n));
        let signature = class_name_node.map(|n| Self::first_line(&state, n));
        let qn = state.member_qualified_name(&script_name);

        let script_id = state.add_node(
            script_kind,
            &script_name,
            qn.clone(),
            root,
            attrs_start,
            signature,
            docstring,
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
        let Some(script_id) = script_id else {
            state.scope_stack.pop();
            return Self::build_result(state, start);
        };

        // Extends target: embedded in `class_name Foo extends Bar`, or a
        // standalone `extends Bar` statement (with or without `class_name`).
        let extends_stmt = class_name_node
            .and_then(|cn| cn.child_by_field_name("extends"))
            .or_else(|| Self::find_child_by_kind(root, "extends_statement"));
        if let Some(es) = extends_stmt {
            if let Some(target) = Self::extends_text(&state, es) {
                Self::push_ref(&mut state, &script_id, &target, EdgeKind::Extends, es);
            }
        }

        state.scope_stack.push(Scope {
            kind: ScopeKind::Script,
            qual: qn,
            id: script_id,
        });
        Self::visit_members(&mut state, root);
        state.scope_stack.pop(); // Script
        state.scope_stack.pop(); // File

        Self::build_result(state, start)
    }

    fn parse_source(source: &str) -> Result<Tree, String> {
        let mut parser = Parser::new();
        let language = crate::extraction::ts_provider::language("gdscript");
        parser
            .set_language(&language)
            .map_err(|e| format!("failed to load GDScript grammar: {e}"))?;
        parser
            .parse(source, None)
            .ok_or_else(|| "tree-sitter parse returned None".to_string())
    }

    /// Visit direct named children of a member container (`source` file root
    /// or an inner `class_body`), dispatching declarations. `class_name`/
    /// `extends` statements are already consumed by the caller and simply
    /// fall through the dispatcher's default arm.
    fn visit_members(state: &mut ExtractionState, container: TsNode<'_>) {
        let mut cursor = container.walk();
        if cursor.goto_first_child() {
            loop {
                let child = cursor.node();
                if child.is_named() {
                    Self::visit_node(state, child);
                }
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
    }

    fn visit_node(state: &mut ExtractionState, node: TsNode<'_>) {
        match node.kind() {
            "function_definition" => Self::visit_function(state, node),
            "constructor_definition" => Self::visit_constructor(state, node),
            "signal_statement" => Self::visit_signal(state, node),
            "variable_statement" | "export_variable_statement" | "onready_variable_statement" => {
                Self::visit_field(state, node);
            }
            "const_statement" => Self::visit_const(state, node),
            "enum_definition" => Self::visit_enum(state, node),
            "class_definition" => Self::visit_inner_class(state, node),
            _ => {}
        }
    }

    /// `func name(...):` — a `Method` inside a nested `class X:` block,
    /// otherwise a `Function` (the script's own top-level members).
    fn visit_function(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = state.node_text(name_node);
        if name.is_empty() {
            return;
        }
        let kind = match state.current_scope().kind {
            ScopeKind::Class => NodeKind::Method,
            ScopeKind::Script | ScopeKind::File => NodeKind::Function,
        };
        let body = node.child_by_field_name("body");
        let metrics = body
            .map(|b| count_complexity(b, &GDSCRIPT_COMPLEXITY, &state.source))
            .unwrap_or_default();
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::signature_text(state, node, body));
        let id = state.add_node(
            kind,
            &name,
            qn,
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            metrics,
        );
        let Some(id) = id else { return };

        if let Some(rt) = node.child_by_field_name("return_type") {
            let ty = state.node_text(rt);
            if !ty.is_empty() {
                Self::push_ref(state, &id, &ty, EdgeKind::TypeOf, rt);
            }
        }
        if let Some(body) = body {
            Self::extract_call_sites(state, body, &id);
        }
    }

    /// `func _init(...):` — the grammar emits a dedicated `constructor_definition`
    /// node for this (see module docs), so no name sniffing is needed.
    fn visit_constructor(state: &mut ExtractionState, node: TsNode<'_>) {
        let name = "_init";
        let body = node.child_by_field_name("body");
        let metrics = body
            .map(|b| count_complexity(b, &GDSCRIPT_COMPLEXITY, &state.source))
            .unwrap_or_default();
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(name);
        let signature = Some(Self::signature_text(state, node, body));
        let id = state.add_node(
            NodeKind::Constructor,
            name,
            qn,
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            metrics,
        );
        let Some(id) = id else { return };

        if let Some(rt) = node.child_by_field_name("return_type") {
            let ty = state.node_text(rt);
            if !ty.is_empty() {
                Self::push_ref(state, &id, &ty, EdgeKind::TypeOf, rt);
            }
        }
        if let Some(body) = body {
            Self::extract_call_sites(state, body, &id);
        }
    }

    /// `signal foo(a, b)`.
    fn visit_signal(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = state.node_text(name_node);
        if name.is_empty() {
            return;
        }
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        state.add_node(
            NodeKind::Signal,
            &name,
            qn,
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
    }

    /// `var`/`@export var`/`@onready var` at script or class scope -> Field.
    /// Never called for locals: function bodies are only walked by
    /// `extract_call_sites`, which doesn't dispatch to this.
    fn visit_field(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = state.node_text(name_node);
        if name.is_empty() {
            return;
        }
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        let id = state.add_node(
            NodeKind::Field,
            &name,
            qn,
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
        let Some(id) = id else { return };
        Self::push_type_ref(state, &id, node);
    }

    /// `const NAME = value`.
    fn visit_const(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = state.node_text(name_node);
        if name.is_empty() {
            return;
        }
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        let id = state.add_node(
            NodeKind::Const,
            &name,
            qn,
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
        let Some(id) = id else { return };
        Self::push_type_ref(state, &id, node);
    }

    /// Push a `TypeOf` ref for a `variable_statement`/`const_statement`'s
    /// `type` field, if present and it's an explicit `type` node (not
    /// `inferred_type`, which carries no type text).
    fn push_type_ref(state: &mut ExtractionState, id: &str, node: TsNode<'_>) {
        if let Some(ty_node) = node.child_by_field_name("type") {
            if ty_node.kind() == "type" {
                let ty = state.node_text(ty_node);
                if !ty.is_empty() {
                    Self::push_ref(state, id, &ty, EdgeKind::TypeOf, ty_node);
                }
            }
        }
    }

    /// `enum Name { A, B, C }` (or anonymous `enum { A, B }`).
    fn visit_enum(state: &mut ExtractionState, node: TsNode<'_>) {
        let name = node
            .child_by_field_name("name")
            .map_or_else(|| "<anonymous>".to_string(), |n| state.node_text(n));
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        let id = state.add_node(
            NodeKind::Enum,
            &name,
            qn.clone(),
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
        let Some(id) = id else { return };

        state.scope_stack.push(Scope {
            kind: ScopeKind::Class,
            qual: qn,
            id,
        });
        if let Some(body) = node.child_by_field_name("body") {
            Self::visit_enum_variants(state, body);
        }
        state.scope_stack.pop();
    }

    fn visit_enum_variants(state: &mut ExtractionState, list: TsNode<'_>) {
        let mut cursor = list.walk();
        if cursor.goto_first_child() {
            loop {
                let child = cursor.node();
                if child.kind() == "enumerator" {
                    Self::visit_enumerator(state, child);
                }
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
    }

    fn visit_enumerator(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(left) = node.child_by_field_name("left") else {
            return;
        };
        let name = state.node_text(left);
        if name.is_empty() {
            return;
        }
        let attrs_start = node.start_position().row as u32;
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        state.add_node(
            NodeKind::EnumVariant,
            &name,
            qn,
            node,
            attrs_start,
            signature,
            None,
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
    }

    /// Nested `class Name: ...` (or `class Name extends Base: ...`).
    fn visit_inner_class(state: &mut ExtractionState, node: TsNode<'_>) {
        let Some(name_node) = node.child_by_field_name("name") else {
            return;
        };
        let name = state.node_text(name_node);
        if name.is_empty() {
            return;
        }
        let attrs_start = Self::attrs_start_line(node);
        let qn = state.member_qualified_name(&name);
        let signature = Some(Self::first_line(state, node));
        let id = state.add_node(
            NodeKind::InnerClass,
            &name,
            qn.clone(),
            node,
            attrs_start,
            signature,
            Self::extract_docstring(state, node),
            Visibility::Pub,
            crate::extraction::complexity::ComplexityMetrics::default(),
        );
        let Some(id) = id else { return };

        let extends_stmt = node.child_by_field_name("extends").or_else(|| {
            node.child_by_field_name("body")
                .and_then(|b| Self::find_child_by_kind(b, "extends_statement"))
        });
        if let Some(es) = extends_stmt {
            if let Some(target) = Self::extends_text(state, es) {
                Self::push_ref(state, &id, &target, EdgeKind::Extends, es);
            }
        }

        state.scope_stack.push(Scope {
            kind: ScopeKind::Class,
            qual: qn,
            id,
        });
        if let Some(body) = node.child_by_field_name("body") {
            Self::visit_members(state, body);
        }
        state.scope_stack.pop();
    }

    // ----------------------------
    // Helpers
    // ----------------------------

    /// Record an unresolved cross-file reference (extends/typeof/calls).
    fn push_ref(
        state: &mut ExtractionState,
        from_id: &str,
        name: &str,
        kind: EdgeKind,
        at: TsNode<'_>,
    ) {
        if name.is_empty() {
            return;
        }
        state.unresolved_refs.push(UnresolvedRef {
            from_node_id: from_id.to_string(),
            reference_name: name.to_string(),
            reference_kind: kind,
            line: at.start_position().row as u32,
            column: at.start_position().column as u32,
            file_path: state.file_path.clone(),
        });
    }

    /// The `extends` target's text: a `type` node (class/dotted name) or a
    /// `string` literal (`res://...` path), whichever the `extends_statement`
    /// carries.
    fn extends_text(state: &ExtractionState, extends_stmt: TsNode<'_>) -> Option<String> {
        let mut cursor = extends_stmt.walk();
        if cursor.goto_first_child() {
            loop {
                let c = cursor.node();
                match c.kind() {
                    "type" => return Some(state.node_text(c)),
                    "string" => {
                        let raw = state.node_text(c);
                        return Some(raw.trim_matches(|ch| ch == '"' || ch == '\'').to_string());
                    }
                    _ => {}
                }
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
        None
    }

    /// File stem (basename without the `.gd` extension) used as the Module
    /// fallback name when a script has no `class_name`.
    fn file_stem(file_path: &str) -> String {
        let base = file_path.rsplit('/').next().unwrap_or(file_path);
        base.strip_suffix(".gd").unwrap_or(base).to_string()
    }

    /// First physical line of a node, trimmed — used as a compact signature.
    fn first_line(state: &ExtractionState, node: TsNode<'_>) -> String {
        state
            .node_text(node)
            .lines()
            .next()
            .unwrap_or_default()
            .trim()
            .to_string()
    }

    /// Function/constructor signature: source text from the node start up to
    /// (excluding) its body, trimmed. `GDScript` has no closing brace, so trim
    /// a trailing `:` instead.
    fn signature_text(
        state: &ExtractionState,
        node: TsNode<'_>,
        body: Option<TsNode<'_>>,
    ) -> String {
        if let Some(body) = body {
            let start = node.start_byte();
            let end = body.start_byte().min(state.source.len());
            if end > start {
                if let Ok(s) = std::str::from_utf8(&state.source[start..end]) {
                    return s.trim().trim_end_matches(':').trim().to_string();
                }
            }
        }
        Self::first_line(state, node)
    }

    /// Start line including a leading `#` comment block, so refactoring
    /// tools can select the doc + declaration together.
    fn attrs_start_line(node: TsNode<'_>) -> u32 {
        let mut start = node.start_position().row as u32;
        let mut prev = node.prev_named_sibling();
        while let Some(p) = prev {
            if p.kind() == "comment" {
                start = p.start_position().row as u32;
                prev = p.prev_named_sibling();
            } else {
                break;
            }
        }
        start
    }

    /// Leading `#` comments immediately preceding a declaration.
    fn extract_docstring(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
        let mut comments: Vec<String> = Vec::new();
        let mut prev = node.prev_named_sibling();
        while let Some(p) = prev {
            if p.kind() == "comment" {
                let t = state.node_text(p);
                comments.push(t.trim_start_matches('#').trim().to_string());
                prev = p.prev_named_sibling();
            } else {
                break;
            }
        }
        if comments.is_empty() {
            return None;
        }
        comments.reverse();
        Some(comments.join("\n"))
    }

    /// Walk a function body, recording `call` / `attribute_call` callees as
    /// unresolved `Calls` references. `GDScript` has no nested named function
    /// definitions (only `lambda` expressions), so unlike `ActionScript` there
    /// is no nested-function guard needed here.
    fn extract_call_sites(state: &mut ExtractionState, node: TsNode<'_>, fn_id: &str) {
        let mut cursor = node.walk();
        if cursor.goto_first_child() {
            loop {
                let child = cursor.node();
                if matches!(child.kind(), "call" | "attribute_call") {
                    if let Some(callee) = Self::callee_name(state, child) {
                        state.unresolved_refs.push(UnresolvedRef {
                            from_node_id: fn_id.to_string(),
                            reference_name: callee,
                            reference_kind: EdgeKind::Calls,
                            line: child.start_position().row as u32,
                            column: child.start_position().column as u32,
                            file_path: state.file_path.clone(),
                        });
                    }
                }
                Self::extract_call_sites(state, child, fn_id);
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
    }

    /// The callee name of a `call` / `attribute_call` node. `attribute_call`
    /// (`obj.method()`) carries the method name as its `identifier` child
    /// directly (the receiver is a sibling under the enclosing `attribute`
    /// node, not part of this node). `call` (`foo()`) carries the callee as
    /// its first named child ahead of the `arguments` field.
    fn callee_name(state: &ExtractionState, node: TsNode<'_>) -> Option<String> {
        match node.kind() {
            "attribute_call" => {
                Self::find_child_by_kind(node, "identifier").map(|n| state.node_text(n))
            }
            "call" => {
                let mut cursor = node.walk();
                if cursor.goto_first_child() {
                    loop {
                        let c = cursor.node();
                        if c.is_named() && c.kind() != "arguments" {
                            let text = state.node_text(c);
                            let trimmed = text.trim();
                            return if trimmed.is_empty() {
                                None
                            } else {
                                Some(trimmed.rsplit('.').next().unwrap_or(trimmed).to_string())
                            };
                        }
                        if !cursor.goto_next_sibling() {
                            break;
                        }
                    }
                }
                None
            }
            _ => None,
        }
    }

    fn find_child_by_kind<'a>(node: TsNode<'a>, kind: &str) -> Option<TsNode<'a>> {
        let mut cursor = node.walk();
        if cursor.goto_first_child() {
            loop {
                let c = cursor.node();
                if c.kind() == kind {
                    return Some(c);
                }
                if !cursor.goto_next_sibling() {
                    break;
                }
            }
        }
        None
    }

    fn build_result(state: ExtractionState, start: Instant) -> ExtractionResult {
        ExtractionResult {
            nodes: state.nodes,
            edges: state.edges,
            unresolved_refs: state.unresolved_refs,
            errors: state.errors,
            duration_ms: start.elapsed().as_millis() as u64,
        }
    }
}

impl crate::extraction::LanguageExtractor for GdScriptExtractor {
    fn extensions(&self) -> &[&str] {
        &["gd"]
    }

    fn language_name(&self) -> &'static str {
        "GDScript"
    }

    fn extract(&self, file_path: &str, source: &str) -> ExtractionResult {
        Self::extract_gdscript(file_path, source)
    }
}