leindex 1.6.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
// Project Configuration
//
// *La Configuration* (The Configuration) - Project settings for LeIndex

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::Path;

/// Default configuration file name
pub const DEFAULT_CONFIG_FILE: &str = ".leindex/config.toml";

/// Project configuration
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProjectConfig {
    /// Language filtering settings
    pub languages: LanguageConfig,

    /// Path exclusion patterns
    pub exclusions: ExclusionConfig,

    /// Token budget settings
    pub tokens: TokenConfig,

    /// Storage configuration
    pub storage: StorageConfig,

    /// Memory management settings
    pub memory: MemoryConfig,
}

impl ProjectConfig {
    /// Load configuration from a directory
    ///
    /// Looks for `.leindex/config.toml` in the project directory.
    /// If not found, returns default configuration.
    ///
    /// # Arguments
    ///
    /// * `project_path` - Path to the project directory
    ///
    /// # Returns
    ///
    /// `Result<ProjectConfig>` - Loaded or default configuration
    pub fn load<P: AsRef<Path>>(project_path: P) -> Result<Self> {
        let config_path = project_path.as_ref().join(DEFAULT_CONFIG_FILE);

        if !config_path.exists() {
            // Return default configuration if file doesn't exist
            return Ok(ProjectConfig::default());
        }

        // Read and parse TOML file
        let content = fs::read_to_string(&config_path)
            .with_context(|| format!("Failed to read config file: {:?}", config_path))?;

        let config: ProjectConfig = toml::from_str(&content)
            .with_context(|| format!("Failed to parse config file: {:?}", config_path))?;

        Ok(config)
    }

    /// Save configuration to a directory
    ///
    /// Creates `.leindex` directory if it doesn't exist.
    ///
    /// # Arguments
    ///
    /// * `project_path` - Path to the project directory
    ///
    /// # Returns
    ///
    /// `Result<()>` - Success or error
    pub fn save<P: AsRef<Path>>(&self, project_path: P) -> Result<()> {
        let config_dir = project_path.as_ref().join(".leindex");
        fs::create_dir_all(&config_dir)
            .with_context(|| format!("Failed to create config directory: {:?}", config_dir))?;

        let config_path = config_dir.join("config.toml");

        let toml_string =
            toml::to_string_pretty(self).context("Failed to serialize configuration")?;

        fs::write(&config_path, toml_string)
            .with_context(|| format!("Failed to write config file: {:?}", config_path))?;

        Ok(())
    }

    /// Get enabled languages as a set of extensions
    pub fn enabled_extensions(&self) -> HashSet<String> {
        self.languages.enabled_extensions()
    }

    /// Check if a path should be excluded
    pub fn should_exclude<P: AsRef<Path>>(&self, path: P) -> bool {
        let path = path.as_ref();

        // Check directory exclusions
        if let Some(parent) = path.parent() {
            if let Some(dir_name) = parent.file_name() {
                let dir = dir_name.to_string_lossy();
                for pattern in &self.exclusions.directory_patterns {
                    if pattern.matches(&dir) {
                        return true;
                    }
                }
            }
        }

        // Check file exclusions
        if let Some(file_name) = path.file_name() {
            let name = file_name.to_string_lossy();
            for pattern in &self.exclusions.file_patterns {
                if pattern.matches(&name) {
                    return true;
                }
            }
        }

        // Check path patterns
        let path_str = path.to_string_lossy();
        for pattern in &self.exclusions.path_patterns {
            if pattern.matches(&path_str) {
                return true;
            }
        }

        false
    }

    /// Get token budget for analysis
    pub fn token_budget(&self) -> usize {
        self.tokens.default_budget
    }

    /// Get maximum tokens for context expansion
    pub fn max_context_tokens(&self) -> usize {
        self.tokens.max_context
    }
}

/// Language filtering configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LanguageConfig {
    /// Enable all supported languages
    pub enable_all: bool,

    /// Explicitly enabled languages (by extension or name)
    pub enabled: Vec<String>,

    /// Explicitly disabled languages
    pub disabled: Vec<String>,
}

impl Default for LanguageConfig {
    fn default() -> Self {
        Self {
            enable_all: true,
            enabled: Vec::new(),
            disabled: vec!["vim".to_string()], // Disable vim scripts by default
        }
    }
}

impl LanguageConfig {
    /// Get enabled language extensions
    pub fn enabled_extensions(&self) -> HashSet<String> {
        let mut extensions = HashSet::new();

        // All file extensions supported by the parse module.
        // Must stay in sync with crate::parse::grammar::LanguageId::from_extension
        // and crate::cli::leindex::collect_source_files_with_hashes.
        let all_extensions = [
            "rs", "py", "js", "jsx", "mjs", "cjs", "ts", "tsx", "mts",
            "cts", // Main languages
            "go", "java", "cpp", "cc", "cxx", "c", "h", "hpp", // Systems languages
            "cs",  // C#
            "rb", "php", "lua", "scala", "sc", // Scripting languages
            "sh", "bash", // Shell
            "json", // Data
        ];

        if self.enable_all {
            for ext in &all_extensions {
                if !self.disabled.contains(&ext.to_string()) {
                    extensions.insert(ext.to_string());
                }
            }
        } else {
            for lang in &self.enabled {
                // Check if it's an extension
                if all_extensions.contains(&lang.as_str()) {
                    extensions.insert(lang.clone());
                } else {
                    // It's a language name - map to extensions
                    let exts = Self::language_to_extensions(lang);
                    for ext in exts {
                        if !self.disabled.contains(&ext.to_string()) {
                            extensions.insert(ext.to_string());
                        }
                    }
                }
            }
        }

        extensions
    }

    /// Map language name to file extensions
    fn language_to_extensions(lang: &str) -> Vec<&'static str> {
        match lang.to_lowercase().as_str() {
            "rust" => vec!["rs"],
            "python" => vec!["py"],
            "javascript" => vec!["js", "jsx", "mjs", "cjs"],
            "typescript" => vec!["ts", "tsx", "mts", "cts"],
            "go" => vec!["go"],
            "java" => vec!["java"],
            "cpp" | "c++" => vec!["cpp", "cc", "cxx", "c", "h", "hpp"],
            "c" => vec!["c", "h"],
            "csharp" | "c#" => vec!["cs"],
            "ruby" => vec!["rb"],
            "php" => vec!["php"],
            "lua" => vec!["lua"],
            "scala" => vec!["scala", "sc"],
            "bash" | "shell" => vec!["sh", "bash"],
            "json" => vec!["json"],
            _ => vec![],
        }
    }

    /// Check if a language (by extension) is enabled
    pub fn is_extension_enabled(&self, ext: &str) -> bool {
        self.enabled_extensions().contains(ext)
    }
}

/// Path exclusion configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExclusionConfig {
    /// Directory name patterns to exclude
    pub directory_patterns: Vec<StringPattern>,

    /// File name patterns to exclude
    pub file_patterns: Vec<StringPattern>,

    /// Full path patterns to exclude
    pub path_patterns: Vec<StringPattern>,
}

impl Default for ExclusionConfig {
    fn default() -> Self {
        Self {
            directory_patterns: vec![
                // Version control
                ".git".into(),
                ".hg".into(),
                ".svn".into(),
                // Build outputs
                "target".into(),
                "build".into(),
                "dist".into(),
                "out".into(),
                ".next".into(),
                "coverage".into(),
                // Package managers / dependencies
                "node_modules".into(),
                "vendor".into(),
                "bower_components".into(),
                // Python virtual environments & caches
                ".venv".into(),
                "venv".into(),
                "env".into(),
                "__pycache__".into(),
                ".tox".into(),
                ".mypy_cache".into(),
                ".pytest_cache".into(),
                ".ruff_cache".into(),
                // IDE / editor metadata
                ".idea".into(),
                ".vscode".into(),
                // Misc generated
                ".leindex".into(),
            ],
            file_patterns: vec![
                // Note: lockfiles (Cargo.lock, package-lock.json, etc.) are NOT listed
                // in file_patterns because scan_project_files() collects them as
                // dependency manifests BEFORE checking exclusions. This ensures they
                // are always available for external dependency resolution while still
                // being excluded from source file indexing (the manifest check `continue`s
                // before the source extension check).
                "*.min.js".into(),
                "*.min.css".into(),
                "*.pb.go".into(), // Generated protobuf files
                "*.generated.rs".into(),
                "*.bundle.js".into(), // Bundled JS
                "*.chunk.js".into(),  // Webpack chunks
            ],
            path_patterns: vec![
                "*/target/*".into(),
                "*/node_modules/*".into(),
                "*/dist/*".into(),
                "*/out/*".into(),
            ],
        }
    }
}

impl ExclusionConfig {
    /// Check if a path should be excluded based on configured patterns
    pub fn should_exclude(&self, path: &str) -> bool {
        // Check directory patterns
        for segment in path.split('/') {
            for pattern in &self.directory_patterns {
                if pattern.matches(segment) {
                    return true;
                }
            }
        }

        // Check file patterns (only the filename part)
        if let Some(filename) = path.rsplit('/').next() {
            for pattern in &self.file_patterns {
                if pattern.matches(filename) {
                    return true;
                }
            }
        }

        // Check full path patterns
        for pattern in &self.path_patterns {
            if pattern.matches(path) {
                return true;
            }
        }

        false
    }
}

/// String pattern for matching (supports wildcards)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StringPattern {
    /// The pattern string, which may contain wildcards
    pub pattern: String,
}

impl From<&str> for StringPattern {
    fn from(s: &str) -> Self {
        Self {
            pattern: s.to_string(),
        }
    }
}

impl From<String> for StringPattern {
    fn from(s: String) -> Self {
        Self { pattern: s }
    }
}

impl StringPattern {
    /// Check if this pattern matches a string
    ///
    /// Supports `*` wildcards.
    pub fn matches(&self, text: &str) -> bool {
        if self.pattern == "*" {
            return true;
        }

        if !self.pattern.contains('*') {
            return self.pattern == text;
        }

        // Simple wildcard matching
        let parts: Vec<&str> = self.pattern.split('*').collect();
        if parts.len() == 2 {
            let prefix = parts[0];
            let suffix = parts[1];
            text.starts_with(prefix) && text.ends_with(suffix)
        } else {
            // Multiple wildcards - check each part in sequence
            let mut idx = 0;
            for (i, part) in parts.iter().enumerate() {
                if i == parts.len() - 1 && !part.is_empty() {
                    // Last part - check suffix
                    if !text.ends_with(part) {
                        return false;
                    }
                } else if !part.is_empty() {
                    // Middle or first part - find it
                    if let Some(pos) = text[idx..].find(part) {
                        idx = pos + part.len();
                    } else {
                        return false;
                    }
                }
            }
            true
        }
    }
}

/// Token budget configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenConfig {
    /// Default token budget for analysis
    pub default_budget: usize,

    /// Maximum tokens for context expansion
    pub max_context: usize,

    /// Minimum tokens for results
    pub min_results: usize,

    /// Maximum number of results to return
    pub max_results: usize,
}

impl Default for TokenConfig {
    fn default() -> Self {
        Self {
            default_budget: 2000,
            max_context: 5000,
            min_results: 5,
            max_results: 20,
        }
    }
}

/// Storage configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    /// Storage backend to use
    pub backend: StorageBackend,

    /// Database path relative to project root
    pub db_path: Option<String>,

    /// Whether to enable WAL mode
    pub wal_enabled: bool,

    /// Cache size in pages
    pub cache_size_pages: Option<usize>,

    /// Connection timeout in seconds
    pub connection_timeout_secs: Option<u64>,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            backend: StorageBackend::SQLite,
            db_path: None, // Use default
            wal_enabled: true,
            cache_size_pages: Some(10000),
            connection_timeout_secs: Some(30),
        }
    }
}

/// Storage backend type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum StorageBackend {
    /// SQLite (default, embedded)
    SQLite,

    /// Turso (remote libsql)
    Turso {
        /// Turso database URL (e.g., libsql://...")
        database_url: String,
        /// Turso authentication token
        auth_token: Option<String>,
    },
}

/// Memory management configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryConfig {
    /// RSS threshold for spilling (0.0-1.0)
    pub spill_threshold: f64,

    /// Whether to enable automatic spilling
    pub auto_spill: bool,

    /// Maximum memory to use in MB (0 = unlimited)
    pub max_memory_mb: usize,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            spill_threshold: 0.9,
            auto_spill: true,
            max_memory_mb: 8192, // 8 GB default
        }
    }
}

/// Configuration error
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    /// Input/Output error during configuration file handling
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Failed to serialize configuration to TOML/JSON
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// Failed to parse configuration from file
    #[error("Parse error: {0}")]
    Parse(String),

    /// The configuration contains invalid values or settings
    #[error("Invalid configuration: {0}")]
    Invalid(String),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = ProjectConfig::default();
        assert!(config.languages.enable_all);
        assert_eq!(config.tokens.default_budget, 2000);
    }

    #[test]
    fn test_language_extensions() {
        let config = LanguageConfig::default();
        let exts = config.enabled_extensions();
        assert!(exts.contains("rs"));
        assert!(exts.contains("py"));
        assert!(!exts.contains("vim"));
    }

    #[test]
    fn test_exclusion_patterns() {
        let config = ExclusionConfig::default();
        assert!(config.should_exclude("target/main.rs"));
        assert!(config.should_exclude("node_modules/package/index.js"));
        assert!(!config.should_exclude("src/main.rs"));
    }

    #[test]
    fn test_exclusion_build_output_dirs() {
        let config = ExclusionConfig::default();
        // Build outputs: out/, dist/, build/, .next/, coverage/
        assert!(config.should_exclude("out/bundle.js"));
        assert!(config.should_exclude("dist/index.js"));
        assert!(config.should_exclude("build/main.js"));
        assert!(config.should_exclude(".next/server/app.js"));
        assert!(config.should_exclude("coverage/lcov.info"));
        // Python caches
        assert!(config.should_exclude("__pycache__/module.pyc"));
        assert!(config.should_exclude(".mypy_cache/stubs.json"));
        assert!(config.should_exclude(".pytest_cache/v/cache.json"));
        assert!(config.should_exclude(".ruff_cache/data.json"));
        assert!(config.should_exclude(".tox/py39/lib/site.py"));
        // IDE
        assert!(config.should_exclude(".idea/workspace.xml"));
        assert!(config.should_exclude(".vscode/settings.json"));
        // Virtual envs
        assert!(config.should_exclude(".venv/lib/python3.12/os.py"));
        assert!(config.should_exclude("venv/lib/python3.12/os.py"));
        // Source should NOT be excluded
        assert!(!config.should_exclude("src/main.rs"));
        assert!(!config.should_exclude("lib/utils.ts"));
    }

    #[test]
    fn test_exclusion_minified_and_generated_files() {
        let config = ExclusionConfig::default();
        // Lockfiles are NOT excluded — they are collected as manifests by scan_project_files()
        // before the exclusion check runs.
        assert!(!config.should_exclude("Cargo.lock"));
        assert!(!config.should_exclude("frontend/package-lock.json"));
        assert!(!config.should_exclude("api/yarn.lock"));
        assert!(!config.should_exclude("packages/web/pnpm-lock.yaml"));
        assert!(!config.should_exclude("backend/composer.lock"));
        assert!(!config.should_exclude("python/Pipfile.lock"));
        assert!(!config.should_exclude("python/poetry.lock"));
        assert!(!config.should_exclude("ruby/Gemfile.lock"));
        // Minified and generated files ARE excluded
        assert!(config.should_exclude("app.min.js"));
        assert!(config.should_exclude("styles.min.css"));
        assert!(config.should_exclude("service.pb.go"));
        assert!(config.should_exclude("schema.generated.rs"));
        assert!(config.should_exclude("vendor.bundle.js"));
        assert!(config.should_exclude("main.chunk.js"));
        // Normal files should NOT be excluded
        assert!(!config.should_exclude("package.json"));
        assert!(!config.should_exclude("Cargo.toml"));
        assert!(!config.should_exclude("pyproject.toml"));
        assert!(!config.should_exclude("app.js"));
        assert!(!config.should_exclude("service.go"));
    }

    #[test]
    fn test_language_extensions_complete() {
        let config = LanguageConfig::default();
        let exts = config.enabled_extensions();
        // All extensions supported by leparse
        for ext in &[
            "rs", "py", "js", "jsx", "mjs", "cjs", "ts", "tsx", "mts", "cts", "go", "java", "cpp",
            "cc", "cxx", "c", "h", "hpp", "cs", "rb", "php", "lua", "scala", "sc", "sh", "bash",
            "json",
        ] {
            assert!(exts.contains(*ext), "Extension '{}' should be enabled", ext);
        }
    }

    #[test]
    fn test_language_to_extensions_complete() {
        // Verify all language names resolve to extensions
        let cases = vec![
            ("rust", vec!["rs"]),
            ("python", vec!["py"]),
            ("javascript", vec!["js", "jsx", "mjs", "cjs"]),
            ("typescript", vec!["ts", "tsx", "mts", "cts"]),
            ("csharp", vec!["cs"]),
            ("c#", vec!["cs"]),
            ("bash", vec!["sh", "bash"]),
            ("scala", vec!["scala", "sc"]),
            ("json", vec!["json"]),
        ];
        for (name, expected_exts) in cases {
            let exts = LanguageConfig::language_to_extensions(name);
            for ext in expected_exts {
                assert!(
                    exts.contains(&ext),
                    "Language '{}' should map to extension '{}'",
                    name,
                    ext
                );
            }
        }
    }

    #[test]
    fn test_string_pattern() {
        let pattern = StringPattern::from("*.min.js");
        assert!(pattern.matches("file.min.js"));
        assert!(pattern.matches("path/to/file.min.js"));
        assert!(!pattern.matches("file.js"));

        let wildcard = StringPattern::from("*");
        assert!(wildcard.matches("anything"));
    }

    #[test]
    fn test_config_serialization() {
        let config = ProjectConfig::default();
        let toml_string = toml::to_string(&config).unwrap();
        println!("{}", toml_string);

        let deserialized: ProjectConfig = toml::from_str(&toml_string).unwrap();
        assert_eq!(deserialized.tokens.default_budget, 2000);
    }
}