knot 1.11.1

Codebase Graph + Vector RAG Indexer for Java, TypeScript, JavaScript, Kotlin, Rust, Python, Groovy, C/C++, C#, Build Systems, and HTML/CSS codebases
Documentation
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
use crate::models::{EntityKind, ReferenceIntent};
use crate::pipeline::parser::languages::{
    cpp, csharp, css, groovy, html, java, javascript, kotlin, markdown, python, rust, typescript,
};
use crate::pipeline::parser::utils::*;
use tree_sitter::Node;

/// Detect whether a Kotlin `class_declaration` AST node is really a class, interface, or enum.
///
/// In tree-sitter-kotlin-ng v1.1.0, `class`, `interface`, and `enum class` declarations all use
/// the same `class_declaration` node type. We distinguish them by inspecting the source text
/// for the keyword after visibility/modifier tokens.
fn detect_kotlin_class_kind(node: Node<'_>, source: &[u8]) -> EntityKind {
    let text = node_text(node, source);
    let first_kw = text
        .split_whitespace()
        .find(|t| {
            !matches!(
                *t,
                "public"
                    | "private"
                    | "protected"
                    | "internal"
                    | "abstract"
                    | "open"
                    | "sealed"
                    | "data"
                    | "annotation"
                    | "final"
                    | "override"
                    | "inner"
            )
        })
        .unwrap_or("");

    match first_kw {
        "enum" => EntityKind::KotlinEnum,
        "interface" => EntityKind::KotlinInterface,
        _ => EntityKind::KotlinClass,
    }
}

#[derive(Default)]
pub(crate) struct CaptureState<'a> {
    pub name: Option<String>,
    pub kind: Option<EntityKind>,
    pub signature: Option<String>,
    pub start_line: usize,
    pub entity_node: Option<Node<'a>>,
    pub reference_intents: Vec<ReferenceIntent>,
}

/// Immutable context shared by every capture handler: the tree-sitter node
/// that produced the capture, the file bytes, and the language being parsed.
struct CaptureCtx<'a> {
    node: Node<'a>,
    source_bytes: &'a [u8],
    lang_name: &'a str,
}

/// Dispatch call-reference extraction to the language handler shared by the
/// `method.name`, `function.name`, and `constant.name` capture arms.
/// The fallback is TypeScript: those captures only occur in TS/JS/Kotlin
/// grammars (Java uses `method.name`), so anything reaching the fallback is
/// TypeScript.
fn extract_call_reference_intents(
    node: Node<'_>,
    source_bytes: &[u8],
    lang_name: &str,
    reference_intents: &mut Vec<ReferenceIntent>,
) {
    match lang_name {
        "java" => java::extract_reference_intents_java(node, source_bytes, reference_intents),
        "javascript" => {
            javascript::extract_reference_intents_javascript(node, source_bytes, reference_intents);
        }
        "kotlin" => {
            kotlin::extract_reference_intents_kotlin(node, source_bytes, reference_intents);
        }
        _ => {
            typescript::extract_reference_intents_typescript(node, source_bytes, reference_intents);
        }
    }
}

/// Dispatches a single tree-sitter capture to the handler for its capture
/// family. Guard arms are order-sensitive: prefix families (`css.`, `dom.`,
/// ...) must be tested before the trailing ignore-list, which names captures
/// that some of those prefixes also match.
#[expect(
    clippy::too_many_arguments,
    reason = "capture processing requires context from tree-sitter, language, and queries"
)]
pub(crate) fn process_capture<'a>(
    cap_name: &str,
    text: String,
    node: Node<'a>,
    source_bytes: &'a [u8],
    lang_name: &'a str,
    state: &mut CaptureState<'a>,
) {
    let ctx = CaptureCtx {
        node,
        source_bytes,
        lang_name,
    };

    match cap_name {
        "class.name" | "kotlin_class.name" | "interface.name" | "kotlin_interface.name" => {
            handle_class_captures(cap_name, &text, &ctx, state);
        }
        "method.name" | "kotlin_method.name" => {
            handle_method_captures(cap_name, &text, &ctx, state);
        }
        "kotlin_object.name" | "kotlin_companion.name" | "kotlin_property.name" => {
            handle_kotlin_decl_captures(cap_name, &text, &ctx, state);
        }
        "kotlin_function.name" => handle_kotlin_function_capture(&text, &ctx, state),
        "function.name" | "constant.name" => {
            handle_function_captures(cap_name, &text, &ctx, state);
        }
        "enum.name" => {
            state.name = Some(text);
            state.kind = Some(EntityKind::Enum);
            state.start_line = node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(node, "enum_declaration");
        }
        "signature" | "python.signature" => state.signature = Some(text),
        "type.reference" => {
            // Type annotations in signatures, variables, etc.
            state
                .reference_intents
                .push(ReferenceIntent::TypeReference {
                    type_name: text,
                    line: node.start_position().row + 1,
                });
        }
        // CSS/SCSS: Delegate to specialized handler
        name_or_intent
            if name_or_intent.starts_with("css.") || name_or_intent.starts_with("scss.") =>
        {
            handle_css_scss_capture(name_or_intent, &text, &ctx, state);
        }
        // HTML: Delegate to specialized handler
        name_or_intent if name_or_intent.starts_with("html_") => {
            handle_html_capture(name_or_intent, &text, &ctx, state);
        }
        // Rust: Handle Rust entity captures
        name_or_intent if name_or_intent.starts_with("rust.") => {
            handle_rust_capture(name_or_intent, &text, &ctx, state);
        }
        // Python: Handle Python entity captures
        name_or_intent if name_or_intent.starts_with("python.") => {
            handle_python_capture(name_or_intent, &text, &ctx, state);
        }
        // Markdown: Handle Markdown entity captures
        name_or_intent if name_or_intent.starts_with("markdown.") => {
            handle_markdown_capture(name_or_intent, &text, &ctx, state);
        }
        // DOM/CSS references: Delegate to JavaScript handler
        name_or_intent
            if name_or_intent.starts_with("dom.") || name_or_intent.starts_with("css.class_") =>
        {
            handle_dom_css_ref_capture(name_or_intent, &text, &ctx, state);
        }
        // Groovy: Handle Groovy entity captures (JVM-shared grammar with Java)
        name_or_intent if name_or_intent.starts_with("groovy.") => {
            handle_groovy_capture(name_or_intent, &text, &ctx, state);
        }
        // C#: Handle C# entity captures (grammar-gap handling in the module)
        name_or_intent if name_or_intent.starts_with("csharp.") => {
            handle_csharp_capture(name_or_intent, &text, &ctx, state);
        }
        // C/C++ Entities
        name_or_intent
            if name_or_intent.starts_with("cpp_")
                || name_or_intent.starts_with("c_")
                || name_or_intent.starts_with("preproc.") =>
        {
            handle_cpp_captures(name_or_intent, &text, &ctx, state);
        }
        // Ignore unhandled captures
        "dom.receiver" | "dom.action" | "dom.method" | "css.receiver" | "css.classList"
        | "css.className" | "css.method" | "css.keyframe" | "script_src" | "stylesheet_href" => {
            // These captures are either metadata or handled in other passes
        }
        _ => {}
    }
}

/// Class and interface name captures shared by Java, TS/JS, and Kotlin
/// grammars. Kotlin reuses `class_declaration` for classes, interfaces, and
/// enums, so its kind is re-detected from the source keywords.
fn handle_class_captures<'a>(
    cap_name: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    match cap_name {
        "class.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::Class);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "class_declaration")
                .or_else(|| find_parent_by_kind(ctx.node, "abstract_class_declaration"));
        }
        "kotlin_class.name" => {
            state.name = Some(text.to_string());
            state.start_line = ctx.node.start_position().row + 1;
            let entity_node = find_parent_by_kind(ctx.node, "class_declaration");
            state.kind = entity_node.map(|n| detect_kotlin_class_kind(n, ctx.source_bytes));
            state.entity_node = entity_node;
        }
        "interface.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::Interface);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "interface_declaration");
        }
        "kotlin_interface.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::KotlinInterface);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "interface_declaration");
        }
        _ => {}
    }
}

/// Method name captures. `method.name` is shared by Java/TS/JS grammars and
/// pulls call and type-reference intents from the method body;
/// `kotlin_method.name` is the Kotlin-specific variant.
fn handle_method_captures<'a>(
    cap_name: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    match cap_name {
        "method.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::Method);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "method_declaration")
                .or_else(|| find_parent_by_kind(ctx.node, "method_definition"))
                .or_else(|| find_parent_by_kind(ctx.node, "method_signature"))
                .or_else(|| find_parent_by_kind(ctx.node, "abstract_method_signature"));
            // For methods, extract reference intents from the method body
            if let Some(method_node) = state.entity_node {
                extract_call_reference_intents(
                    method_node,
                    ctx.source_bytes,
                    ctx.lang_name,
                    &mut state.reference_intents,
                );

                // Extract type references from method signatures (parameters, return types)
                if ctx.lang_name == "java" {
                    extract_type_references(
                        method_node,
                        ctx.source_bytes,
                        &mut state.reference_intents,
                    );
                } else if ctx.lang_name == "kotlin" {
                    kotlin::extract_type_references(
                        method_node,
                        ctx.source_bytes,
                        &mut state.reference_intents,
                    );
                }
            }
        }
        "kotlin_method.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::KotlinMethod);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "function_declaration");
            // For Kotlin methods, extract reference intents from the method body
            if let Some(method_node) = state.entity_node
                && ctx.lang_name == "kotlin"
            {
                kotlin::extract_reference_intents_kotlin(
                    method_node,
                    ctx.source_bytes,
                    &mut state.reference_intents,
                );
            }
        }
        _ => {}
    }
}

/// Simple Kotlin declaration captures: objects, companion objects, and
/// properties. Only properties pull reference intents.
fn handle_kotlin_decl_captures<'a>(
    cap_name: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    match cap_name {
        "kotlin_object.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::KotlinObject);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "object_declaration");
        }
        "kotlin_companion.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::KotlinCompanionObject);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "companion_object");
        }
        "kotlin_property.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::KotlinProperty);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "property_declaration");
            // For Kotlin properties, extract reference intents from the property
            if let Some(prop_node) = state.entity_node
                && ctx.lang_name == "kotlin"
            {
                kotlin::extract_reference_intents_kotlin(
                    prop_node,
                    ctx.source_bytes,
                    &mut state.reference_intents,
                );
            }
        }
        _ => {}
    }
}

/// `kotlin_function.name` capture. A Kotlin `function_declaration` enclosed
/// in a class/object/companion/interface is really a method; outside them, it
/// is a free function.
fn handle_kotlin_function_capture<'a>(
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    state.name = Some(text.to_string());
    state.start_line = ctx.node.start_position().row + 1;
    state.entity_node = find_parent_by_kind(ctx.node, "function_declaration");

    // Determine if this function is actually a method (enclosed in a
    // class_declaration, object_declaration, companion_object, or interface_declaration).
    let is_method = state.entity_node.is_some_and(|n| {
        let mut current = n.parent();
        while let Some(p) = current {
            match p.kind() {
                "class_declaration"
                | "object_declaration"
                | "companion_object"
                | "interface_declaration" => return true,
                _ => {}
            }
            current = p.parent();
        }
        false
    });

    state.kind = Some(if is_method {
        EntityKind::KotlinMethod
    } else {
        EntityKind::KotlinFunction
    });

    // For Kotlin methods/functions, extract reference intents from the function body
    if let Some(func_node) = state.entity_node
        && ctx.lang_name == "kotlin"
    {
        kotlin::extract_reference_intents_kotlin(
            func_node,
            ctx.source_bytes,
            &mut state.reference_intents,
        );

        // Extract type references from method signatures (parameters, return types)
        // for functions that are really methods inside a class/object
        if is_method {
            kotlin::extract_type_references(
                func_node,
                ctx.source_bytes,
                &mut state.reference_intents,
            );
        }
    }
}

/// Free-function and constant captures shared by JS/TS grammars. Both pull
/// reference intents from the declaration/initializer node.
fn handle_function_captures<'a>(
    cap_name: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    match cap_name {
        "function.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::Function);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "function_declaration")
                .or_else(|| find_parent_by_kind(ctx.node, "lexical_declaration"))
                .or_else(|| find_parent_by_kind(ctx.node, "variable_declaration"))
                .or_else(|| find_parent_by_kind(ctx.node, "export_statement"));
            // For functions, extract reference intents from the function body
            if let Some(func_node) = state.entity_node {
                extract_call_reference_intents(
                    func_node,
                    ctx.source_bytes,
                    ctx.lang_name,
                    &mut state.reference_intents,
                );
            }
        }
        "constant.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::Constant);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "lexical_declaration")
                .or_else(|| find_parent_by_kind(ctx.node, "variable_declarator"))
                .or_else(|| find_parent_by_kind(ctx.node, "field_declaration"))
                .or_else(|| find_parent_by_kind(ctx.node, "public_field_definition"))
                .or_else(|| find_parent_by_kind(ctx.node, "field_definition"));

            // Extract reference intents from constant initializers
            // This captures function calls inside const assignments like:
            //   const formattedItems = formatRegistryItems(registryItems)
            //   const config = await getMcpConfig(process.cwd())
            //   val result = someFunction()
            if let Some(const_node) = state.entity_node {
                extract_call_reference_intents(
                    const_node,
                    ctx.source_bytes,
                    ctx.lang_name,
                    &mut state.reference_intents,
                );
            }
        }
        _ => {}
    }
}

fn promote_captured_entity<'a, F>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
    handler: F,
) where
    F: FnOnce(&str, &str, Node<'a>) -> Option<(String, EntityKind, usize)>,
{
    if let Some((entity_name, entity_kind, entity_line)) = handler(name_or_intent, text, ctx.node) {
        state.name = Some(entity_name);
        state.kind = Some(entity_kind);
        state.start_line = entity_line;
        state.entity_node = Some(ctx.node);
    }
}

/// CSS/SCSS capture: delegates to the CSS handler and promotes its result to
/// an entity capture on the same node.
fn handle_css_scss_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    promote_captured_entity(name_or_intent, text, ctx, state, css::handle_css_capture);
}

/// HTML capture: delegates to the HTML handler and promotes its result to an
/// entity capture on the same node.
fn handle_html_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    promote_captured_entity(name_or_intent, text, ctx, state, html::handle_html_capture);
}

/// Rust entity capture (`rust.*`). Entity kinds that carry preceding doc
/// comments on the parent item re-anchor the entity node to that parent.
fn handle_rust_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    if let Some((entity_name, entity_kind, entity_line)) =
        rust::handle_rust_capture(name_or_intent, text, ctx.node)
    {
        state.name = Some(entity_name);
        state.kind = Some(entity_kind);
        state.start_line = entity_line;

        // Rust captures point at the identifier (`function_item name:
        // (identifier)`), but preceding `///` doc comments are siblings of
        // the parent item, not of the identifier. Re-anchor every Rust
        // entity kind to its parent item so the docstring upward pass can
        // reach them (mirrors the Python handler); without this the
        // entity's embed text carries no docstring and semantic search
        // cannot find Rust definitions by behaviour.
        state.entity_node = ctx.node.parent().or(Some(ctx.node));
    }
}

/// Python entity capture (`python.*`). Callable kinds pull call intents from
/// the definition body; all kinds pull decorator intents, and classes pull
/// inheritance intents.
fn handle_python_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    if let Some((entity_name, entity_kind, entity_line)) =
        python::handle_python_capture(name_or_intent, text, ctx.node)
    {
        state.name = Some(entity_name);
        state.kind = Some(entity_kind.clone());
        state.start_line = entity_line;
        // Use parent to get the full definition node for proper scope tracking
        // (captures always point to identifiers, not the full definition)
        state.entity_node = ctx.node.parent();

        // Extract call reference intents from Python function/method bodies
        let is_callable = matches!(
            entity_kind,
            EntityKind::PythonFunction | EntityKind::PythonMethod
        );
        #[expect(
            clippy::collapsible_if,
            reason = "separate guards aid readability when both conditions are semantically distinct"
        )]
        if is_callable {
            if let Some(entity_n) = state.entity_node {
                python::extract_reference_intents_python(
                    entity_n,
                    ctx.source_bytes,
                    &mut state.reference_intents,
                );
            }
        }

        // Phase 5: Extract decorators for ALL Python entities
        if let Some(entity_n) = state.entity_node {
            python::extract_decorator_intents_python(
                entity_n,
                ctx.source_bytes,
                &mut state.reference_intents,
            );
        }

        // Phase 5: Extract inheritance (EXTENDS) for Python classes
        if entity_kind == EntityKind::PythonClass
            && let Some(entity_n) = state.entity_node
        {
            python::extract_inheritance_intents_python(
                entity_n,
                ctx.source_bytes,
                &mut state.reference_intents,
            );
        }
    }
}

/// Markdown entity capture (`markdown.*`): delegates to the Markdown handler
/// and promotes its result to an entity capture on the same node.
fn handle_markdown_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    if let Some((entity_name, entity_kind, entity_line)) =
        markdown::handle_markdown_capture(name_or_intent, text, ctx.node, ctx.source_bytes)
    {
        state.name = Some(entity_name);
        state.kind = Some(entity_kind);
        state.start_line = entity_line;
        state.entity_node = Some(ctx.node);
    }
}

/// DOM/CSS reference capture: converts `dom.*` and `css.class_*` captures
/// into JavaScript-side reference intents.
fn handle_dom_css_ref_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    if let Some(intent) =
        javascript::handle_dom_css_capture(name_or_intent, text, ctx.node.start_position().row + 1)
    {
        state.reference_intents.push(intent);
    }
}

/// Groovy entity capture (`groovy.*`). The grammar is shared with Java, so
/// parent lookup mirrors the Java node kinds.
fn handle_groovy_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    if let Some((entity_name, entity_kind, entity_line)) =
        groovy::handle_groovy_capture(name_or_intent, text, ctx.node)
    {
        state.name = Some(entity_name);
        state.kind = Some(entity_kind.clone());
        state.start_line = entity_line;
        // Find parent node using same patterns as Java (shared grammar)
        state.entity_node = match name_or_intent {
            n if n.contains("class") => find_parent_by_kind(ctx.node, "class_declaration"),
            n if n.contains("interface") => find_parent_by_kind(ctx.node, "interface_declaration"),
            n if n.contains("enum") => find_parent_by_kind(ctx.node, "enum_declaration"),
            n if n.contains("method") => find_parent_by_kind(ctx.node, "method_declaration")
                .or_else(|| find_parent_by_kind(ctx.node, "constructor_declaration")),
            _ => ctx.node.parent(),
        };
        // Groovy reference extraction is handled by ad-hoc `extract_method_calls`
        // in extract_entities_groovy_standard(), which uses innermost assignment.
        // Java's tree-sitter ref extraction is unreliable for Groovy because
        // tree-sitter-groovy misparses methods nested inside closures (e.g.,
        // `new AnAction() { @Override void actionPerformed(...) { ... } }`).
    }
}

/// C# entity capture (`csharp.*`). The signature capture rides along with the
/// name capture in the same match and does not introduce an entity; method
/// like kinds pull call + type-reference intents from their bodies.
fn handle_csharp_capture<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    // Signature capture rides along with the name capture in the
    // same match; it does not introduce an entity.
    if name_or_intent == "csharp.signature" {
        state.signature = Some(text.to_string());
    } else if let Some(capture) =
        csharp::handle_csharp_capture(name_or_intent, ctx.node, ctx.source_bytes)
    {
        let csharp_kind = capture.kind.clone();
        state.name = Some(capture.name);
        state.kind = Some(capture.kind);
        state.start_line = capture.start_line;
        state.entity_node = Some(capture.entity_node);

        // Extract call + type-reference intents from method-like
        // bodies (constructor bodies included). Class-level
        // inheritance/attribute/type refs are handled in enrich.
        if matches!(
            csharp_kind,
            EntityKind::CSharpMethod
                | EntityKind::CSharpConstructor
                | EntityKind::CSharpLocalFunction
        ) {
            csharp::extract_reference_intents_csharp(
                capture.entity_node,
                ctx.source_bytes,
                &mut state.reference_intents,
            );
        }
    }
}

/// C/C++ captures: classes, structs, namespaces, methods, functions, macros,
/// and `#include` directives.
fn handle_cpp_captures<'a>(
    name_or_intent: &str,
    text: &str,
    ctx: &CaptureCtx<'a>,
    state: &mut CaptureState<'a>,
) {
    match name_or_intent {
        "cpp_class.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::CppClass);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "class_specifier");
        }
        "c_struct.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::CStruct);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "struct_specifier");
        }
        "cpp_namespace.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::CppNamespace);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "namespace_definition");
        }
        "cpp_method.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::CppMethod);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "function_definition")
                .or_else(|| find_parent_by_kind(ctx.node, "declaration"))
                .or_else(|| find_parent_by_kind(ctx.node, "field_declaration"));
            if let Some(m_node) = state.entity_node {
                cpp::extract_reference_intents_cpp(
                    m_node,
                    ctx.source_bytes,
                    &mut state.reference_intents,
                );
            }
        }
        "c_function.name" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::CFunction);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "function_definition");
            if let Some(m_node) = state.entity_node {
                cpp::extract_reference_intents_cpp(
                    m_node,
                    ctx.source_bytes,
                    &mut state.reference_intents,
                );
            }
        }
        "preproc.macro" => {
            state.name = Some(text.to_string());
            state.kind = Some(EntityKind::MacroDefinition);
            state.start_line = ctx.node.start_position().row + 1;
            state.entity_node = find_parent_by_kind(ctx.node, "preproc_def");
        }
        "preproc.include" => {
            // Extract included file path and register it as an intent
            let mut path_str = text.to_string();
            if path_str.starts_with('"') || path_str.starts_with('<') {
                path_str = path_str[1..path_str.len() - 1].to_string();
            }
            state.reference_intents.push(ReferenceIntent::Call {
                method: path_str,
                receiver: None,
                line: ctx.node.start_position().row + 1,
                arg_count: None,
            });
        }
        _ => {}
    }
}