codesearch 0.1.15

A fast, intelligent CLI tool with multiple search modes (regex, fuzzy, semantic), code analysis, and dead code detection for popular programming languages
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
//! Fuzzy Search Implementation
//!
//! Provides fuzzy matching and relevance scoring for search results.

use crate::types::{Match, SearchResult};
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::Path;

/// Search within a single file using parallel processing
#[allow(clippy::too_many_arguments)]
pub fn search_in_file_parallel(
    file_path: &Path,
    matcher: &super::SearchMatcher,
    fuzzy: bool,
    fuzzy_threshold: f64,
    query: &str,
    max_results: usize,
    rank: bool,
    bm25_stats: Option<&super::bm25::Bm25Stats>,
    context_filter: Option<&crate::types::ContextFilter>,
    declaration_filter: Option<&crate::types::DeclarationFilter>,
    context: usize,
) -> Result<Vec<SearchResult>, Box<dyn std::error::Error>> {
    let file = fs::File::open(file_path)?;
    let reader = BufReader::new(file);
    let mut results = Vec::new();
    let mut term_freq = 0usize;
    let fuzzy_matcher = SkimMatcherV2::default();

    // Read all lines so we can extract before/after context when requested.
    let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?;

    for (idx, line) in lines.iter().enumerate() {
        let line_number = idx + 1;

        if results.len() >= max_results {
            break;
        }

        // Apply structural context filter before matching
        if let Some(filter) = context_filter {
            let ctx = super::context::classify_line(line);
            if !super::context::should_include(ctx, Some(filter)) {
                continue;
            }
        }

        if fuzzy {
            if let Some((score, indices)) = fuzzy_matcher.fuzzy_indices(line, query) {
                if score as f64 >= fuzzy_threshold {
                    term_freq += 1;
                    // Collect characters once to avoid repeated O(n) indexing
                    let line_chars: Vec<char> = line.chars().collect();
                    let mut matches = Vec::new();

                    for &idx in &indices {
                        if matches.is_empty()
                            || idx >= matches.last().map(|m: &Match| m.end).unwrap_or(0)
                        {
                            // Safe character access using the pre-collected vector
                            let text = if idx < line_chars.len() {
                                line_chars[idx].to_string()
                            } else {
                                String::new()
                            };

                            matches.push(Match {
                                start: idx,
                                end: idx + 1,
                                text,
                            });
                        }
                    }

                    let (score_val, relevance) = if rank {
                        let s = calculate_relevance_score(
                            line,
                            query,
                            line_number,
                            file_path,
                            true,
                            Some(score),
                        );
                        let r = relevance_label(s);
                        (s, r)
                    } else {
                        (score as f64, "Medium".to_string())
                    };

                    if let Some(filter) = declaration_filter {
                        if !super::declarations::should_include_declaration(line, Some(filter)) {
                            continue;
                        }
                    }

                    let (before_context, after_context) = extract_context(&lines, idx, context);
                    results.push(SearchResult {
                        file: file_path.to_string_lossy().into_owned(),
                        line_number,
                        content: line.clone(),
                        matches,
                        score: score_val,
                        relevance,
                        before_context,
                        after_context,
                    });
                }
            }
        } else if let Some(mat) = matcher.find(line) {
            term_freq += 1;
            let (score_val, relevance) = if rank {
                let s = calculate_relevance_score(line, query, line_number, file_path, false, None);
                let r = relevance_label(s);
                (s, r)
            } else {
                (50.0, "Medium".to_string())
            };

            if let Some(filter) = declaration_filter {
                if !super::declarations::should_include_declaration(line, Some(filter)) {
                    continue;
                }
            }

            let matches = vec![Match {
                start: mat.start,
                end: mat.end,
                text: line[mat.clone()].to_string(),
            }];

            let (before_context, after_context) = extract_context(&lines, idx, context);
            results.push(SearchResult {
                file: file_path.to_string_lossy().into_owned(),
                line_number,
                content: line.clone(),
                matches,
                score: score_val,
                relevance,
                before_context,
                after_context,
            });
        }
    }

    // Apply BM25 document-level scoring if stats were provided
    if let Some(stats) = bm25_stats {
        if !results.is_empty() {
            let bm25_score = stats.normalized_score(term_freq, lines.len());
            let relevance = relevance_label(bm25_score);
            for r in &mut results {
                r.score = bm25_score;
                r.relevance = relevance.clone();
            }
        }
    }

    Ok(results)
}

/// Extract `context` lines before and after the line at `idx`.
fn extract_context(lines: &[String], idx: usize, context: usize) -> (Vec<String>, Vec<String>) {
    if context == 0 {
        return (Vec::new(), Vec::new());
    }
    let before_start = idx.saturating_sub(context);
    let before = lines[before_start..idx].to_vec();
    let after_start = (idx + 1).min(lines.len());
    let after_end = (idx + 1 + context).min(lines.len());
    let after = lines[after_start..after_end].to_vec();
    (before, after)
}

fn relevance_label(score: f64) -> String {
    if score >= 80.0 {
        "Very High".to_string()
    } else if score >= 60.0 {
        "High".to_string()
    } else if score >= 40.0 {
        "Medium".to_string()
    } else {
        "Low".to_string()
    }
}

/// Calculate relevance score for a search result
pub fn calculate_relevance_score(
    line: &str,
    query: &str,
    line_number: usize,
    file_path: &Path,
    _is_fuzzy: bool,
    fuzzy_score: Option<i64>,
) -> f64 {
    let mut score = 50.0;

    if line.contains(query) {
        score += 30.0;
    }

    if let Some(fs) = fuzzy_score {
        score += (fs as f64) / 10.0;
    }

    if line_number < 100 {
        score += 5.0;
    }

    if let Some(ext) = file_path.extension().and_then(|e| e.to_str()) {
        match ext {
            "rs" | "py" | "js" | "ts" => score += 10.0,
            "md" | "txt" => score += 5.0,
            _ => {}
        }
    }

    // Test-file dampening: lower score for matches in test / mock / fixture files
    let path_str = file_path.to_string_lossy();
    let is_test_file = path_str.contains("/test")
        || path_str.contains("/spec/")
        || path_str.contains("/mock")
        || path_str.contains("/fixture")
        || path_str.contains("_test.")
        || path_str.contains("_spec.")
        || path_str.contains("test_");
    if is_test_file {
        score *= 0.7;
    }

    // Noise penalty for data blobs / minified files: very long lines
    if line.len() > 500 {
        score *= 0.5;
    }

    let definition_patterns = [
        "fn ",
        "def ",
        "function ",
        "class ",
        "struct ",
        "impl ",
        "trait ",
    ];
    for pattern in &definition_patterns {
        if line.contains(pattern) {
            score += 15.0;
            break;
        }
    }

    score.clamp(0.0, 100.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_fuzzy_search_performance() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("test.rs");

        // Create a file with multiple lines
        let content = "fn test_function() {\n    let test_var = 123;\n    println!(\"test\");\n}\n";
        fs::write(&file_path, content).unwrap();

        let matcher = crate::search::SearchMatcher::Regex(std::sync::Arc::new(
            regex::Regex::new(r"test").unwrap(),
        ));

        // Test fuzzy search
        let result = search_in_file_parallel(
            &file_path, &matcher, true, 0.0, "test", 100, false, None, None, None, 0,
        );

        assert!(result.is_ok());
        let results = result.unwrap();
        assert!(!results.is_empty());
    }

    #[test]
    fn test_fuzzy_indices_optimization() {
        // This test verifies that the character collection optimization works correctly
        let line = "fn test_function() {";
        let query = "test";

        let matcher = SkimMatcherV2::default();
        if let Some((_score, indices)) = matcher.fuzzy_indices(line, query) {
            // Collect characters once (as done in the optimized version)
            let line_chars: Vec<char> = line.chars().collect();

            // Verify all indices are valid
            for &idx in &indices {
                assert!(
                    idx < line_chars.len(),
                    "Index {} should be less than line length {}",
                    idx,
                    line_chars.len()
                );
                // Verify we can access the character without panicking
                let _c = line_chars[idx];
            }
        }
    }

    #[test]
    fn test_test_file_dampening() {
        let src_score = calculate_relevance_score(
            "fn helper() {}",
            "helper",
            1,
            std::path::Path::new("src/main.rs"),
            false,
            None,
        );
        let test_score = calculate_relevance_score(
            "fn helper() {}",
            "helper",
            1,
            std::path::Path::new("tests/test_main.rs"),
            false,
            None,
        );
        assert!(
            test_score < src_score,
            "Test file score ({}) should be lower than src file score ({})",
            test_score,
            src_score
        );
    }

    #[test]
    fn test_noise_penalty() {
        let normal_line = "fn helper() { println!(\"hello\"); }";
        let minified_line = "a".repeat(600);

        let normal_score = calculate_relevance_score(
            normal_line,
            "helper",
            1,
            std::path::Path::new("src/main.rs"),
            false,
            None,
        );
        let noisy_score = calculate_relevance_score(
            &minified_line,
            "a",
            1,
            std::path::Path::new("dist/bundle.js"),
            false,
            None,
        );
        assert!(
            noisy_score < normal_score,
            "Noisy line score ({}) should be lower than normal line score ({})",
            noisy_score,
            normal_score
        );
    }

    #[test]
    fn test_extract_context_middle_of_file() {
        let lines: Vec<String> = (0..10).map(|i| format!("line{i}")).collect();
        let (before, after) = extract_context(&lines, 5, 2);
        assert_eq!(before, vec!["line3", "line4"]);
        assert_eq!(after, vec!["line6", "line7"]);
    }

    #[test]
    fn test_extract_context_at_start() {
        let lines: Vec<String> = (0..5).map(|i| format!("line{i}")).collect();
        let (before, after) = extract_context(&lines, 0, 3);
        assert!(before.is_empty(), "no context before first line");
        assert_eq!(after, vec!["line1", "line2", "line3"]);
    }

    #[test]
    fn test_extract_context_at_end() {
        let lines: Vec<String> = (0..5).map(|i| format!("line{i}")).collect();
        let (before, after) = extract_context(&lines, 4, 3);
        assert_eq!(before, vec!["line1", "line2", "line3"]);
        assert_eq!(after, Vec::<String>::new(), "no context after last line");
    }

    #[test]
    fn test_extract_context_zero() {
        let lines: Vec<String> = vec!["a".into(), "b".into(), "c".into()];
        let (before, after) = extract_context(&lines, 1, 0);
        assert!(before.is_empty());
        assert!(after.is_empty());
    }

    #[test]
    fn test_extract_context_context_exceeds_file() {
        let lines: Vec<String> = vec!["a".into(), "b".into(), "c".into()];
        let (before, after) = extract_context(&lines, 1, 10);
        assert_eq!(before, vec!["a"]);
        assert_eq!(after, vec!["c"]);
    }

    #[test]
    fn test_search_with_context_lines() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("ctx.rs");
        let content = "line zero\nfn target() {\n    body\n}\nline four\n";
        fs::write(&file_path, content).unwrap();

        let matcher = crate::search::SearchMatcher::Regex(std::sync::Arc::new(
            regex::Regex::new(r"target").unwrap(),
        ));

        let results = search_in_file_parallel(
            &file_path, &matcher, false, 0.6, "target", 10, false, None, None, None, 1,
        )
        .unwrap();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].before_context, vec!["line zero"]);
        assert_eq!(results[0].after_context, vec!["    body"]);
    }

    #[test]
    fn test_search_without_context() {
        let dir = tempdir().unwrap();
        let file_path = dir.path().join("noctx.rs");
        let content = "before\nfn target() {}\nafter\n";
        fs::write(&file_path, content).unwrap();

        let matcher = crate::search::SearchMatcher::Regex(std::sync::Arc::new(
            regex::Regex::new(r"target").unwrap(),
        ));

        let results = search_in_file_parallel(
            &file_path, &matcher, false, 0.6, "target", 10, false, None, None, None, 0,
        )
        .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].before_context.is_empty());
        assert!(results[0].after_context.is_empty());
    }
}