krait-cli 0.1.2

Code intelligence CLI for AI agents
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
use std::path::{Path, PathBuf};

use anyhow::Context as _;
use rayon::prelude::*;
use regex::{Regex, RegexBuilder};
use serde::Serialize;

use crate::detect::Language;

/// Bytes to scan at the start of a file for binary detection.
/// Smaller than read.rs's `BINARY_SCAN_SIZE` intentionally — search is a hot path.
const BINARY_SCAN_BYTES: usize = 512;

/// A single match found during search.
#[derive(Debug, Serialize)]
pub struct SearchMatch {
    pub path: String,
    pub line: u32,
    pub column: u32,
    pub preview: String,
    pub context_before: Vec<String>,
    pub context_after: Vec<String>,
}

/// Aggregated search results.
#[derive(Debug, Serialize)]
pub struct SearchOutput {
    pub matches: Vec<SearchMatch>,
    pub total_matches: usize,
    pub files_searched: usize,
    pub files_with_matches: usize,
    pub truncated: bool,
}

/// Options controlling search behaviour.
#[allow(clippy::struct_excessive_bools)]
pub struct SearchOptions {
    pub pattern: String,
    pub path: Option<PathBuf>,
    pub ignore_case: bool,
    pub word: bool,
    pub literal: bool,
    pub context: u32,
    pub files_only: bool,
    pub lang_filter: Option<String>,
    pub max_matches: usize,
}

/// Run the search and return aggregated results.
///
/// # Errors
/// Returns an error if the regex pattern is invalid or file walking fails.
pub fn run(opts: &SearchOptions, project_root: &Path) -> anyhow::Result<SearchOutput> {
    let re = build_regex(opts)?;
    let search_root = opts.path.as_deref().unwrap_or(project_root);
    let files = collect_files(search_root, opts)?;
    let files_searched = files.len();

    // Parallel search: each file returns a Vec<SearchMatch>
    let file_results: Vec<Vec<SearchMatch>> = files
        .par_iter()
        .map(|path| search_file(path, search_root, project_root, &re, opts))
        .collect();

    // Flatten, sort, truncate
    let mut matches: Vec<SearchMatch> = Vec::new();
    let mut files_with_matches: usize = 0;
    let mut truncated = false;

    for file_matches in file_results {
        if file_matches.is_empty() {
            continue;
        }
        files_with_matches += 1;
        for m in file_matches {
            if matches.len() >= opts.max_matches {
                truncated = true;
                break;
            }
            matches.push(m);
        }
        if truncated {
            break;
        }
    }

    let total_matches = matches.len();
    Ok(SearchOutput {
        matches,
        total_matches,
        files_searched,
        files_with_matches,
        truncated,
    })
}

fn build_regex(opts: &SearchOptions) -> anyhow::Result<Regex> {
    let pat = if opts.literal {
        regex::escape(&opts.pattern)
    } else {
        normalize_grep_escapes(&opts.pattern)
    };

    let pat = if opts.word {
        format!(r"\b{pat}\b")
    } else {
        pat
    };

    RegexBuilder::new(&pat)
        .case_insensitive(opts.ignore_case)
        .build()
        .with_context(|| format!("invalid regex pattern: {}", opts.pattern))
}

/// Convert grep/BRE-style escape sequences to Rust regex (ERE) syntax.
///
/// Agents trained on Unix tools often emit `\|` for alternation, `\+` for
/// one-or-more, etc. In Rust regex these are invalid or literal — normalize
/// them silently so patterns just work.
fn normalize_grep_escapes(pattern: &str) -> String {
    let mut out = String::with_capacity(pattern.len());
    let mut chars = pattern.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.peek() {
                Some('|') => {
                    out.push('|');
                    chars.next();
                }
                Some('+') => {
                    out.push('+');
                    chars.next();
                }
                Some('?') => {
                    out.push('?');
                    chars.next();
                }
                Some('(') => {
                    out.push('(');
                    chars.next();
                }
                Some(')') => {
                    out.push(')');
                    chars.next();
                }
                _ => out.push(c),
            }
        } else {
            out.push(c);
        }
    }
    out
}

fn collect_files(root: &Path, opts: &SearchOptions) -> anyhow::Result<Vec<PathBuf>> {
    let extensions: Option<&[&str]> = opts.lang_filter.as_deref().map(extensions_for_lang);

    let mut builder = ignore::WalkBuilder::new(root);
    builder
        .hidden(true)
        .git_ignore(true)
        .git_global(false)
        .git_exclude(true);

    let mut files: Vec<PathBuf> = Vec::new();
    for entry in builder.build() {
        let entry = entry?;
        if !entry.file_type().is_some_and(|ft| ft.is_file()) {
            continue;
        }
        let path = entry.path();
        if let Some(exts) = extensions {
            match path.extension().and_then(|e| e.to_str()) {
                Some(ext) if exts.contains(&ext) => {}
                _ => continue,
            }
        }
        files.push(path.to_path_buf());
    }

    files.sort();
    Ok(files)
}

fn search_file(
    path: &Path,
    search_root: &Path,
    project_root: &Path,
    re: &Regex,
    opts: &SearchOptions,
) -> Vec<SearchMatch> {
    let Ok(bytes) = std::fs::read(path) else {
        return vec![];
    };

    // Skip binary files: null byte in first BINARY_SCAN_BYTES
    if bytes[..bytes.len().min(BINARY_SCAN_BYTES)].contains(&0u8) {
        return vec![];
    }

    let Ok(content) = std::str::from_utf8(&bytes) else {
        return vec![];
    };

    // Relative path for display
    let rel = path
        .strip_prefix(search_root)
        .or_else(|_| path.strip_prefix(project_root))
        .unwrap_or(path)
        .to_string_lossy()
        .into_owned();

    let lines: Vec<&str> = content.lines().collect();
    let mut result: Vec<SearchMatch> = Vec::new();

    for (idx, line) in lines.iter().enumerate() {
        let Some(m) = re.find(line) else { continue };

        let line_no = u32::try_from(idx + 1).unwrap_or(u32::MAX);
        let col = u32::try_from(m.start() + 1).unwrap_or(1);

        let (context_before, context_after) = if opts.context > 0 {
            let ctx = opts.context as usize;
            let before: Vec<String> = lines[idx.saturating_sub(ctx)..idx]
                .iter()
                .map(ToString::to_string)
                .collect();
            let after: Vec<String> = lines[(idx + 1)..(idx + 1 + ctx).min(lines.len())]
                .iter()
                .map(ToString::to_string)
                .collect();
            (before, after)
        } else {
            (vec![], vec![])
        };

        result.push(SearchMatch {
            path: rel.clone(),
            line: line_no,
            column: col,
            preview: line.to_string(),
            context_before,
            context_after,
        });
    }

    result
}

/// Map CLI language flag to file extensions via `Language::extensions()` — single source of truth.
fn extensions_for_lang(lang: &str) -> &'static [&'static str] {
    match lang {
        "ts" | "typescript" => Language::TypeScript.extensions(),
        "js" | "javascript" => Language::JavaScript.extensions(),
        "rs" | "rust" => Language::Rust.extensions(),
        "go" => Language::Go.extensions(),
        "c" | "cpp" | "c++" | "cxx" => Language::Cpp.extensions(),
        _ => &[],
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    fn make_project(files: &[(&str, &str)]) -> TempDir {
        let dir = tempfile::tempdir().unwrap();
        for (name, content) in files {
            let path = dir.path().join(name);
            if let Some(parent) = path.parent() {
                fs::create_dir_all(parent).unwrap();
            }
            fs::write(path, content).unwrap();
        }
        dir
    }

    fn opts(pattern: &str) -> SearchOptions {
        SearchOptions {
            pattern: pattern.to_string(),
            path: None,
            ignore_case: false,
            word: false,
            literal: false,
            context: 0,
            files_only: false,
            lang_filter: None,
            max_matches: 200,
        }
    }

    #[test]
    fn finds_literal_match() {
        let dir = make_project(&[("src/lib.rs", "fn hello() {}\nfn world() {}")]);
        let o = run(
            &SearchOptions {
                literal: true,
                ..opts("hello")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.total_matches, 1);
        assert_eq!(o.matches[0].line, 1);
        assert!(o.matches[0].preview.contains("hello"));
    }

    #[test]
    fn finds_regex_match() {
        let dir = make_project(&[("a.rs", "foo123\nbar456")]);
        let o = run(&opts(r"\d+"), dir.path()).unwrap();
        assert_eq!(o.total_matches, 2);
    }

    #[test]
    fn ignore_case_works() {
        let dir = make_project(&[("a.rs", "Hello\nhello\nHELLO")]);
        let o = run(
            &SearchOptions {
                ignore_case: true,
                ..opts("hello")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.total_matches, 3);
    }

    #[test]
    fn word_boundary_works() {
        let dir = make_project(&[("a.rs", "foobar\nfoo bar\nfoo")]);
        let o = run(
            &SearchOptions {
                word: true,
                ..opts("foo")
            },
            dir.path(),
        )
        .unwrap();
        // "foobar" should NOT match, "foo bar" and "foo" should match
        assert_eq!(o.total_matches, 2);
    }

    #[test]
    fn respects_max_matches() {
        let content: String = (1..=10).map(|i| format!("line{i}\n")).collect();
        let dir = make_project(&[("a.rs", &content)]);
        let o = run(
            &SearchOptions {
                max_matches: 3,
                ..opts("line")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.total_matches, 3);
        assert!(o.truncated);
    }

    #[test]
    fn skips_binary_files() {
        let dir = tempfile::tempdir().unwrap();
        let mut binary = vec![0u8; 100];
        binary.extend_from_slice(b"hello");
        fs::write(dir.path().join("file.bin"), &binary).unwrap();
        let o = run(&opts("hello"), dir.path()).unwrap();
        assert_eq!(o.total_matches, 0);
    }

    #[test]
    fn lang_filter_ts_only() {
        let dir = make_project(&[("a.ts", "const foo = 1;"), ("b.rs", "const foo: i32 = 1;")]);
        let o = run(
            &SearchOptions {
                lang_filter: Some("ts".to_string()),
                ..opts("foo")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.total_matches, 1);
        assert!(o.matches[0].path.ends_with("a.ts"));
    }

    #[test]
    fn context_lines_correct() {
        let dir = make_project(&[("a.rs", "line1\nline2\ntarget\nline4\nline5")]);
        let o = run(
            &SearchOptions {
                context: 1,
                ..opts("target")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.total_matches, 1);
        let m = &o.matches[0];
        assert_eq!(m.context_before, vec!["line2"]);
        assert_eq!(m.context_after, vec!["line4"]);
    }

    #[test]
    fn files_only_mode() {
        let dir = make_project(&[("a.rs", "needle"), ("b.rs", "haystack")]);
        let o = run(
            &SearchOptions {
                files_only: true,
                ..opts("needle")
            },
            dir.path(),
        )
        .unwrap();
        assert_eq!(o.files_with_matches, 1);
    }

    #[test]
    fn no_matches_returns_empty() {
        let dir = make_project(&[("a.rs", "hello world")]);
        let o = run(&opts("nonexistent_xyz"), dir.path()).unwrap();
        assert_eq!(o.total_matches, 0);
        assert!(!o.truncated);
    }

    #[test]
    fn grep_escape_alternation() {
        // \| is grep BRE syntax; should be treated as | (alternation)
        let dir = make_project(&[("a.ts", "import foo\nfrom bar")]);
        let o = run(&opts(r"foo\|bar"), dir.path()).unwrap();
        assert_eq!(o.total_matches, 2);
    }

    #[test]
    fn grep_escape_plus_and_parens() {
        assert_eq!(normalize_grep_escapes(r"foo\+"), "foo+");
        assert_eq!(normalize_grep_escapes(r"\(foo\)"), "(foo)");
        assert_eq!(normalize_grep_escapes(r"a\?b"), "a?b");
    }

    #[test]
    fn real_pipe_unaffected() {
        // a plain | should still work as alternation
        let dir = make_project(&[("a.ts", "import foo\nfrom bar")]);
        let o = run(&opts("foo|bar"), dir.path()).unwrap();
        assert_eq!(o.total_matches, 2);
    }
}