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

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

#[test]
fn test_basic_function() {
    let source = r#"func greet(name: String) -> String {
    return "Hello, \(name)!"
}"#;
    let units = parse(source, Language::Swift, "test.swift");

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

    let expected = r#"Function: greet
Signature: func greet(name: String) -> String {
Parameters: name
File: test test.swift
Code:
func greet(name: String) -> String {
    return "Hello, \(name)!"
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_function_with_doc_comment() {
    let source = r#"/// Calculates the sum of two numbers.
/// - Parameters:
///   - a: First number
///   - b: Second number
/// - Returns: Sum of a and b
func add(a: Int, b: Int) -> Int {
    return a + b
}"#;
    let units = parse(source, Language::Swift, "test.swift");

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

    let expected = r#"Function: add
Signature: func add(a: Int, b: Int) -> Int {
Description: Calculates the sum of two numbers. - Parameters: - a: First number - b: Second number - Returns: Sum of a and b
Parameters: a, b
File: test test.swift
Code:
func add(a: Int, b: Int) -> Int {
    return a + b
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_class_definition() {
    let source = r#"class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func greet() -> String {
        return "Hello, I'm \(name)"
    }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    // Class is extracted as a single chunk with all members inside
    let class = get_unit_by_name(&units, "Person").unwrap();
    let text = build_embedding_text(class);
    let expected = r#"Class: Person
Signature: class Person {
Variables: age, name
File: test test.swift
Code:
class Person {
    var name: String
    var age: Int

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func greet() -> String {
        return "Hello, I'm \(name)"
    }
}"#;
    assert_eq!(text, expected);

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

#[test]
fn test_struct_definition() {
    let source = r#"struct Point {
    var x: Double
    var y: Double

    func distance() -> Double {
        return sqrt(x*x + y*y)
    }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    // Struct is extracted as a single chunk with all members inside
    let class = get_unit_by_name(&units, "Point").unwrap();
    let text = build_embedding_text(class);
    let expected = r#"Class: Point
Signature: struct Point {
Calls: sqrt
Variables: x, y
File: test test.swift
Code:
struct Point {
    var x: Double
    var y: Double

    func distance() -> Double {
        return 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_async_function() {
    let source = r#"func fetchData(url: URL) async throws -> Data {
    let (data, _) = try await URLSession.shared.data(from: url)
    return data
}"#;
    let units = parse(source, Language::Swift, "test.swift");

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

    let expected = r#"Function: fetchData
Signature: func fetchData(url: URL) async throws -> Data {
Parameters: url
Calls: data
File: test test.swift
Code:
func fetchData(url: URL) async throws -> Data {
    let (data, _) = try await URLSession.shared.data(from: url)
    return data
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_function_with_throws() {
    let source = r#"func parse(data: String) throws -> Int {
    guard let result = Int(data) else {
        throw ParseError.invalidFormat
    }
    return result
}"#;
    let units = parse(source, Language::Swift, "test.swift");

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

    let expected = r#"Function: parse
Signature: func parse(data: String) throws -> Int {
Parameters: data
Calls: Int
File: test test.swift
Code:
func parse(data: String) throws -> Int {
    guard let result = Int(data) else {
        throw ParseError.invalidFormat
    }
    return result
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_protocol_definition() {
    let source = r#"protocol Drawable {
    func draw()
    var bounds: CGRect { get }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    let protocol = get_unit_by_name(&units, "Drawable").unwrap();
    let text = build_embedding_text(protocol);
    let expected = r#"Class: Drawable
Signature: protocol Drawable {
File: test test.swift
Code:
protocol Drawable {
    func draw()
    var bounds: CGRect { get }
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_enum_definition() {
    let source = r#"enum Status {
    case active
    case inactive
    case pending(reason: String)

    var description: String {
        switch self {
        case .active: return "Active"
        case .inactive: return "Inactive"
        case .pending(let reason): return "Pending: \(reason)"
        }
    }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    let enum_unit = get_unit_by_name(&units, "Status").unwrap();
    let text = build_embedding_text(enum_unit);
    let expected = r#"Class: Status
Signature: enum Status {
Variables: description
File: test test.swift
Code:
enum Status {
    case active
    case inactive
    case pending(reason: String)

    var description: String {
        switch self {
        case .active: return "Active"
        case .inactive: return "Inactive"
        case .pending(let reason): return "Pending: \(reason)"
        }
    }
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_extension() {
    let source = r#"extension String {
    func addExclamation() -> String {
        return self + "!"
    }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    // Extension is extracted as a single chunk with all methods inside
    let ext = get_unit_by_name(&units, "String").unwrap();
    let text = build_embedding_text(ext);
    let expected = r#"Class: String
Signature: extension String {
File: test test.swift
Code:
extension String {
    func addExclamation() -> String {
        return self + "!"
    }
}"#;
    assert_eq!(text, expected);

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

#[test]
fn test_generic_function() {
    let source = r#"func swap<T>(a: inout T, b: inout T) {
    let temp = a
    a = b
    b = temp
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    let func = get_unit_by_name(&units, "swap").unwrap();
    let text = build_embedding_text(func);
    let expected = r#"Function: swap
Signature: func swap<T>(a: inout T, b: inout T) {
Parameters: a, b
Variables: temp
File: test test.swift
Code:
func swap<T>(a: inout T, b: inout T) {
    let temp = a
    a = b
    b = temp
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_property_wrapper() {
    let source = r#"@propertyWrapper
struct Clamped<Value: Comparable> {
    var wrappedValue: Value {
        didSet { wrappedValue = min(max(wrappedValue, range.lowerBound), range.upperBound) }
    }
    let range: ClosedRange<Value>
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    let class = get_unit_by_name(&units, "Clamped").unwrap();
    let text = build_embedding_text(class);
    let expected = r#"Class: Clamped
Signature: @propertyWrapper
Calls: max, min
Variables: range, wrappedValue
File: test test.swift
Code:
@propertyWrapper
struct Clamped<Value: Comparable> {
    var wrappedValue: Value {
        didSet { wrappedValue = min(max(wrappedValue, range.lowerBound), range.upperBound) }
    }
    let range: ClosedRange<Value>
}"#;
    assert_eq!(text, expected);
}

#[test]
fn test_class_inheritance() {
    let source = r#"class Animal {
    func speak() -> String {
        return "..."
    }
}

class Dog: Animal {
    override func speak() -> String {
        return "Woof!"
    }
}"#;
    let units = parse(source, Language::Swift, "test.swift");

    // 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: class Animal {
File: test test.swift
Code:
class Animal {
    func speak() -> String {
        return "..."
    }
}"#
    );
    // 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: class Dog: Animal {
Extends: Animal
File: test test.swift
Code:
class Dog: Animal {
    override func speak() -> String {
        return "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: Swift imports frameworks (import Foundation), and types are used directly
    // without module prefix (DateFormatter() not Foundation.DateFormatter()).
    // Uses tracking doesn't apply to Swift's import pattern - similar to C# namespace imports.
    let source = r#"import Foundation

func formatDate(_ date: Date) -> String {
    let formatter = DateFormatter()
    formatter.dateStyle = .medium
    return formatter.string(from: date)
}
"#;
    let units = parse(source, Language::Swift, "test.swift");
    let func = get_unit_by_name(&units, "formatDate").unwrap();
    let text = build_embedding_text(func);

    // Swift doesn't use Module.Type pattern, so Uses won't be populated
    let expected = r#"Function: formatDate
Signature: func formatDate(_ date: Date) -> String {
Parameters: date
Calls: DateFormatter, string
Variables: formatter
File: test test.swift
Code:
func formatDate(_ date: Date) -> String {
    let formatter = DateFormatter()
    formatter.dateStyle = .medium
    return formatter.string(from: date)
}"#;
    assert_eq!(text, expected);
}