loregrep 0.5.0

Repository indexing library for AI coding assistants. Tree-sitter parsing, fast in-memory indexing, and tool APIs for LLM integration.
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
use anyhow::{Context, Result};
use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::{Walk, WalkBuilder};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Instant, SystemTime};
use tracing::{info, warn};

use crate::analyzers::registry::RegistryHandle;
use crate::internal::config::FileScanningConfig;

#[derive(Clone)]
pub struct FileFilters {
    include_globs: GlobSet,
    exclude_globs: GlobSet,
    max_file_size: u64,
}

#[derive(Clone)]
pub struct LanguageDetector {
    rust_extensions: GlobSet,
    python_extensions: GlobSet,
    typescript_extensions: GlobSet,
    javascript_extensions: GlobSet,
    go_extensions: GlobSet,
}

#[derive(Debug, Clone)]
pub struct ScanConfig {
    pub follow_symlinks: bool,
    pub max_depth: Option<u32>,
    pub parallel: bool,
}

pub struct ScanResult {
    pub files: Vec<DiscoveredFile>,
    pub total_files_found: usize,
    pub total_files_filtered: usize,
    pub scan_duration: std::time::Duration,
    pub languages_found: std::collections::HashMap<String, usize>,
}

#[derive(Debug, Clone)]
pub struct DiscoveredFile {
    pub path: PathBuf,
    pub language: String,
    pub size: u64,
    pub relative_path: PathBuf,
}

#[derive(Clone)]
pub struct RepositoryScanner {
    filters: FileFilters,
    language_detector: LanguageDetector,
    config: ScanConfig,
    scanning_config: FileScanningConfig,
    /// Optional handle into the analyzer registry. When present, language
    /// labeling is driven by the registered analyzers' extensions (so adding a
    /// language needs no change here). When absent, the built-in
    /// `LanguageDetector` fallback is used.
    registry: Option<RegistryHandle>,
}

impl FileFilters {
    pub fn new(config: &FileScanningConfig) -> Result<Self> {
        let mut include_builder = GlobSetBuilder::new();
        for pattern in &config.include_patterns {
            let glob = Glob::new(pattern)
                .with_context(|| format!("Invalid include pattern: {}", pattern))?;
            include_builder.add(glob);
        }

        let mut exclude_builder = GlobSetBuilder::new();
        for pattern in &config.exclude_patterns {
            let glob = Glob::new(pattern)
                .with_context(|| format!("Invalid exclude pattern: {}", pattern))?;
            exclude_builder.add(glob);
        }

        Ok(Self {
            include_globs: include_builder.build()?,
            exclude_globs: exclude_builder.build()?,
            max_file_size: config.max_file_size,
        })
    }

    pub fn should_include(&self, path: &Path, size: u64) -> bool {
        // Check file size first (quick check)
        if size > self.max_file_size {
            return false;
        }

        let path_str = path.to_string_lossy().to_string();

        // If explicitly excluded, reject
        if self.exclude_globs.is_match(&path_str) {
            return false;
        }

        // If include patterns are specified, file must match at least one
        if self.include_globs.len() > 0 {
            self.include_globs.is_match(&path_str)
        } else {
            // No include patterns means include everything (except excluded)
            true
        }
    }
}

impl LanguageDetector {
    pub fn new() -> Result<Self> {
        let rust_globs = Self::build_globset(&["*.rs"])?;
        let python_globs = Self::build_globset(&["*.py", "*.pyi"])?;
        let typescript_globs = Self::build_globset(&["*.ts", "*.tsx"])?;
        let javascript_globs = Self::build_globset(&["*.js", "*.jsx", "*.mjs", "*.cjs"])?;
        let go_globs = Self::build_globset(&["*.go"])?;

        Ok(Self {
            rust_extensions: rust_globs,
            python_extensions: python_globs,
            typescript_extensions: typescript_globs,
            javascript_extensions: javascript_globs,
            go_extensions: go_globs,
        })
    }

    fn build_globset(patterns: &[&str]) -> Result<GlobSet> {
        let mut builder = GlobSetBuilder::new();
        for pattern in patterns {
            builder.add(Glob::new(pattern)?);
        }
        Ok(builder.build()?)
    }

    pub fn detect_language(&self, path: &Path) -> String {
        let path_str = path.to_string_lossy().to_string();

        if self.rust_extensions.is_match(&path_str) {
            "rust".to_string()
        } else if self.python_extensions.is_match(&path_str) {
            "python".to_string()
        } else if self.typescript_extensions.is_match(&path_str) {
            "typescript".to_string()
        } else if self.javascript_extensions.is_match(&path_str) {
            "javascript".to_string()
        } else if self.go_extensions.is_match(&path_str) {
            "go".to_string()
        } else {
            "unknown".to_string()
        }
    }
}

impl Default for ScanConfig {
    fn default() -> Self {
        Self {
            follow_symlinks: false,
            max_depth: Some(20),
            parallel: true,
        }
    }
}

impl RepositoryScanner {
    pub fn new(
        scanning_config: &FileScanningConfig,
        scan_config: Option<ScanConfig>,
    ) -> Result<Self> {
        let filters = FileFilters::new(scanning_config)?;
        let language_detector = LanguageDetector::new()?;
        let config = scan_config.unwrap_or_default();

        Ok(Self {
            filters,
            language_detector,
            config,
            scanning_config: scanning_config.clone(),
            registry: None,
        })
    }

    /// Construct a scanner whose language labeling is driven by the analyzer
    /// registry. Extensions of registered analyzers determine the language of
    /// each discovered file, so no per-language edits are needed in this module
    /// when a new analyzer is added.
    pub fn new_with_registry(
        scanning_config: &FileScanningConfig,
        registry: RegistryHandle,
        scan_config: Option<ScanConfig>,
    ) -> Result<Self> {
        let mut scanner = Self::new(scanning_config, scan_config)?;
        scanner.registry = Some(registry);
        Ok(scanner)
    }

    /// Determine the language label for a path, preferring the registry (when
    /// present) and falling back to the built-in extension detector.
    ///
    /// The registry label wins when an analyzer is registered for the file's
    /// extension (this keeps drop-in support for newly-registered languages).
    /// When the registry does not recognize the extension, we fall back to the
    /// built-in `LanguageDetector` so that files of a *known* language whose
    /// analyzer simply isn't registered (e.g. Python when only Rust is
    /// registered) are still discovered. This lets the scan loop emit its
    /// helpful "No analyzer available for '<lang>' files" guidance instead of
    /// silently dropping them at discovery. Genuinely-unknown extensions map to
    /// "unknown" through both paths and are skipped by the scan loop.
    fn resolve_language(&self, path: &Path) -> String {
        if let Some(registry) = &self.registry {
            if let Some(label) = registry.detect_language(&path.to_string_lossy()) {
                return label;
            }
        }
        self.language_detector.detect_language(path)
    }

    pub fn scan<P: AsRef<Path>>(&self, root_path: P) -> Result<ScanResult> {
        let start_time = Instant::now();
        let root_path = root_path.as_ref();

        info!("Starting repository scan at: {:?}", root_path);

        // Build the walker
        let walker = self.build_walker(root_path)?;

        // Track statistics
        let total_found = Arc::new(AtomicUsize::new(0));
        let total_filtered = Arc::new(AtomicUsize::new(0));

        // Collect files
        let mut discovered_files = Vec::new();
        let mut languages_found = std::collections::HashMap::new();

        for result in walker {
            match result {
                Ok(entry) => {
                    total_found.fetch_add(1, Ordering::Relaxed);

                    // Skip directories
                    if entry.file_type().map_or(false, |ft| ft.is_dir()) {
                        continue;
                    }

                    let path = entry.path();

                    // Get file size
                    let metadata = match entry.metadata() {
                        Ok(meta) => meta,
                        Err(e) => {
                            warn!("Failed to get metadata for {:?}: {}", path, e);
                            continue;
                        }
                    };

                    let file_size = metadata.len();

                    // Apply filters
                    if !self.filters.should_include(path, file_size) {
                        total_filtered.fetch_add(1, Ordering::Relaxed);
                        continue;
                    }

                    // Detect language (via the registry when available)
                    let language = self.resolve_language(path);

                    // Skip unknown languages for now
                    if language == "unknown" {
                        continue;
                    }

                    // Calculate relative path
                    let relative_path = path.strip_prefix(root_path).unwrap_or(path).to_path_buf();

                    let discovered_file = DiscoveredFile {
                        path: path.to_path_buf(),
                        language: language.clone(),
                        size: file_size,
                        relative_path,
                    };

                    discovered_files.push(discovered_file);
                    *languages_found.entry(language).or_insert(0) += 1;
                }
                Err(e) => {
                    warn!("Error walking directory: {}", e);
                }
            }
        }

        let scan_duration = start_time.elapsed();

        info!(
            "Repository scan completed in {:?}. Found {} files, filtered out {}",
            scan_duration,
            discovered_files.len(),
            total_filtered.load(Ordering::Relaxed)
        );

        Ok(ScanResult {
            files: discovered_files,
            total_files_found: total_found.load(Ordering::Relaxed),
            total_files_filtered: total_filtered.load(Ordering::Relaxed),
            scan_duration,
            languages_found,
        })
    }

    fn build_walker(&self, root_path: &Path) -> Result<Walk> {
        let mut builder = WalkBuilder::new(root_path);

        builder
            .follow_links(self.scanning_config.follow_symlinks)
            .git_ignore(self.scanning_config.respect_gitignore)
            .git_global(self.scanning_config.respect_gitignore)
            .git_exclude(self.scanning_config.respect_gitignore)
            .hidden(false); // Include hidden files by default

        if let Some(max_depth) = self.scanning_config.max_depth {
            builder.max_depth(Some(max_depth as usize));
        }

        // Add thread count for parallel processing
        if self.config.parallel {
            builder.threads(num_cpus::get());
        } else {
            builder.threads(1);
        }

        Ok(builder.build())
    }

    /// Quick scan that just counts files without detailed analysis
    pub fn quick_scan<P: AsRef<Path>>(
        &self,
        root_path: P,
    ) -> Result<(usize, std::collections::HashMap<String, usize>)> {
        let root_path = root_path.as_ref();
        let walker = self.build_walker(root_path)?;

        let mut count = 0;
        let mut languages = std::collections::HashMap::new();

        for result in walker {
            if let Ok(entry) = result {
                if entry.file_type().map_or(false, |ft| ft.is_file()) {
                    let path = entry.path();

                    if let Ok(metadata) = entry.metadata() {
                        if self.filters.should_include(path, metadata.len()) {
                            let language = self.resolve_language(path);
                            if language != "unknown" {
                                count += 1;
                                *languages.entry(language).or_insert(0) += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok((count, languages))
    }

    /// Return the most recent modification time among the files this scanner
    /// would index under `root_path`, or `None` when there are no such files.
    ///
    /// This deliberately reuses the SAME gitignore-aware walker and the SAME
    /// include/exclude filters and language detection as [`scan`], so a
    /// freshness check built on it considers exactly the files that get
    /// indexed. A file living in an excluded directory (e.g. `target/`,
    /// `dist/`, `.venv/`) or matched by `.gitignore` therefore does not affect
    /// the result — otherwise a regenerated artifact there would make a cache
    /// look perpetually stale and defeat it.
    pub fn newest_modified_time<P: AsRef<Path>>(&self, root_path: P) -> Option<SystemTime> {
        let root_path = root_path.as_ref();
        let walker = self.build_walker(root_path).ok()?;

        let mut newest: Option<SystemTime> = None;
        for result in walker {
            let entry = match result {
                Ok(entry) => entry,
                Err(_) => continue,
            };

            // Files only (mirror the `scan` loop, which skips directories).
            if entry.file_type().map_or(true, |ft| !ft.is_file()) {
                continue;
            }

            let path = entry.path();
            let metadata = match entry.metadata() {
                Ok(meta) => meta,
                Err(_) => continue,
            };

            // Apply the exact same include/exclude + language gates as `scan`,
            // so excluded/gitignored/unknown files never influence freshness.
            if !self.filters.should_include(path, metadata.len()) {
                continue;
            }
            if self.resolve_language(path) == "unknown" {
                continue;
            }

            if let Ok(modified) = metadata.modified() {
                if newest.is_none_or(|n| modified > n) {
                    newest = Some(modified);
                }
            }
        }

        newest
    }

    /// Check if a path should be analyzed based on current filters
    pub fn should_analyze(&self, path: &Path) -> Result<bool> {
        let metadata = std::fs::metadata(path)
            .with_context(|| format!("Failed to get metadata for {:?}", path))?;

        let file_size = metadata.len();
        Ok(self.filters.should_include(path, file_size))
    }

    /// Get language for a specific file
    pub fn detect_file_language(&self, path: &Path) -> String {
        self.resolve_language(path)
    }
}

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

    fn create_test_config() -> FileScanningConfig {
        FileScanningConfig {
            include_patterns: vec!["*.rs".to_string(), "*.py".to_string()],
            exclude_patterns: vec!["**/target/**".to_string(), "*.test.rs".to_string()],
            max_file_size: 1024 * 1024, // 1MB
            follow_symlinks: false,
            max_depth: Some(10),
            respect_gitignore: true,
        }
    }

    #[test]
    fn test_file_filters_creation() {
        let config = create_test_config();
        let filters = FileFilters::new(&config).unwrap();

        // Test include patterns
        assert!(filters.should_include(Path::new("main.rs"), 1000));
        assert!(filters.should_include(Path::new("script.py"), 1000));
        assert!(!filters.should_include(Path::new("main.js"), 1000));

        // Test exclude patterns
        assert!(!filters.should_include(Path::new("target/debug/main.rs"), 1000));
        assert!(!filters.should_include(Path::new("main.test.rs"), 1000));

        // Test file size limit
        assert!(!filters.should_include(Path::new("huge.rs"), 2 * 1024 * 1024));
    }

    #[test]
    fn test_language_detector() {
        let detector = LanguageDetector::new().unwrap();

        assert_eq!(detector.detect_language(Path::new("main.rs")), "rust");
        assert_eq!(detector.detect_language(Path::new("script.py")), "python");
        assert_eq!(detector.detect_language(Path::new("app.ts")), "typescript");
        assert_eq!(detector.detect_language(Path::new("app.js")), "javascript");
        assert_eq!(detector.detect_language(Path::new("main.go")), "go");
        assert_eq!(
            detector.detect_language(Path::new("unknown.txt")),
            "unknown"
        );
    }

    #[test]
    fn test_resolve_language_falls_back_for_unregistered_known_language() -> Result<()> {
        use crate::analyzers::LanguageAnalyzerRegistry;
        use crate::analyzers::registry::{DefaultLanguageRegistry, RegistryHandle};
        use crate::analyzers::rust::RustAnalyzer;

        // Registry with ONLY Rust registered.
        let mut registry = DefaultLanguageRegistry::new();
        registry.register(Box::new(RustAnalyzer::new().unwrap()))?;
        let handle = RegistryHandle::new(&registry);

        let config = create_test_config();
        let scanner = RepositoryScanner::new_with_registry(&config, handle, None)?;

        // Rust is registered -> label from registry.
        assert_eq!(scanner.detect_file_language(Path::new("main.rs")), "rust");

        // Python is a KNOWN language whose analyzer is not registered. It must
        // still be discovered (labeled "python"), not dropped as "unknown", so
        // the scan loop can emit its "no analyzer for python" guidance.
        assert_eq!(
            scanner.detect_file_language(Path::new("script.py")),
            "python"
        );

        // Genuinely-unknown extensions remain "unknown".
        assert_eq!(
            scanner.detect_file_language(Path::new("readme.txt")),
            "unknown"
        );

        Ok(())
    }

    #[test]
    fn test_repository_scanner() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let root = temp_dir.path();

        // Create test files
        fs::write(root.join("main.rs"), "fn main() {}")?;
        fs::write(root.join("lib.rs"), "pub fn test() {}")?;
        fs::write(root.join("script.py"), "print('hello')")?;
        fs::write(root.join("readme.txt"), "This is a readme")?;

        // Create subdirectory
        fs::create_dir(root.join("src"))?;
        fs::write(root.join("src/parser.rs"), "pub mod parser;")?;

        // Create excluded file
        fs::create_dir(root.join("target"))?;
        fs::write(root.join("target/main.rs"), "// generated")?;

        let config = create_test_config();
        let scan_config = ScanConfig {
            follow_symlinks: false,
            ..Default::default()
        };

        let scanner = RepositoryScanner::new(&config, Some(scan_config))?;
        let result = scanner.scan(root)?;

        // Should find main.rs, lib.rs, script.py, src/parser.rs
        // Should exclude readme.txt (not in include patterns) and target/main.rs (in exclude patterns)
        assert_eq!(result.files.len(), 4);
        assert_eq!(result.languages_found.get("rust"), Some(&3));
        assert_eq!(result.languages_found.get("python"), Some(&1));

        Ok(())
    }
}