codesearch 0.1.12

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
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
//! Symbol Indexer and Store
//!
//! Provides fast, persistent indexing of symbols with incremental updates.

use super::{Symbol, SymbolKind, IndexStats};
use dashmap::DashMap;
use rayon::prelude::*;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
use walkdir::WalkDir;

/// In-memory symbol index with fast lookups
#[derive(Clone)]
pub struct SymbolIndex {
    /// All symbols by ID
    symbols: Arc<DashMap<String, Symbol>>,

    /// Symbol name to IDs mapping
    name_index: Arc<DashMap<String, Vec<String>>>,

    /// File path to symbols mapping
    file_index: Arc<DashMap<String, Vec<String>>>,

    /// Kind to symbols mapping
    kind_index: Arc<DashMap<String, Vec<String>>>,

    /// Symbol relationships
    relationships: Arc<DashMap<String, Vec<String>>>,
}

/// Persistent symbol index store
pub struct SymbolIndexStore {
    /// In-memory index
    index: SymbolIndex,

    /// Storage path
    store_path: PathBuf,

    /// Indexed files metadata
    file_metadata: Arc<DashMap<String, FileMetadata>>,
}

/// File metadata for incremental indexing
#[derive(Debug, Clone, Serialize, Deserialize)]
struct FileMetadata {
    path: String,
    size: u64,
    modified: SystemTime,
    hash: String,
}

impl SymbolIndex {
    /// Create a new empty index
    pub fn new() -> Self {
        Self {
            symbols: Arc::new(DashMap::new()),
            name_index: Arc::new(DashMap::new()),
            file_index: Arc::new(DashMap::new()),
            kind_index: Arc::new(DashMap::new()),
            relationships: Arc::new(DashMap::new()),
        }
    }

    /// Add a symbol to the index
    pub fn add_symbol(&self, symbol: Symbol) {
        let symbol_id = symbol.id.clone();

        // Add to main symbol store
        self.symbols.insert(symbol_id.clone(), symbol.clone());

        // Update name index
        self.name_index
            .entry(symbol.name.clone())
            .or_insert_with(Vec::new)
            .push(symbol_id.clone());

        // Update file index
        self.file_index
            .entry(symbol.file_path.clone())
            .or_insert_with(Vec::new)
            .push(symbol_id.clone());

        // Update kind index
        let kind_str = format!("{:?}", symbol.kind);
        self.kind_index
            .entry(kind_str)
            .or_insert_with(Vec::new)
            .push(symbol_id.clone());
    }

    /// Get symbol by ID
    pub fn get_symbol(&self, id: &str) -> Option<Symbol> {
        self.symbols.get(id).map(|s| s.value().clone())
    }

    /// Find symbols by name (exact match)
    pub fn find_by_name(&self, name: &str) -> Vec<Symbol> {
        self.name_index
            .get(name)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.get_symbol(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Find symbols by name pattern
    pub fn find_by_name_pattern(&self, pattern: &str) -> Result<Vec<Symbol>, Box<dyn std::error::Error>> {
        let regex = Regex::new(pattern)?;
        let mut symbols = Vec::new();

        for entry in self.name_index.iter() {
            if regex.is_match(entry.key()) {
                for id in entry.value() {
                    if let Some(symbol) = self.get_symbol(id) {
                        symbols.push(symbol);
                    }
                }
            }
        }

        Ok(symbols)
    }

    /// Get symbols by file
    pub fn get_symbols_in_file(&self, file_path: &str) -> Vec<Symbol> {
        self.file_index
            .get(file_path)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.get_symbol(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Get symbols by kind
    pub fn get_symbols_by_kind(&self, kind: &SymbolKind) -> Vec<Symbol> {
        let kind_str = format!("{:?}", kind);
        self.kind_index
            .get(&kind_str)
            .map(|ids| {
                ids.iter()
                    .filter_map(|id| self.get_symbol(id))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Search symbols with multiple criteria
    pub fn search_symbols(
        &self,
        name_pattern: Option<&str>,
        kind: Option<&SymbolKind>,
        file_path: Option<&str>,
        visibility: Option<&super::SymbolVisibility>,
    ) -> Vec<Symbol> {
        let mut results = Vec::new();

        // Start with all symbols or filter by one criterion
        let candidates: Vec<Symbol> = if let Some(pattern) = name_pattern {
            if let Ok(regex) = Regex::new(pattern) {
                let mut symbols = Vec::new();
                for entry in self.name_index.iter() {
                    if regex.is_match(entry.key()) {
                        for id in entry.value() {
                            if let Some(symbol) = self.get_symbol(id) {
                                symbols.push(symbol);
                            }
                        }
                    }
                }
                symbols
            } else {
                Vec::new()
            }
        } else if let Some(k) = kind {
            self.get_symbols_by_kind(k)
        } else if let Some(fp) = file_path {
            self.get_symbols_in_file(fp)
        } else {
            self.symbols.iter().map(|e| e.value().clone()).collect()
        };

        // Apply additional filters
        for symbol in candidates {
            if let Some(k) = kind {
                if symbol.kind != *k {
                    continue;
                }
            }

            if let Some(fp) = file_path {
                if symbol.file_path != fp {
                    continue;
                }
            }

            if let Some(v) = visibility {
                if &symbol.visibility != v {
                    continue;
                }
            }

            results.push(symbol);
        }

        results
    }

    /// Get index statistics
    pub fn get_stats(&self) -> IndexStats {
        let mut symbols_by_kind = HashMap::new();
        let mut symbols_by_language = HashMap::new();

        for symbol in self.symbols.iter() {
            // Count by kind
            let kind_str = format!("{:?}", symbol.kind);
            *symbols_by_kind.entry(kind_str).or_insert(0) += 1;

            // Count by language (inferred from file extension)
            if let Some(ext) = Path::new(&symbol.file_path).extension() {
                let lang = match ext.to_str().unwrap_or("") {
                    "rs" => "Rust",
                    "py" | "pyw" | "pyi" => "Python",
                    "js" | "jsx" | "mjs" | "cjs" => "JavaScript",
                    "ts" | "tsx" => "TypeScript",
                    "go" => "Go",
                    "java" => "Java",
                    _ => "Other",
                };
                *symbols_by_language.entry(lang.to_string()).or_insert(0) += 1;
            }
        }

        let total_files = self.file_index.len();

        IndexStats {
            total_symbols: self.symbols.len(),
            total_files,
            symbols_by_kind,
            symbols_by_language,
            index_size_bytes: 0, // TODO: Calculate actual size
            last_updated: chrono::Utc::now(),
        }
    }

    /// Clear the index
    pub fn clear(&self) {
        self.symbols.clear();
        self.name_index.clear();
        self.file_index.clear();
        self.kind_index.clear();
        self.relationships.clear();
    }
}

impl Default for SymbolIndex {
    fn default() -> Self {
        Self::new()
    }
}

impl SymbolIndexStore {
    /// Create a new index store
    pub fn new(store_path: PathBuf) -> Self {
        let index = SymbolIndex::new();
        let file_metadata = Arc::new(DashMap::new());

        // Load existing index if available
        let metadata = if store_path.exists() {
            Self::load_metadata(&store_path).unwrap_or_default()
        } else {
            HashMap::new()
        };

        for (path, meta) in metadata {
            file_metadata.insert(path, meta);
        }

        Self {
            index,
            store_path,
            file_metadata,
        }
    }

    /// Index a directory
    pub fn index_directory(
        &self,
        path: &Path,
        extensions: Option<&[String]>,
        exclude: Option<&[String]>,
    ) -> Result<usize, Box<dyn std::error::Error>> {
        let mut indexed_count = 0;

        let walker = WalkDir::new(path)
            .into_iter()
            .filter_entry(|e| {
                if let Some(name) = e.file_name().to_str() {
                    if let Some(exclude_dirs) = exclude {
                        for exclude_dir in exclude_dirs {
                            if name == exclude_dir {
                                return false;
                            }
                        }
                    }
                }
                true
            })
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file());

        let files: Vec<PathBuf> = walker
            .filter(|entry| {
                let file_path = entry.path();
                if let Some(exts) = extensions {
                    if let Some(ext) = file_path.extension().and_then(|s| s.to_str()) {
                        exts.iter().any(|e| e == ext)
                    } else {
                        false
                    }
                } else {
                    true
                }
            })
            .map(|e| e.path().to_path_buf())
            .collect();

        // Process files in parallel
        use rayon::prelude::*;
        let results: Vec<Vec<Symbol>> = files
            .par_iter()
            .filter_map(|file_path| {
                if self.should_reindex(file_path) {
                    if let Ok(symbols) = self.index_file(file_path) {
                        Some(symbols)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect();

        // Add symbols to index
        for symbols in results {
            for symbol in symbols {
                self.index.add_symbol(symbol);
            }
        }
        indexed_count = self.index.symbols.len();

        // Save index
        self.save()?;

        Ok(indexed_count)
    }

    /// Index a single file
    fn index_file(&self, file_path: &Path) -> Result<Vec<Symbol>, Box<dyn std::error::Error>> {
        use super::extractor::SymbolExtractor;

        let extractor = SymbolExtractor::new();
        let symbols = extractor.extract_from_file(file_path)?;

        // Update file metadata
        if let Ok(metadata) = fs::metadata(file_path) {
            let modified = metadata.modified()?;
            let size = metadata.len();
            let content = fs::read_to_string(file_path)?;
            let hash = format!("{:x}", md5::compute(content.as_bytes()));

            let file_meta = FileMetadata {
                path: file_path.to_string_lossy().to_string(),
                size,
                modified,
                hash,
            };

            self.file_metadata.insert(
                file_path.to_string_lossy().to_string(),
                file_meta,
            );
        }

        Ok(symbols)
    }

    /// Check if file should be reindexed
    fn should_reindex(&self, file_path: &Path) -> bool {
        if let Ok(metadata) = fs::metadata(file_path) {
            if let Ok(modified) = metadata.modified() {
                if let Some(existing_meta) = self.file_metadata.get(
                    &file_path.to_string_lossy().to_string()
                ) {
                    return existing_meta.modified != modified || existing_meta.size != metadata.len();
                }
            }
        }
        true
    }

    /// Get the underlying index
    pub fn index(&self) -> &SymbolIndex {
        &self.index
    }

    /// Save index to disk
    pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
        // Save metadata
        self.save_metadata()?;

        // Save symbols (in production, you'd want a more efficient format)
        // For now, we'll skip this and just save metadata
        // In a real implementation, you'd serialize the entire index

        Ok(())
    }

    /// Save file metadata
    fn save_metadata(&self) -> Result<(), Box<dyn std::error::Error>> {
        let metadata_map: HashMap<String, FileMetadata> = self
            .file_metadata
            .iter()
            .map(|entry| (entry.key().clone(), entry.value().clone()))
            .collect();

        let json = serde_json::to_string_pretty(&metadata_map)?;

        if let Some(parent) = self.store_path.parent() {
            fs::create_dir_all(parent)?;
        }

        let metadata_path = self.store_path.with_extension("meta.json");
        fs::write(&metadata_path, json)?;

        Ok(())
    }

    /// Load file metadata
    fn load_metadata(path: &Path) -> Result<HashMap<String, FileMetadata>, Box<dyn std::error::Error>> {
        let metadata_path = path.with_extension("meta.json");

        if metadata_path.exists() {
            let content = fs::read_to_string(&metadata_path)?;
            let metadata: HashMap<String, FileMetadata> = serde_json::from_str(&content)?;
            Ok(metadata)
        } else {
            Ok(HashMap::new())
        }
    }

    /// Clear the index
    pub fn clear(&self) -> Result<(), Box<dyn std::error::Error>> {
        self.index.clear();
        self.file_metadata.clear();

        // Remove files
        if self.store_path.exists() {
            fs::remove_file(&self.store_path)?;
        }

        let metadata_path = self.store_path.with_extension("meta.json");
        if metadata_path.exists() {
            fs::remove_file(&metadata_path)?;
        }

        Ok(())
    }
}

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

    #[test]
    fn test_symbol_index() {
        let index = SymbolIndex::new();

        let symbol = Symbol {
            id: "test_id".to_string(),
            name: "test_function".to_string(),
            kind: SymbolKind::Function,
            file_path: "test.rs".to_string(),
            line: 10,
            column: 0,
            end_line: 15,
            signature: "()".to_string(),
            documentation: None,
            visibility: super::super::SymbolVisibility::Public,
            parent: None,
            type_info: None,
            generics: vec![],
            annotations: vec![],
            attributes: vec![],
            metadata: HashMap::new(),
        };

        index.add_symbol(symbol.clone());

        // Test retrieval
        let retrieved = index.get_symbol("test_id");
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().name, "test_function");

        // Test name search
        let by_name = index.find_by_name("test_function");
        assert_eq!(by_name.len(), 1);
        assert_eq!(by_name[0].name, "test_function");

        // Test file search
        let by_file = index.get_symbols_in_file("test.rs");
        assert_eq!(by_file.len(), 1);

        // Test kind search
        let by_kind = index.get_symbols_by_kind(&SymbolKind::Function);
        assert_eq!(by_kind.len(), 1);
    }

    #[test]
    fn test_index_store() {
        let dir = tempdir().unwrap();
        let store_path = dir.path().join("test_index");
        let store = SymbolIndexStore::new(store_path);

        // Create a test file
        let test_file = dir.path().join("test.rs");
        fs::write(
            &test_file,
            r#"
fn test_function() -> i32 {
    42
}
"#,
        ).unwrap();

        // Index the directory
        let count = store
            .index_directory(dir.path(), Some(&["rs".to_string()]), None)
            .unwrap();
        assert!(count > 0);

        // Test search
        let symbols = store.index().find_by_name("test_function");
        assert!(!symbols.is_empty());

        // Test statistics
        let stats = store.index().get_stats();
        assert_eq!(stats.total_symbols, count);
    }
}