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
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
//! Tests for C# code extraction.

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

#[test]
fn test_basic_method() {
    let source = "public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}";
    let units = parse(source, Language::CSharp, "Calculator.cs");

    // Class is extracted as a single chunk with method inside
    let class_unit = get_unit_by_name(&units, "Calculator").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        "Class: Calculator
Signature: public class Calculator
File: calculator Calculator.cs
Code:
public class Calculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }
}"
    );

    // 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_xml_doc() {
    let source = r#"public class Math
{
    /// <summary>
    /// Calculates the sum of two numbers.
    /// </summary>
    /// <param name="a">First number</param>
    /// <param name="b">Second number</param>
    /// <returns>Sum of a and b</returns>
    public int Add(int a, int b)
    {
        return a + b;
    }
}"#;
    let units = parse(source, Language::CSharp, "Math.cs");

    // Class is extracted as a single chunk with XML doc and method inside
    let class_unit = get_unit_by_name(&units, "Math").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        r#"Class: Math
Signature: public class Math
File: math Math.cs
Code:
public class Math
{
    /// <summary>
    /// Calculates the sum of two numbers.
    /// </summary>
    /// <param name="a">First number</param>
    /// <param name="b">Second number</param>
    /// <returns>Sum of a and b</returns>
    public int Add(int a, int b)
    {
        return a + b;
    }
}"#
    );

    // 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_class_definition() {
    let source = r#"public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public string Greet()
    {
        return $"Hello, I'm {Name}";
    }
}"#;
    let units = parse(source, Language::CSharp, "Person.cs");

    // Class is extracted as a single chunk with all members inside
    let class_unit = get_unit_by_name(&units, "Person").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        r#"Class: Person
Signature: public class Person
File: person Person.cs
Code:
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }

    public string Greet()
    {
        return $"Hello, I'm {Name}";
    }
}"#
    );

    // Verify NO separate method/constructor units exist
    assert!(
        get_unit_by_name(&units, "Greet").is_none(),
        "Methods should not be extracted separately from classes"
    );
    // Constructor has same name as class, so we check there's only 1 unit with name "Person"
    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_async_method() {
    let source = "public class DataService
{
    public async Task<string> FetchDataAsync(string url)
    {
        using var client = new HttpClient();
        return await client.GetStringAsync(url);
    }
}";
    let units = parse(source, Language::CSharp, "DataService.cs");

    // Class is extracted as a single chunk with async method inside
    let class_unit = get_unit_by_name(&units, "DataService").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        "Class: DataService
Signature: public class DataService
Calls: new
Variables: client
File: data service DataService.cs
Code:
public class DataService
{
    public async Task<string> FetchDataAsync(string url)
    {
        using var client = new HttpClient();
        return await client.GetStringAsync(url);
    }
}"
    );

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

#[test]
fn test_static_method() {
    let source = "public static class Utils
{
    public static string Format(string template, params object[] args)
    {
        return string.Format(template, args);
    }
}";
    let units = parse(source, Language::CSharp, "Utils.cs");

    // Static class is extracted as a single chunk with static method inside
    let class_unit = get_unit_by_name(&units, "Utils").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        "Class: Utils
Signature: public static class Utils
File: utils Utils.cs
Code:
public static class Utils
{
    public static string Format(string template, params object[] args)
    {
        return string.Format(template, args);
    }
}"
    );

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

#[test]
fn test_interface() {
    let source = "public interface IDrawable
{
    void Draw();
    Rectangle GetBounds();
}";
    let units = parse(source, Language::CSharp, "IDrawable.cs");

    // Interface is extracted as a single chunk with all method signatures inside
    let interface_unit = get_unit_by_name(&units, "IDrawable").unwrap();
    let interface_text = build_embedding_text(interface_unit);
    assert_eq!(
        interface_text,
        "Class: IDrawable
Signature: public interface IDrawable
File: idrawable IDrawable.cs
Code:
public interface IDrawable
{
    void Draw();
    Rectangle GetBounds();
}"
    );

    // Verify NO separate method units exist
    assert!(
        get_unit_by_name(&units, "Draw").is_none(),
        "Interface methods should not be extracted separately"
    );
    assert!(
        get_unit_by_name(&units, "GetBounds").is_none(),
        "Interface methods should not be extracted separately"
    );
}

#[test]
fn test_generic_method() {
    let source = "public class Container<T>
{
    private T _value;

    public T GetValue()
    {
        return _value;
    }

    public void SetValue(T value)
    {
        _value = value;
    }
}";
    let units = parse(source, Language::CSharp, "Container.cs");

    // Generic class is extracted as a single chunk with all methods inside
    let class_unit = get_unit_by_name(&units, "Container").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        "Class: Container
Signature: public class Container<T>
Variables: _value
File: container Container.cs
Code:
public class Container<T>
{
    private T _value;

    public T GetValue()
    {
        return _value;
    }

    public void SetValue(T value)
    {
        _value = value;
    }
}"
    );

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

#[test]
fn test_extension_method() {
    let source = r#"public static class StringExtensions
{
    public static string AddExclamation(this string str)
    {
        return str + "!";
    }
}"#;
    let units = parse(source, Language::CSharp, "StringExtensions.cs");

    // Extension class is extracted as a single chunk with extension method inside
    let class_unit = get_unit_by_name(&units, "StringExtensions").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        r#"Class: StringExtensions
Signature: public static class StringExtensions
File: string extensions StringExtensions.cs
Code:
public static class StringExtensions
{
    public static string AddExclamation(this string str)
    {
        return str + "!";
    }
}"#
    );

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

#[test]
fn test_property_accessor() {
    let source = "public class Circle
{
    private double _radius;

    public double Radius
    {
        get => _radius;
        set => _radius = value > 0 ? value : 0;
    }

    public double Area => Math.PI * _radius * _radius;
}";
    let units = parse(source, Language::CSharp, "Circle.cs");

    // Class with properties is extracted as a single chunk
    let class_unit = get_unit_by_name(&units, "Circle").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        "Class: Circle
Signature: public class Circle
Variables: _radius
File: circle Circle.cs
Code:
public class Circle
{
    private double _radius;

    public double Radius
    {
        get => _radius;
        set => _radius = value > 0 ? value : 0;
    }

    public double Area => Math.PI * _radius * _radius;
}"
    );
}

#[test]
fn test_record() {
    let source = "public record Person(string Name, int Age);

public record Point
{
    public double X { get; init; }
    public double Y { get; init; }
}";
    let units = parse(source, Language::CSharp, "Records.cs");

    // Records are extracted as RawCode blocks
    assert_eq!(units.len(), 1);
    let unit = &units[0];
    let text = build_embedding_text(unit);
    assert_eq!(
        text,
        "public record Person(string Name, int Age);

public record Point
{
    public double X { get; init; }
    public double Y { get; init; }
}"
    );
}

#[test]
fn test_linq_expression() {
    let source = r#"public class QueryService
{
    public IEnumerable<string> FilterNames(IEnumerable<string> names)
    {
        return names.Where(n => n.StartsWith("A"))
                    .OrderBy(n => n)
                    .ToList();
    }
}"#;
    let units = parse(source, Language::CSharp, "QueryService.cs");

    // Class with LINQ method is extracted as a single chunk
    let class_unit = get_unit_by_name(&units, "QueryService").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        r#"Class: QueryService
Signature: public class QueryService
File: query service QueryService.cs
Code:
public class QueryService
{
    public IEnumerable<string> FilterNames(IEnumerable<string> names)
    {
        return names.Where(n => n.StartsWith("A"))
                    .OrderBy(n => n)
                    .ToList();
    }
}"#
    );

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

#[test]
fn test_class_inheritance() {
    let source = r#"public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("...");
    }
}

public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}"#;
    let units = parse(source, Language::CSharp, "Animals.cs");

    // Animal class is extracted as a single chunk
    let animal = get_unit_by_name(&units, "Animal").unwrap();
    let animal_text = build_embedding_text(animal);
    assert_eq!(
        animal_text,
        r#"Class: Animal
Signature: public class Animal
File: animals Animals.cs
Code:
public class Animal
{
    public virtual void Speak()
    {
        Console.WriteLine("...");
    }
}"#
    );
    // Animal has no parent
    assert!(!animal_text.contains("Extends:"));

    // Dog class is extracted as a single chunk with inheritance info
    let dog = get_unit_by_name(&units, "Dog").unwrap();
    let dog_text = build_embedding_text(dog);
    assert_eq!(
        dog_text,
        r#"Class: Dog
Signature: public class Dog : Animal
Extends: Animal
File: animals Animals.cs
Code:
public class Dog : Animal
{
    public override void Speak()
    {
        Console.WriteLine("Woof!");
    }
}"#
    );

    // 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_function_with_imports() {
    // Note: C# uses namespace imports (using System.Collections.Generic;), not class imports.
    // This means the Uses field won't be populated for types from imported namespaces
    // since the import extracts "Generic" (the namespace) but the code uses "List" (the class).
    // However, object creation is still tracked - the class name is extracted from `new ClassName()`.
    let source = r#"using MyApp.Models;

public class Factory
{
    public User CreateUser()
    {
        return new User();
    }
}"#;
    let units = parse(source, Language::CSharp, "Factory.cs");

    // Class is extracted as a single chunk
    let class_unit = get_unit_by_name(&units, "Factory").unwrap();
    let class_text = build_embedding_text(class_unit);
    assert_eq!(
        class_text,
        r#"Class: Factory
Signature: public class Factory
Calls: new
File: factory Factory.cs
Code:
public class Factory
{
    public User CreateUser()
    {
        return new User();
    }
}"#
    );

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