treesearch 0.1.0

Structure-aware document search CLI. Fast keyword matching over hierarchical document trees.
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
use crate::document::{Document, Node, SourceType};
use anyhow::Result;
use regex::Regex;
use std::path::Path;

/// Phase 1 tree-sitter stub: uses regex heuristics to detect function/class
/// boundaries in source code files. Each detected definition becomes a node
/// with the function/class name as title, its body as text, and line numbers.
///
/// Phase 2 will replace the regex approach with actual tree-sitter grammars.
pub struct TreeSitterParser;

/// File extensions handled by this parser (all source code files).
const CODE_EXTENSIONS: &[&str] = &[
    "rs", "py", "js", "ts", "jsx", "tsx", "go", "java", "c", "cpp", "h", "hpp", "cs", "rb",
    "php", "swift", "kt", "scala", "sh", "bash", "zsh", "fish", "lua", "r", "m", "mm", "pl",
    "pm", "ex", "exs", "erl", "hs", "ml", "mli", "clj", "cljs", "el", "vim", "sql", "graphql",
    "proto", "tf", "hcl", "zig", "nim", "v", "d", "dart", "cmake", "makefile", "dockerfile",
    "css", "scss", "sass", "less", "vue", "svelte",
];

impl super::Parser for TreeSitterParser {
    fn extensions(&self) -> &[&str] {
        CODE_EXTENSIONS
    }

    fn source_type(&self) -> SourceType {
        SourceType::Code
    }

    fn parse(&self, path: &Path, content: &str) -> Result<Document> {
        let file_name = path
            .file_name()
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default();
        let doc_id = path.to_string_lossy().to_string();
        let mut doc = Document::new(&doc_id, &file_name, SourceType::Code);

        if content.trim().is_empty() {
            doc.assign_node_ids();
            return Ok(doc);
        }

        let ext = path
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();

        let boundaries = detect_boundaries(content, &ext);

        if boundaries.is_empty() {
            // No detectable boundaries — treat entire file as one node.
            let mut node = Node::new("", &file_name);
            node.text = content.to_string();
            node.line_start = Some(1);
            node.line_end = Some(content.lines().count().max(1) as u32);
            doc.structure.push(node);
        } else {
            let lines: Vec<&str> = content.lines().collect();
            let total_lines = lines.len();

            // Capture preamble before the first boundary (imports, comments, etc.)
            if boundaries[0].line > 0 {
                let preamble_text: String = lines[..boundaries[0].line].join("\n");
                let trimmed = preamble_text.trim();
                if !trimmed.is_empty() {
                    let mut node = Node::new("", "(preamble)");
                    node.text = trimmed.to_string();
                    node.line_start = Some(1);
                    node.line_end = Some(boundaries[0].line as u32);
                    doc.structure.push(node);
                }
            }

            for (i, boundary) in boundaries.iter().enumerate() {
                let start = boundary.line;
                let end = if i + 1 < boundaries.len() {
                    boundaries[i + 1].line
                } else {
                    total_lines
                };

                let body: String = lines[start..end].join("\n");
                let trimmed = body.trim_end();

                let mut node = Node::new("", &boundary.name);
                node.text = trimmed.to_string();
                node.line_start = Some(start as u32 + 1); // 1-based
                node.line_end = Some(end as u32);

                doc.structure.push(node);
            }
        }

        doc.assign_node_ids();
        Ok(doc)
    }
}

// ---------------------------------------------------------------------------
// Boundary detection
// ---------------------------------------------------------------------------

/// A detected function/class/struct boundary.
struct Boundary {
    /// 0-based line index in the source.
    line: usize,
    /// Extracted definition name (e.g. "main", "Foo", "hello").
    name: String,
}

/// Detect function/class boundaries by matching regex patterns against each line.
fn detect_boundaries(content: &str, ext: &str) -> Vec<Boundary> {
    let patterns = get_patterns(ext);
    if patterns.is_empty() {
        return Vec::new();
    }

    let regexes: Vec<Regex> = patterns
        .iter()
        .filter_map(|p| Regex::new(p).ok())
        .collect();

    let mut boundaries = Vec::new();

    for (line_idx, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        for re in &regexes {
            if let Some(caps) = re.captures(trimmed) {
                let name = caps
                    .get(1)
                    .map(|m| m.as_str().to_string())
                    .unwrap_or_else(|| trimmed.to_string());
                boundaries.push(Boundary {
                    line: line_idx,
                    name,
                });
                break; // one match per line
            }
        }
    }

    boundaries
}

/// Return regex patterns for the given language extension.
/// Each pattern must contain a capture group (1) for the definition name.
fn get_patterns(ext: &str) -> Vec<&'static str> {
    match ext {
        // Rust
        "rs" => vec![
            r"^\s*(?:pub\s+)?(?:async\s+)?fn\s+(\w+)",
            r"^\s*(?:pub\s+)?struct\s+(\w+)",
            r"^\s*(?:pub\s+)?enum\s+(\w+)",
            r"^\s*(?:pub\s+)?trait\s+(\w+)",
            r"^\s*(?:pub\s+)?impl(?:<[^>]*>)?\s+(\w+)",
            r"^\s*(?:pub\s+)?mod\s+(\w+)",
        ],
        // Python
        "py" => vec![
            r"^(?:async\s+)?def\s+(\w+)",
            r"^class\s+(\w+)",
        ],
        // JavaScript / TypeScript
        "js" | "jsx" | "ts" | "tsx" | "vue" | "svelte" => vec![
            r"^\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)",
            r"^\s*(?:export\s+)?class\s+(\w+)",
            r"^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?\(",
            r"^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?(?:\([^)]*\)|[^=])\s*=>",
            r"^\s*(?:export\s+)?interface\s+(\w+)",
            r"^\s*(?:export\s+)?type\s+(\w+)",
        ],
        // Go
        "go" => vec![
            r"^func\s+(?:\([^)]+\)\s+)?(\w+)",
            r"^type\s+(\w+)\s+struct",
            r"^type\s+(\w+)\s+interface",
        ],
        // Java / Kotlin / Scala
        "java" | "kt" | "scala" => vec![
            r"(?:public|private|protected|static|final|abstract)?\s*(?:class|interface|enum)\s+(\w+)",
            r"(?:public|private|protected|static|final|abstract|override)?\s*(?:fun|void|int|String|boolean|long|double|float|Object|var|val)\s+(\w+)\s*\(",
        ],
        // C / C++
        "c" | "cpp" | "h" | "hpp" | "cc" | "cxx" => vec![
            r"^\s*(?:static\s+)?(?:inline\s+)?(?:virtual\s+)?(?:const\s+)?(?:\w+[\s*&]+)+(\w+)\s*\([^;]*$",
            r"^\s*(?:class|struct|enum)\s+(\w+)",
            r"^\s*namespace\s+(\w+)",
        ],
        // C#
        "cs" => vec![
            r"(?:public|private|protected|internal|static|virtual|abstract|override|async)?\s*(?:class|interface|struct|enum)\s+(\w+)",
            r"(?:public|private|protected|internal|static|virtual|abstract|override|async)?\s*(?:void|int|string|bool|Task|var|dynamic|\w+)\s+(\w+)\s*\(",
        ],
        // Ruby
        "rb" => vec![
            r"^\s*def\s+(\w+)",
            r"^\s*class\s+(\w+)",
            r"^\s*module\s+(\w+)",
        ],
        // PHP
        "php" => vec![
            r"^\s*(?:public|private|protected|static)?\s*function\s+(\w+)",
            r"^\s*class\s+(\w+)",
        ],
        // Swift
        "swift" => vec![
            r"^\s*(?:public|private|internal|open|fileprivate)?\s*(?:static|class)?\s*func\s+(\w+)",
            r"^\s*(?:public|private|internal|open|fileprivate)?\s*class\s+(\w+)",
            r"^\s*(?:public|private|internal|open|fileprivate)?\s*struct\s+(\w+)",
            r"^\s*(?:public|private|internal|open|fileprivate)?\s*enum\s+(\w+)",
            r"^\s*(?:public|private|internal|open|fileprivate)?\s*protocol\s+(\w+)",
        ],
        // Lua
        "lua" => vec![
            r"^\s*(?:local\s+)?function\s+(\w[\w.:]*)",
        ],
        // Shell
        "sh" | "bash" | "zsh" | "fish" => vec![
            r"^\s*(?:function\s+)?(\w+)\s*\(\)",
            r"^\s*function\s+(\w+)",
        ],
        // Elixir
        "ex" | "exs" => vec![
            r"^\s*def[p]?\s+(\w+)",
            r"^\s*defmodule\s+(\w[\w.]*)",
        ],
        // Haskell
        "hs" => vec![
            r"^(\w+)\s+::",
        ],
        // SQL
        "sql" => vec![
            r"(?i)^\s*CREATE\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|FUNCTION|PROCEDURE|INDEX)\s+(?:IF\s+NOT\s+EXISTS\s+)?(\w+)",
        ],
        // Dart
        "dart" => vec![
            r"^\s*(?:abstract\s+)?class\s+(\w+)",
            r"^\s*(?:\w+\s+)*(\w+)\s*\([^)]*\)\s*(?:async\s*)?\{",
        ],
        // Zig
        "zig" => vec![
            r"^\s*(?:pub\s+)?fn\s+(\w+)",
            r"^\s*(?:pub\s+)?const\s+(\w+)\s*=\s*struct",
        ],
        // Nim
        "nim" => vec![
            r"^\s*(?:proc|func|method|template|macro)\s+(\w+)",
            r"^\s*type\s+(\w+)",
        ],
        // Default: no patterns → file treated as a single node
        _ => vec![],
    }
}

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

    fn parse_code(content: &str, filename: &str) -> Document {
        let parser = TreeSitterParser;
        parser
            .parse(Path::new(filename), content)
            .expect("parse failed")
    }

    #[test]
    fn test_empty() {
        let doc = parse_code("", "main.rs");
        assert!(doc.structure.is_empty());
    }

    #[test]
    fn test_rust_functions() {
        let content = "use std::io;\n\nfn hello() {\n    println!(\"hello\");\n}\n\npub fn world() {\n    println!(\"world\");\n}\n";
        let doc = parse_code(content, "lib.rs");
        assert_eq!(doc.source_type, SourceType::Code);
        // preamble + two functions
        assert_eq!(doc.structure.len(), 3);
        assert_eq!(doc.structure[0].title, "(preamble)");
        assert_eq!(doc.structure[1].title, "hello");
        assert_eq!(doc.structure[2].title, "world");
    }

    #[test]
    fn test_rust_struct_and_impl() {
        let content = "pub struct Foo {\n    x: i32,\n}\n\nimpl Foo {\n    pub fn new() -> Self {\n        Self { x: 0 }\n    }\n}\n";
        let doc = parse_code(content, "foo.rs");
        assert!(doc.structure.len() >= 2);
        assert_eq!(doc.structure[0].title, "Foo"); // struct
    }

    #[test]
    fn test_python_functions() {
        let content = "import os\n\ndef hello():\n    print('hello')\n\nclass Foo:\n    def bar(self):\n        pass\n";
        let doc = parse_code(content, "app.py");
        assert!(doc.structure.len() >= 3);
        assert_eq!(doc.structure[1].title, "hello");
        assert_eq!(doc.structure[2].title, "Foo");
    }

    #[test]
    fn test_javascript_functions() {
        let content = "\nfunction greet(name) {\n    return `Hello ${name}`;\n}\n\nclass App {\n    constructor() {}\n}\n";
        let doc = parse_code(content, "app.js");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"greet"));
        assert!(titles.contains(&"App"));
    }

    #[test]
    fn test_go_functions() {
        let content = "package main\n\nfunc main() {\n    fmt.Println(\"hello\")\n}\n\nfunc (s *Server) Start() error {\n    return nil\n}\n\ntype Config struct {\n    Port int\n}\n";
        let doc = parse_code(content, "main.go");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"main"));
        assert!(titles.contains(&"Config"));
    }

    #[test]
    fn test_line_numbers() {
        let content = "fn foo() {\n    1\n}\n\nfn bar() {\n    2\n}\n";
        let doc = parse_code(content, "test.rs");
        let foo = &doc.structure[0];
        assert_eq!(foo.title, "foo");
        assert_eq!(foo.line_start, Some(1));
        let bar = &doc.structure[1];
        assert_eq!(bar.title, "bar");
        assert_eq!(bar.line_start, Some(5));
    }

    #[test]
    fn test_no_boundaries_fallback() {
        let content = "some random content\nwith lines\n";
        let doc = parse_code(content, "data.xyz");
        // No patterns for .xyz → single-node fallback
        assert_eq!(doc.structure.len(), 1);
        assert!(doc.structure[0].text.contains("some random content"));
    }

    #[test]
    fn test_node_ids_assigned() {
        let content = "fn a() {}\n\nfn b() {}\n";
        let doc = parse_code(content, "test.rs");
        for (i, node) in doc.structure.iter().enumerate() {
            assert_eq!(node.node_id, i.to_string());
        }
    }

    #[test]
    fn test_source_type() {
        let doc = parse_code("fn main() {}", "main.rs");
        assert_eq!(doc.source_type, SourceType::Code);
        assert_eq!(doc.doc_id, "main.rs");
    }

    #[test]
    fn test_ruby() {
        let content = "class Foo\n  def bar\n    puts 'hi'\n  end\nend\n";
        let doc = parse_code(content, "app.rb");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"Foo"));
    }

    #[test]
    fn test_shell() {
        let content = "#!/bin/bash\n\nhello() {\n    echo 'hi'\n}\n\nfunction world {\n    echo 'world'\n}\n";
        let doc = parse_code(content, "script.sh");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"hello"));
        assert!(titles.contains(&"world"));
    }

    #[test]
    fn test_sql() {
        let content = "CREATE TABLE users (\n  id INT PRIMARY KEY\n);\n\nCREATE VIEW active_users AS\nSELECT * FROM users;\n";
        let doc = parse_code(content, "schema.sql");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"users"));
        assert!(titles.contains(&"active_users"));
    }

    #[test]
    fn test_preamble_included() {
        let content = "// Copyright 2024\n// License: MIT\n\nfn main() {}\n";
        let doc = parse_code(content, "main.rs");
        assert_eq!(doc.structure[0].title, "(preamble)");
        assert!(doc.structure[0].text.contains("Copyright"));
    }

    #[test]
    fn test_rust_enum_and_trait() {
        let content = "pub enum Color {\n    Red,\n    Blue,\n}\n\npub trait Drawable {\n    fn draw(&self);\n}\n";
        let doc = parse_code(content, "lib.rs");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"Color"));
        assert!(titles.contains(&"Drawable"));
    }

    #[test]
    fn test_python_async_def() {
        let content = "async def fetch_data():\n    pass\n\ndef sync_func():\n    pass\n";
        let doc = parse_code(content, "app.py");
        let titles: Vec<&str> = doc.structure.iter().map(|n| n.title.as_str()).collect();
        assert!(titles.contains(&"fetch_data"));
        assert!(titles.contains(&"sync_func"));
    }
}