srcwalk 0.1.1

Tree-sitter indexed lookups — smart code reading 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
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
//! Materialized symbol index — pre-computes symbol-to-file mappings for O(1) resolution.
//!
//! Instead of walking the entire tree on every symbol query, `SymbolIndex::build()`
//! parses all code files in scope using tree-sitter and stores (`symbol_name` -> locations)
//! in a concurrent `DashMap`. Subsequent lookups are O(1) hash lookups plus a filter.

use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;

use dashmap::DashMap;

use crate::lang::detect_file_type;
use crate::lang::outline::outline_language;
use crate::lang::treesitter::{extract_definition_name, DEFINITION_KINDS};
use crate::types::FileType;

/// Maximum file size to index (500 KB). Matches the limit in symbol search.
const MAX_FILE_SIZE: u64 = 500_000;

/// Per-file extraction result: (path, mtime, extracted symbols).
type FileSymbols = (PathBuf, SystemTime, Vec<(Arc<str>, u32, bool)>);

/// A location where a symbol appears in the codebase.
#[derive(Clone, Debug)]
pub struct SymbolLocation {
    pub path: PathBuf,
    pub line: u32,
    pub is_definition: bool,
    pub mtime: SystemTime,
}

/// Pre-computed symbol-to-file index for O(1) lookups.
///
/// Uses `DashMap` for lock-free concurrent reads and writes.
/// Keys are `Arc<str>` for memory-efficient string interning — many lookups
/// against the same symbol names benefit from shared allocations.
pub struct SymbolIndex {
    /// `symbol_name` -> list of locations
    symbols: DashMap<Arc<str>, Vec<SymbolLocation>>,
    /// file -> mtime when last indexed
    indexed_files: DashMap<PathBuf, SystemTime>,
}

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

impl SymbolIndex {
    /// Create an empty symbol index.
    #[must_use]
    pub fn new() -> Self {
        Self {
            symbols: DashMap::new(),
            indexed_files: DashMap::new(),
        }
    }

    /// Build the index by walking all code files in `scope`.
    ///
    /// Uses `ignore::WalkBuilder` with the same directory filtering as search
    /// (skipping `.git`, `node_modules`, `target`, etc.) and processes files
    /// in parallel via rayon for speed.
    pub fn build(&self, scope: &Path) {
        use ignore::WalkBuilder;
        use rayon::prelude::*;

        // Collect file paths first, then process in parallel with rayon.
        // We use WalkBuilder for directory filtering but rayon for parallelism
        // because rayon gives us better work-stealing than ignore's parallel walker
        // for CPU-bound tree-sitter parsing.
        let files: Vec<PathBuf> = WalkBuilder::new(scope)
            .follow_links(true)
            .hidden(false)
            .git_ignore(false)
            .git_global(false)
            .git_exclude(false)
            .ignore(false)
            .parents(false)
            .filter_entry(|entry| {
                if entry.file_type().is_some_and(|ft| ft.is_dir()) {
                    if let Some(name) = entry.file_name().to_str() {
                        return !crate::search::io::SKIP_DIRS.contains(&name);
                    }
                }
                true
            })
            .build()
            .filter_map(|entry| {
                let entry = entry.ok()?;
                if !entry.file_type()?.is_file() {
                    return None;
                }
                let path = entry.into_path();
                // Only index code files that have tree-sitter grammars
                if let FileType::Code(lang) = detect_file_type(&path) {
                    if outline_language(lang).is_some() {
                        // Skip oversized files
                        if let Ok(meta) = fs::metadata(&path) {
                            if meta.len() <= MAX_FILE_SIZE {
                                return Some(path);
                            }
                        }
                    }
                }
                None
            })
            .collect();

        // Process files in parallel with rayon
        let results: Vec<FileSymbols> = files
            .par_iter()
            .filter_map(|path| {
                let content = fs::read_to_string(path).ok()?;
                let mtime = fs::metadata(path)
                    .and_then(|m| m.modified())
                    .unwrap_or(SystemTime::UNIX_EPOCH);
                let symbols = extract_symbols(path, &content);
                if symbols.is_empty() {
                    // Still record the file as indexed even if no symbols found
                    Some((path.clone(), mtime, Vec::new()))
                } else {
                    Some((path.clone(), mtime, symbols))
                }
            })
            .collect();

        // Insert results into the DashMaps
        for (path, mtime, symbols) in results {
            self.indexed_files.insert(path.clone(), mtime);
            for (name, line, is_def) in symbols {
                let loc = SymbolLocation {
                    path: path.clone(),
                    line,
                    is_definition: is_def,
                    mtime,
                };
                self.symbols.entry(name).or_default().push(loc);
            }
        }
    }

    /// Check if the index has been built for the given scope.
    ///
    /// Simple heuristic: returns true if any indexed file path starts with `scope`.
    #[must_use]
    pub fn is_built(&self, scope: &Path) -> bool {
        self.indexed_files
            .iter()
            .any(|entry| entry.key().starts_with(scope))
    }

    /// Look up all locations of a symbol within `scope`.
    ///
    /// Returns matching locations filtered to paths within `scope`.
    /// Results may include stale entries if files have changed since indexing --
    /// callers can check `mtime` against the current file if freshness matters.
    #[must_use]
    pub fn lookup(&self, name: &str, scope: &Path) -> Vec<SymbolLocation> {
        let key: Arc<str> = Arc::from(name);
        let Some(locations) = self.symbols.get(&key) else {
            return Vec::new();
        };
        locations
            .iter()
            .filter(|loc| loc.path.starts_with(scope))
            .cloned()
            .collect()
    }

    /// Look up only definition locations of a symbol within `scope`.
    ///
    /// Same as `lookup` but filters to `is_definition == true`.
    #[must_use]
    pub fn lookup_definitions(&self, name: &str, scope: &Path) -> Vec<SymbolLocation> {
        let key: Arc<str> = Arc::from(name);
        let Some(locations) = self.symbols.get(&key) else {
            return Vec::new();
        };
        locations
            .iter()
            .filter(|loc| loc.is_definition && loc.path.starts_with(scope))
            .cloned()
            .collect()
    }

    /// Index a single file, updating the symbol maps.
    ///
    /// Used for incremental updates when a file changes.
    /// Removes old entries for this file before inserting new ones.
    pub fn index_file(&self, path: &Path, content: &str) {
        let mtime = fs::metadata(path)
            .and_then(|m| m.modified())
            .unwrap_or(SystemTime::UNIX_EPOCH);

        // Remove old entries for this file from all symbol lists
        let old_mtime = self.indexed_files.get(path).map(|r| *r.value());
        if old_mtime.is_some() {
            self.symbols.iter_mut().for_each(|mut entry| {
                entry.value_mut().retain(|loc| loc.path != path);
            });
        }

        // Extract and insert new symbols
        let symbols = extract_symbols(path, content);
        self.indexed_files.insert(path.to_path_buf(), mtime);

        for (name, line, is_def) in symbols {
            let loc = SymbolLocation {
                path: path.to_path_buf(),
                line,
                is_definition: is_def,
                mtime,
            };
            self.symbols.entry(name).or_default().push(loc);
        }
    }

    /// Number of unique symbol names in the index.
    #[must_use]
    pub fn symbol_count(&self) -> usize {
        self.symbols.len()
    }

    /// Number of indexed files.
    #[must_use]
    pub fn file_count(&self) -> usize {
        self.indexed_files.len()
    }
}

/// Extract all symbol definitions from a file using tree-sitter.
///
/// Returns a list of `(name, line_number, is_definition)` tuples.
/// Line numbers are 1-based (matching the convention used in search results).
///
/// Only extracts definitions (function, struct, trait, class, etc.) --
/// not usages. This keeps the index focused and compact.
fn extract_symbols(path: &Path, content: &str) -> Vec<(Arc<str>, u32, bool)> {
    let FileType::Code(lang) = detect_file_type(path) else {
        return Vec::new();
    };

    let Some(ts_lang) = outline_language(lang) else {
        return Vec::new();
    };

    let mut parser = tree_sitter::Parser::new();
    if parser.set_language(&ts_lang).is_err() {
        return Vec::new();
    }

    let Some(tree) = parser.parse(content, None) else {
        return Vec::new();
    };

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

    walk_definitions(tree.root_node(), &lines, &mut symbols, lang, 0);

    symbols
}

/// Recursively walk tree-sitter AST nodes to find all definitions.
///
/// Unlike `search::symbol::walk_for_definitions` which searches for a specific name,
/// this extracts ALL definition names for index building.
/// Depth-limited to 3 levels (matching search behavior) to avoid descending
/// into deeply nested anonymous blocks.
fn walk_definitions(
    node: tree_sitter::Node,
    lines: &[&str],
    symbols: &mut Vec<(Arc<str>, u32, bool)>,
    lang: crate::types::Lang,
    depth: usize,
) {
    if depth > 3 {
        return;
    }

    let kind = node.kind();

    if DEFINITION_KINDS.contains(&kind) {
        if let Some(name) = extract_definition_name(node, lines) {
            let line = node.start_position().row as u32 + 1;
            symbols.push((Arc::from(name.as_str()), line, true));
        }

        // For impl blocks in Rust, also index the trait name and type name
        // so lookups for "MyTrait" find `impl MyTrait for Foo`.
        if kind == "impl_item" {
            if let Some(trait_name) = crate::lang::treesitter::extract_impl_trait(node, lines) {
                let line = node.start_position().row as u32 + 1;
                symbols.push((Arc::from(trait_name.as_str()), line, true));
            }
            if let Some(type_name) = crate::lang::treesitter::extract_impl_type(node, lines) {
                let line = node.start_position().row as u32 + 1;
                symbols.push((Arc::from(type_name.as_str()), line, true));
            }
        }

        // For classes implementing interfaces, index the interface names too
        if kind == "class_declaration" || kind == "class_definition" {
            let interfaces = crate::lang::treesitter::extract_implemented_interfaces(node, lines);
            for iface in interfaces {
                let line = node.start_position().row as u32 + 1;
                symbols.push((Arc::from(iface.as_str()), line, true));
            }
        }
    } else if lang == crate::types::Lang::Elixir
        && crate::lang::treesitter::is_elixir_definition(node, lines)
    {
        // Elixir: all definitions are `call` nodes — handle separately
        if let Some(name) = crate::lang::treesitter::extract_elixir_definition_name(node, lines) {
            let line = node.start_position().row as u32 + 1;
            symbols.push((Arc::from(name.as_str()), line, true));
        }
    }

    // Recurse into children for nested definitions (impl blocks, class bodies, modules)
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_definitions(child, lines, symbols, lang, depth + 1);
    }
}

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

    #[test]
    fn test_empty_index() {
        let index = SymbolIndex::new();
        assert_eq!(index.symbol_count(), 0);
        assert_eq!(index.file_count(), 0);
        assert!(!index.is_built(Path::new("/tmp")));
        assert!(index.lookup("foo", Path::new("/tmp")).is_empty());
    }

    #[test]
    fn test_extract_symbols_rust() {
        let content = r#"
pub struct Foo {
    bar: u32,
}

impl Foo {
    pub fn baz(&self) -> u32 {
        self.bar
    }
}

trait MyTrait {
    fn do_thing(&self);
}

impl MyTrait for Foo {
    fn do_thing(&self) {}
}
"#;
        let dir = std::env::temp_dir().join("srcwalk_test_extract_symbols");
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("test.rs");
        let mut f = fs::File::create(&path).unwrap();
        f.write_all(content.as_bytes()).unwrap();

        let symbols = extract_symbols(&path, content);
        let names: Vec<&str> = symbols.iter().map(|(n, _, _)| n.as_ref()).collect();

        assert!(names.contains(&"Foo"), "should find struct Foo: {names:?}");
        assert!(names.contains(&"baz"), "should find fn baz: {names:?}");
        assert!(
            names.contains(&"MyTrait"),
            "should find trait MyTrait: {names:?}"
        );
        assert!(
            names.contains(&"do_thing"),
            "should find fn do_thing: {names:?}"
        );

        // All extracted symbols should be definitions
        assert!(symbols.iter().all(|(_, _, is_def)| *is_def));

        let _ = fs::remove_file(&path);
    }

    #[test]
    fn test_index_file() {
        let content = "pub fn hello() {}\npub fn world() {}";
        let dir = std::env::temp_dir().join("srcwalk_test_index_file");
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("test.rs");
        fs::write(&path, content).unwrap();

        let index = SymbolIndex::new();
        index.index_file(&path, content);

        assert_eq!(index.file_count(), 1);
        let results = index.lookup("hello", &dir);
        assert_eq!(results.len(), 1);
        assert!(results[0].is_definition);
        assert_eq!(results[0].line, 1);

        let results = index.lookup("world", &dir);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].line, 2);

        // Test incremental update
        let new_content = "pub fn hello() {}\npub fn updated() {}";
        fs::write(&path, new_content).unwrap();
        index.index_file(&path, new_content);

        // "world" should be gone, "updated" should be present
        assert!(index.lookup("world", &dir).is_empty());
        assert_eq!(index.lookup("updated", &dir).len(), 1);

        let _ = fs::remove_file(&path);
    }

    #[test]
    fn test_lookup_definitions_filter() {
        let content = "pub fn target() {}";
        let dir = std::env::temp_dir().join("srcwalk_test_lookup_defs");
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("test.rs");
        fs::write(&path, content).unwrap();

        let index = SymbolIndex::new();
        index.index_file(&path, content);

        let defs = index.lookup_definitions("target", &dir);
        assert_eq!(defs.len(), 1);
        assert!(defs[0].is_definition);

        // lookup_definitions with wrong scope should return empty
        let defs = index.lookup_definitions("target", Path::new("/nonexistent"));
        assert!(defs.is_empty());

        let _ = fs::remove_file(&path);
    }

    #[test]
    fn test_extract_symbols_typescript() {
        let content = r#"
function greet(name: string): string {
    return `Hello, ${name}!`;
}

class Greeter {
    greeting: string;
    constructor(message: string) {
        this.greeting = message;
    }
}

interface Printable {
    print(): void;
}
"#;
        let dir = std::env::temp_dir().join("srcwalk_test_extract_ts");
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("test.ts");
        fs::write(&path, content).unwrap();

        let symbols = extract_symbols(&path, content);
        let names: Vec<&str> = symbols.iter().map(|(n, _, _)| n.as_ref()).collect();

        assert!(
            names.contains(&"greet"),
            "should find function greet: {names:?}"
        );
        assert!(
            names.contains(&"Greeter"),
            "should find class Greeter: {names:?}"
        );
        assert!(
            names.contains(&"Printable"),
            "should find interface Printable: {names:?}"
        );

        let _ = fs::remove_file(&path);
    }

    #[test]
    fn test_extract_symbols_python() {
        let content = r#"
def hello():
    pass

class MyClass:
    def method(self):
        pass
"#;
        let dir = std::env::temp_dir().join("srcwalk_test_extract_py");
        let _ = fs::create_dir_all(&dir);
        let path = dir.join("test.py");
        fs::write(&path, content).unwrap();

        let symbols = extract_symbols(&path, content);
        let names: Vec<&str> = symbols.iter().map(|(n, _, _)| n.as_ref()).collect();

        assert!(names.contains(&"hello"), "should find def hello: {names:?}");
        assert!(
            names.contains(&"MyClass"),
            "should find class MyClass: {names:?}"
        );

        let _ = fs::remove_file(&path);
    }
}