colgrep 1.3.0

Semantic code search powered by ColBERT
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
//! Tests for C++ code extraction.

use super::common::*;
use crate::embed::build_embedding_text;
use crate::parser::Language;

#[test]
fn test_basic_function() {
    let source = r#"int add(int a, int b) {
    return a + b;
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    let unit = get_unit_by_name(&units, "add").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Function: add
Signature: int add(int a, int b) {
Parameters: a, b
Returns: int
File: test test.cpp
Code:
int add(int a, int b) {
    return a + b;
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_class_definition() {
    let source = r#"class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }

private:
    int value;
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Class is extracted as a single chunk with method inside
    assert_eq!(units.len(), 1);

    let class = get_unit_by_name(&units, "Calculator").unwrap();
    let class_text = build_embedding_text(class);
    let expected_class = r#"Class: Calculator
Signature: class Calculator {
File: test test.cpp
Code:
class Calculator {
public:
    int add(int a, int b) {
        return a + b;
    }

private:
    int value;
};"#;
    assert_eq!(class_text, expected_class);

    // Verify NO separate method unit exists
    assert!(
        get_unit_by_name(&units, "add").is_none(),
        "Methods should not be extracted separately from classes"
    );
}

#[test]
fn test_method_with_doxygen() {
    let source = r#"class Math {
public:
    /**
     * @brief Calculates the sum of two numbers.
     * @param a First number
     * @param b Second number
     * @return Sum of a and b
     */
    int add(int a, int b) {
        return a + b;
    }
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Class is extracted as a single chunk with doxygen comment and method inside
    assert_eq!(units.len(), 1);

    let class_unit = get_unit_by_name(&units, "Math").unwrap();
    let class_text = build_embedding_text(class_unit);
    let expected = r#"Class: Math
Signature: class Math {
File: test test.cpp
Code:
class Math {
public:
    /**
     * @brief Calculates the sum of two numbers.
     * @param a First number
     * @param b Second number
     * @return Sum of a and b
     */
    int add(int a, int b) {
        return a + b;
    }
};"#;
    assert_eq!(class_text, expected);

    // Verify NO separate method unit exists
    assert!(
        get_unit_by_name(&units, "add").is_none(),
        "Methods should not be extracted separately from classes"
    );
}

#[test]
fn test_template_function() {
    let source = r#"template<typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    let unit = get_unit_by_name(&units, "max").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Function: max
Signature: T max(T a, T b) {
Parameters: a, b
Returns: T
File: test test.cpp
Code:
T max(T a, T b) {
    return (a > b) ? a : b;
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_namespace_function() {
    let source = r#"namespace utils {
    int helper(int x) {
        return x * 2;
    }
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    let unit = get_unit_by_name(&units, "helper").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Function: helper
Signature: int helper(int x) {
Parameters: x
Returns: int
File: test test.cpp
Code:
    int helper(int x) {
        return x * 2;
    }"#;
    assert_eq!(text, expected);
}

#[test]
fn test_constructor() {
    let source = r#"class Person {
public:
    Person(const std::string& name, int age)
        : name_(name), age_(age) {}

private:
    std::string name_;
    int age_;
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Class is extracted as a single chunk with constructor inside
    assert_eq!(units.len(), 1);

    let class_unit = get_unit_by_name(&units, "Person").unwrap();
    let class_text = build_embedding_text(class_unit);
    let expected = r#"Class: Person
Signature: class Person {
File: test test.cpp
Code:
class Person {
public:
    Person(const std::string& name, int age)
        : name_(name), age_(age) {}

private:
    std::string name_;
    int age_;
};"#;
    assert_eq!(class_text, expected);

    // Verify NO separate constructor unit exists (constructor has same name as class)
    let person_units: Vec<_> = units.iter().filter(|u| u.name == "Person").collect();
    assert_eq!(
        person_units.len(),
        1,
        "Should only have 1 Person unit (the class), not separate constructor"
    );
}

#[test]
fn test_virtual_method() {
    let source = r#"class Shape {
public:
    virtual double area() const = 0;
    virtual ~Shape() = default;
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Class is extracted as a single chunk with virtual methods inside
    assert_eq!(units.len(), 1);

    let class_unit = get_unit_by_name(&units, "Shape").unwrap();
    let class_text = build_embedding_text(class_unit);
    let expected = r#"Class: Shape
Signature: class Shape {
File: test test.cpp
Code:
class Shape {
public:
    virtual double area() const = 0;
    virtual ~Shape() = default;
};"#;
    assert_eq!(class_text, expected);

    // Verify NO separate method/destructor units exist
    assert!(
        get_unit_by_name(&units, "~Shape").is_none(),
        "Destructors should not be extracted separately from classes"
    );
    assert!(
        get_unit_by_name(&units, "area").is_none(),
        "Methods should not be extracted separately from classes"
    );
}

#[test]
fn test_function_with_stl() {
    let source = r#"#include <vector>
#include <algorithm>

std::vector<int> filter_positive(const std::vector<int>& nums) {
    std::vector<int> result;
    std::copy_if(nums.begin(), nums.end(), std::back_inserter(result),
                 [](int x) { return x > 0; });
    return result;
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    assert_eq!(units.len(), 3);

    let unit = get_unit_by_name(&units, "filter_positive").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Function: filter_positive
Signature: std::vector<int> filter_positive(const std::vector<int>& nums) {
Parameters: nums
Returns: std::vector<int>
Calls: back_inserter, begin, copy_if, end
Variables: result
File: test test.cpp
Code:
std::vector<int> filter_positive(const std::vector<int>& nums) {
    std::vector<int> result;
    std::copy_if(nums.begin(), nums.end(), std::back_inserter(result),
                 [](int x) { return x > 0; });
    return result;
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_struct_with_methods() {
    let source = r#"struct Point {
    double x, y;

    double distance() const {
        return std::sqrt(x*x + y*y);
    }
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Struct is extracted as a single chunk with method inside
    assert_eq!(units.len(), 1);

    let unit = get_unit_by_name(&units, "Point").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Class: Point
Signature: struct Point {
Calls: sqrt
File: test test.cpp
Code:
struct Point {
    double x, y;

    double distance() const {
        return std::sqrt(x*x + y*y);
    }
};"#;
    assert_eq!(text, expected);

    // Verify NO separate method unit exists
    assert!(
        get_unit_by_name(&units, "distance").is_none(),
        "Methods should not be extracted separately from structs"
    );
}

#[test]
fn test_operator_overload() {
    let source = r#"class Vector {
public:
    Vector operator+(const Vector& other) const {
        return Vector(x + other.x, y + other.y);
    }

private:
    double x, y;
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Class is extracted as a single chunk with operator overload inside
    assert_eq!(units.len(), 1);

    let class_unit = get_unit_by_name(&units, "Vector").unwrap();
    let class_text = build_embedding_text(class_unit);
    let expected = r#"Class: Vector
Signature: class Vector {
Calls: Vector
File: test test.cpp
Code:
class Vector {
public:
    Vector operator+(const Vector& other) const {
        return Vector(x + other.x, y + other.y);
    }

private:
    double x, y;
};"#;
    assert_eq!(class_text, expected);

    // Verify NO separate operator method unit exists
    assert!(
        get_unit_by_name(&units, "operator+").is_none(),
        "Operator overloads should not be extracted separately from classes"
    );
}

#[test]
fn test_constexpr_function() {
    let source = r#"constexpr int factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    assert_eq!(units.len(), 1);

    let unit = get_unit_by_name(&units, "factorial").unwrap();
    let text = build_embedding_text(unit);
    let expected = r#"Function: factorial
Signature: constexpr int factorial(int n) {
Parameters: n
Returns: int
Calls: factorial
File: test test.cpp
Code:
constexpr int factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_class_inheritance() {
    let source = r#"class Animal {
public:
    virtual void speak() {
        std::cout << "..." << std::endl;
    }
};

class Dog : public Animal {
public:
    void speak() override {
        std::cout << "Woof!" << std::endl;
    }
};"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    // Both classes are extracted as single chunks
    assert_eq!(units.len(), 2);

    let animal = get_unit_by_name(&units, "Animal").unwrap();
    let animal_text = build_embedding_text(animal);
    let expected_animal = r#"Class: Animal
Signature: class Animal {
File: test test.cpp
Code:
class Animal {
public:
    virtual void speak() {
        std::cout << "..." << std::endl;
    }
};"#;
    assert_eq!(animal_text, expected_animal);
    // Animal has no parent
    assert!(!animal_text.contains("Extends:"));

    let dog = get_unit_by_name(&units, "Dog").unwrap();
    let dog_text = build_embedding_text(dog);
    let expected_dog = r#"Class: Dog
Signature: class Dog : public Animal {
Extends: Animal
File: test test.cpp
Code:
class Dog : public Animal {
public:
    void speak() override {
        std::cout << "Woof!" << std::endl;
    }
};"#;
    assert_eq!(dog_text, expected_dog);

    // Verify NO separate method units exist
    assert!(
        get_unit_by_name(&units, "speak").is_none(),
        "Methods should not be extracted separately from classes"
    );
}

#[test]
fn test_uses_not_applicable() {
    // Note: C++ uses #include directives and namespace qualifiers (std::).
    // Unlike module imports, std:: is always available - it's a namespace qualifier,
    // not a module that needs to be tracked. The Uses field doesn't apply to C++ code.
    let source = r#"#include <iostream>
#include <vector>

void print_values(const std::vector<int>& values) {
    for (int v : values) {
        std::cout << v << std::endl;
    }
}"#;
    let units = parse(source, Language::Cpp, "test.cpp");

    let func = get_unit_by_name(&units, "print_values").unwrap();
    let text = build_embedding_text(func);

    // No Uses field - std:: is a namespace qualifier, not a tracked module import
    let expected = r#"Function: print_values
Signature: void print_values(const std::vector<int>& values) {
Parameters: values
Returns: void
File: test test.cpp
Code:
void print_values(const std::vector<int>& values) {
    for (int v : values) {
        std::cout << v << std::endl;
    }
}"#;
    assert_eq!(text, expected);
}