ck-chunk 0.7.11

Text chunking and parsing for ck semantic search
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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
use std::{borrow::Cow, collections::HashSet, env, fs, path::PathBuf};

use anyhow::{Context, Result};
use tree_sitter::{Language, Query, QueryCursor, StreamingIterator, Tree};

use crate::{Chunk, ChunkType, ParseableLanguage, build_chunk};

const QUERY_OVERRIDE_DIR_ENV: &str = "CK_CHUNK_QUERY_DIR";

pub(crate) fn chunk_with_queries(
    language: ParseableLanguage,
    ts_language: Language,
    tree: &Tree,
    source: &str,
) -> Result<Option<Vec<Chunk>>> {
    let Some(query_source) = load_query_source(language)? else {
        return Ok(None);
    };

    let query = Query::new(&ts_language, &query_source)
        .with_context(|| format!("Failed to compile query for {language}"))?;

    let capture_names = query.capture_names();
    let mut cursor = QueryCursor::new();
    let mut seen_spans = HashSet::new();
    let mut chunks = Vec::new();

    let mut matches = cursor.matches(&query, tree.root_node(), source.as_bytes());
    while let Some(mat) = matches.next() {
        for capture in mat.captures {
            let capture_name = &capture_names[capture.index as usize];
            if let Some(chunk_type) = chunk_type_from_capture(capture_name) {
                // Filter out duplicates for C/C++ where template_declaration wraps the definition
                if matches!(language, ParseableLanguage::C | ParseableLanguage::Cpp) {
                    if let Some(parent) = capture.node.parent()
                        && parent.kind() == "template_declaration"
                        && matches!(
                            chunk_type,
                            ChunkType::Class | ChunkType::Function | ChunkType::Method
                        )
                    {
                        continue;
                    }

                    if chunk_type == ChunkType::Class
                        && matches!(
                            capture.node.kind(),
                            "struct_specifier" | "union_specifier" | "enum_specifier"
                        )
                        && !c_cpp_type_has_body(capture.node)
                    {
                        continue;
                    }

                    // Filter out granular text chunks (using, typedef, locals, etc.) inside
                    // classes/structs/unions or inside function/method bodies.
                    if matches!(language, ParseableLanguage::C | ParseableLanguage::Cpp)
                        && chunk_type == ChunkType::Text
                        && is_inside_c_cpp_type_or_function_body(capture.node)
                    {
                        continue;
                    }

                    // Filter out text chunks that come from declarations inside function bodies
                    if chunk_type == ChunkType::Text && is_inside_function_or_method(capture.node) {
                        continue;
                    }

                    // Skip local class/struct/enum/union chunks inside functions
                    if chunk_type == ChunkType::Class && is_inside_function_or_method(capture.node)
                    {
                        continue;
                    }

                    // Skip local function/method chunks inside functions (e.g., local classes)
                    if matches!(chunk_type, ChunkType::Function | ChunkType::Method)
                        && is_inside_function_or_method(capture.node)
                    {
                        continue;
                    }

                    // Skip C/C++ function/method declarations without bodies (defaulted/deleted)
                    if matches!(chunk_type, ChunkType::Function | ChunkType::Method)
                        && capture.node.kind() == "function_definition"
                        && !has_compound_statement(capture.node)
                    {
                        continue;
                    }

                    // Avoid declaration-wrapped duplicates that contain full definitions
                    if capture.node.kind() == "declaration"
                        && declaration_contains_definition(capture.node)
                    {
                        continue;
                    }
                }

                if language == ParseableLanguage::Haskell
                    && chunk_type == ChunkType::Function
                    && capture
                        .node
                        .parent()
                        .is_some_and(|parent| parent.kind() == "signature")
                {
                    continue;
                }

                if let Some(chunk) = build_chunk(capture.node, source, chunk_type, language) {
                    let span_key = (chunk.span.byte_start, chunk.span.byte_end);
                    if seen_spans.insert(span_key) {
                        chunks.push(chunk);
                    }
                }
            }
        }
    }

    if chunks.is_empty() {
        return Ok(None);
    }

    chunks.sort_by_key(|chunk| chunk.span.byte_start);
    Ok(Some(chunks))
}

fn is_inside_c_cpp_type_or_function_body(mut node: tree_sitter::Node<'_>) -> bool {
    while let Some(parent) = node.parent() {
        match parent.kind() {
            "class_specifier" | "struct_specifier" | "union_specifier" => return true,
            "function_definition" | "method_definition" => return true,
            _ => {}
        }
        node = parent;
    }
    false
}

fn is_inside_function_or_method(mut node: tree_sitter::Node<'_>) -> bool {
    while let Some(parent) = node.parent() {
        match parent.kind() {
            "function_definition"
            | "method_definition"
            | "constructor_definition"
            | "destructor_definition"
            | "lambda_expression" => return true,
            _ => {}
        }
        node = parent;
    }
    false
}

fn declaration_contains_definition(node: tree_sitter::Node<'_>) -> bool {
    let mut stack = vec![node];

    while let Some(current) = stack.pop() {
        if matches!(
            current.kind(),
            "class_specifier"
                | "struct_specifier"
                | "enum_specifier"
                | "union_specifier"
                | "function_definition"
                | "method_definition"
                | "constructor_definition"
                | "destructor_definition"
        ) {
            return true;
        }

        let child_count = current.child_count();
        for idx in (0..child_count).rev() {
            if let Some(child) = current.child(idx) {
                stack.push(child);
            }
        }
    }

    false
}

fn has_compound_statement(node: tree_sitter::Node<'_>) -> bool {
    for idx in 0..node.child_count() {
        if let Some(child) = node.child(idx)
            && child.kind() == "compound_statement"
        {
            return true;
        }
    }
    false
}

fn c_cpp_type_has_body(node: tree_sitter::Node<'_>) -> bool {
    let mut cursor = node.walk();

    match node.kind() {
        "struct_specifier" | "union_specifier" => node
            .children(&mut cursor)
            .any(|child| child.kind() == "field_declaration_list"),
        "enum_specifier" => node
            .children(&mut cursor)
            .any(|child| child.kind() == "enumerator_list"),
        _ => false,
    }
}

fn load_query_source(language: ParseableLanguage) -> Result<Option<Cow<'static, str>>> {
    if let Some(dir) = env::var_os(QUERY_OVERRIDE_DIR_ENV) {
        let override_path = PathBuf::from(dir)
            .join(language.to_string())
            .join("tags.scm");

        if override_path.exists() {
            let contents = fs::read_to_string(&override_path).with_context(|| {
                format!(
                    "Failed to read query override for {} at {}",
                    language,
                    override_path.display()
                )
            })?;
            return Ok(Some(Cow::Owned(contents)));
        }
    }

    Ok(builtin_query(language).map(Cow::Borrowed))
}

fn builtin_query(language: ParseableLanguage) -> Option<&'static str> {
    match language {
        ParseableLanguage::Python => Some(include_str!("../queries/python/tags.scm")),
        ParseableLanguage::TypeScript => Some(include_str!("../queries/typescript/tags.scm")),
        ParseableLanguage::JavaScript => Some(include_str!("../queries/javascript/tags.scm")),
        ParseableLanguage::Rust => Some(include_str!("../queries/rust/tags.scm")),
        ParseableLanguage::Haskell => Some(include_str!("../queries/haskell/tags.scm")),
        ParseableLanguage::Ruby => Some(include_str!("../queries/ruby/tags.scm")),
        ParseableLanguage::Go => Some(include_str!("../queries/go/tags.scm")),
        ParseableLanguage::C => Some(include_str!("../queries/c/tags.scm")),
        ParseableLanguage::Cpp => Some(include_str!("../queries/cpp/tags.scm")),
        ParseableLanguage::CSharp => Some(include_str!("../queries/csharp/tags.scm")),
        ParseableLanguage::Zig => Some(include_str!("../queries/zig/tags.scm")),

        ParseableLanguage::Dart => Some(include_str!("../queries/dart/tags.scm")),

        ParseableLanguage::Elixir => Some(include_str!("../queries/elixir/tags.scm")),

        ParseableLanguage::Markdown => Some(include_str!("../queries/markdown/tags.scm")),
    }
}

fn chunk_type_from_capture(name: &str) -> Option<ChunkType> {
    let type_name = name.split('.').next_back().unwrap_or(name);

    match type_name {
        "function" | "fn" => Some(ChunkType::Function),
        "method" => Some(ChunkType::Method),
        "class" | "struct" | "enum" | "trait" => Some(ChunkType::Class),
        "module" | "impl" | "mod" => Some(ChunkType::Module),
        // Module attributes (Elixir @spec, @type, @callback, @behaviour)
        "spec" | "type" | "callback" | "behaviour" => Some(ChunkType::Text),
        "text" | "import" => Some(ChunkType::Text),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ChunkType, tree_sitter_language};
    use tree_sitter::Parser;

    #[test]
    fn rust_queries_capture_core_constructs() {
        let source = r"
            mod sample {
                pub struct Thing;

                impl Thing {
                    pub fn new() -> Self { Self }
                    fn helper(&self) {}
                }
            }

            fn util() {}

            trait Runner {
                fn run(&self);
            }
        ";

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Rust).expect("rust language");
        parser
            .set_language(&ts_language)
            .expect("set rust language");
        let tree = parser.parse(source, None).expect("parse rust source");

        let chunks = chunk_with_queries(ParseableLanguage::Rust, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Function
                    && chunk.text.contains("fn util"))
        );
        assert!(chunks
            .iter()
            .any(|chunk| chunk.chunk_type == ChunkType::Method && chunk.text.contains("fn new")));
        assert!(chunks.iter().any(
            |chunk| chunk.chunk_type == ChunkType::Class && chunk.text.contains("struct Thing")
        ));
        assert!(chunks.iter().any(
            |chunk| chunk.chunk_type == ChunkType::Module && chunk.text.contains("mod sample")
        ));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Module && chunk.text.contains("impl Thing")
        }));

        let method_chunk = chunks
            .iter()
            .find(|chunk| chunk.chunk_type == ChunkType::Method && chunk.text.contains("fn new"))
            .expect("method chunk present");
        assert_eq!(
            method_chunk.metadata.ancestry,
            vec!["sample".to_string(), "Thing".to_string()]
        );
        let util_chunk = chunks
            .iter()
            .find(|chunk| chunk.chunk_type == ChunkType::Function && chunk.text.contains("fn util"))
            .expect("util chunk present");
        assert!(util_chunk.metadata.ancestry.is_empty());
    }

    #[test]
    fn python_queries_capture_core_constructs() {
        let source = r#"
class Greeter:
    def __init__(self, name: str):
        self.name = name

    @staticmethod
    def hello():
        return "hi"


async def async_worker():
    return None


def top_level():
    return "done"
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Python).expect("python language");
        parser
            .set_language(&ts_language)
            .expect("set python language");
        let tree = parser.parse(source, None).expect("parse python source");

        let chunks = chunk_with_queries(ParseableLanguage::Python, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("class Greeter"))
        );
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Method && chunk.text.contains("def hello")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("def top_level")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("async def async_worker")
        }));

        let method_chunk = chunks
            .iter()
            .find(|chunk| chunk.chunk_type == ChunkType::Method && chunk.text.contains("def hello"))
            .expect("method chunk present");
        assert_eq!(method_chunk.metadata.ancestry, vec!["Greeter".to_string()]);
        assert!(
            method_chunk
                .metadata
                .leading_trivia
                .iter()
                .any(|text| text.contains("@staticmethod"))
        );

        let top_level_chunk = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Function && chunk.text.contains("def top_level")
            })
            .expect("top level chunk");
        assert!(top_level_chunk.metadata.ancestry.is_empty());
    }

    #[test]
    fn markdown_queries_capture_headings_and_code_blocks() {
        let source = r#"
# Title

Setext Section
==============

Intro paragraph with **bold** text.

## Usage

```rust
fn main() {
    println!("hi");
}
```

- Item one
- Item two
"#;

        let mut parser = Parser::new();
        let ts_language =
            tree_sitter_language(ParseableLanguage::Markdown).expect("markdown language");
        parser
            .set_language(&ts_language)
            .expect("set markdown language");
        let tree = parser.parse(source, None).expect("parse markdown source");

        let chunks = chunk_with_queries(ParseableLanguage::Markdown, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Module
                && (chunk.text.contains("# Title")
                    || chunk.text.contains("## Usage")
                    || chunk.text.contains("Setext Section"))
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("```rust")
        }));
    }

    #[test]
    #[ignore] // TODO: Update test expectations to match actual query behavior
    fn typescript_queries_capture_core_constructs() {
        let source = r"
// Utility function
export const util = () => {
    // comment about util
    return 42;
};

export class Example {
    // leading comment for method
    constructor() {}

    // Another comment
    run = () => {
        return util();
    };
}

const compute = (x: number) => x * 2;
";

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::TypeScript).expect("ts language");
        parser.set_language(&ts_language).expect("set ts language");
        let tree = parser.parse(source, None).expect("parse ts source");

        let chunks = chunk_with_queries(ParseableLanguage::TypeScript, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("export const util")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Method && chunk.text.contains("run = () =>")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("const compute")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("class Example")
        }));

        let arrow_chunk = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Function && chunk.text.contains("export const util")
            })
            .expect("arrow chunk present");
        assert!(arrow_chunk.text.contains("return 42"));
        assert!(arrow_chunk.metadata.ancestry.is_empty());

        let method_chunk = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Method && chunk.text.contains("run = () =>")
            })
            .expect("method chunk");
        assert_eq!(method_chunk.metadata.ancestry, vec!["Example".to_string()]);
        assert!(
            method_chunk
                .metadata
                .leading_trivia
                .iter()
                .any(|text| text.contains("Another comment"))
        );
    }

    #[test]
    fn dart_queries_capture_core_constructs() {
        let source = r"
class Helper {
  Helper();
  void help() {}
}

mixin MyMixin {}

enum State { on, off }

void globalFunc() {}

const int MAX = 100;
";

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Dart).expect("dart language");
        parser
            .set_language(&ts_language)
            .expect("set dart language");
        let tree = parser.parse(source, None).expect("parse dart source");
        println!("Dart Tree: {}", tree.root_node().to_sexp());

        let chunks = chunk_with_queries(ParseableLanguage::Dart, ts_language, &tree, source)
            .expect("query execution") // This should fail if query is invalid
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("class Helper")
        }));

        // Check global function capture - note: name extraction might need work if it fails
        let global_func = chunks
            .iter()
            .find(|chunk| chunk.text.contains("globalFunc"));
        assert!(global_func.is_some(), "Should capture global function");
        if let Some(func) = global_func {
            println!("Global Func Metadata: {:?}", func.metadata);
        }
    }

    #[test]
    fn c_queries_capture_core_constructs() {
        let source = r#"
#include <stdio.h>

#define MAX_SIZE 1024

#define SQUARE(x) ((x) * (x))

typedef struct {
    int x;
    int y;
} Point;

struct Node {
    int value;
    struct Node* next;
};

enum Color {
    RED,
    GREEN,
    BLUE
};

union Data {
    int i;
    float f;
};

struct Node node_instance;
union Data data_instance;
enum Color color_instance;

void helper(int n) {
    printf("%d\n", n);
}

int compute(int a, int b) {
    return a + b;
}
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::C).expect("c language");
        parser.set_language(&ts_language).expect("set c language");
        let tree = parser.parse(source, None).expect("parse c source");

        let chunks = chunk_with_queries(ParseableLanguage::C, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        // Check function captures
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Function
                    && chunk.text.contains("void helper")),
            "Should capture helper function"
        );
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Function
                    && chunk.text.contains("int compute")),
            "Should capture compute function"
        );

        // Check struct capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("struct Node")),
            "Should capture struct Node"
        );

        // Check enum capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("enum Color")),
            "Should capture enum Color"
        );

        // Check union capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("union Data")),
            "Should capture union Data"
        );

        // Ensure bodyless type specifiers are not captured
        assert!(
            !chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("node_instance")),
            "Should not capture struct declarations without bodies"
        );
        assert!(
            !chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("data_instance")),
            "Should not capture union declarations without bodies"
        );
        assert!(
            !chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("color_instance")),
            "Should not capture enum declarations without bodies"
        );

        // Check macro function capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Function
                    && chunk.text.contains("SQUARE")),
            "Should capture SQUARE macro function"
        );

        // Verify top-level function has empty ancestry
        let compute_chunk = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Function && chunk.text.contains("int compute")
            })
            .expect("compute chunk present");
        assert!(compute_chunk.metadata.ancestry.is_empty());
    }

    #[test]
    fn cpp_queries_capture_core_constructs() {
        let source = r#"
#include <iostream>
#include <string>

#define MAX_ITEMS 256

namespace utils {

class Calculator {
public:
    Calculator() {}

    int add(int a, int b) {
        return a + b;
    }

    virtual int multiply(int a, int b) {
        return a * b;
    }
};

struct Point {
    double x;
    double y;
};

enum class Color {
    Red,
    Green,
    Blue
};

} // namespace utils

template <typename T>
T identity(T value) {
    return value;
}

void global_func() {
    std::cout << "hello" << std::endl;
}
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        // Check class capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("class Calculator")),
            "Should capture class Calculator"
        );

        // Check struct capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("struct Point")),
            "Should capture struct Point"
        );

        // Check enum class capture
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Class
                    && chunk.text.contains("enum class Color")),
            "Should capture enum class Color"
        );

        // Check global function
        assert!(
            chunks
                .iter()
                .any(|chunk| chunk.chunk_type == ChunkType::Function
                    && chunk.text.contains("void global_func")),
            "Should capture global_func"
        );

        // Verify top-level function has empty ancestry
        let global_func = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Function && chunk.text.contains("void global_func")
            })
            .expect("global_func chunk present");
        assert!(global_func.metadata.ancestry.is_empty());
    }

    #[test]
    fn cpp_queries_skip_function_body_declarations() {
        let source = r#"
int compute(int base) {
    int local = base + 1;
    if (auto value = base * 2; value > 0) {
        return value;
    }
    return local;
}
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("int compute")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("int local")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("auto value")
        }));
    }

    #[test]
    fn cpp_queries_skip_local_class_in_function() {
        let source = r#"
void build() {
    struct Temp {
        int value;
    };
    Temp temp{42};
}
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("void build")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("struct Temp")
        }));
    }

    #[test]
    fn cpp_queries_namespace_ancestry_for_class() {
        let source = r#"
namespace outer {
namespace inner {
class Widget {
public:
    void run() {}
};
} // namespace inner
} // namespace outer
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        let class_chunk = chunks
            .iter()
            .find(|chunk| {
                chunk.chunk_type == ChunkType::Class && chunk.text.contains("class Widget")
            })
            .expect("class chunk present");
        assert_eq!(
            class_chunk.metadata.ancestry,
            vec!["outer".to_string(), "inner".to_string()]
        );
    }

    #[test]
    fn cpp_queries_skip_defaulted_deleted_ctors() {
        let source = r#"
class Sample {
public:
    Sample() = default;
    Sample(const Sample&) = delete;
};
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("class Sample")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text
                && (chunk.text.contains("= default") || chunk.text.contains("= delete"))
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Method
                && (chunk.text.contains("= default") || chunk.text.contains("= delete"))
        }));
    }

    #[test]
    fn cpp_queries_drop_text_inside_types_and_functions() {
        let source = r#"
template <typename T>
class Box {
  using Value = T;
  T get() { T local{}; return local; }
};

using Outside = int;

template <typename T>
T make(T value) { T inner = value; return inner; }
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("class Box")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Function && chunk.text.contains("T make")
        }));
        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("using Outside")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("using Value")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("T local")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("T inner")
        }));
    }

    #[test]
    fn cpp_queries_no_declaration_dup_for_class() {
        let source = r#"
class DupCheck {
public:
    void run() {}
};
"#;

        let mut parser = Parser::new();
        let ts_language = tree_sitter_language(ParseableLanguage::Cpp).expect("cpp language");
        parser.set_language(&ts_language).expect("set cpp language");
        let tree = parser.parse(source, None).expect("parse cpp source");

        let chunks = chunk_with_queries(ParseableLanguage::Cpp, ts_language, &tree, source)
            .expect("query execution")
            .expect("query should be available");

        assert!(chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Class && chunk.text.contains("class DupCheck")
        }));
        assert!(!chunks.iter().any(|chunk| {
            chunk.chunk_type == ChunkType::Text && chunk.text.contains("class DupCheck")
        }));
    }
}