smart-tree 8.0.1

Smart Tree - An intelligent, AI-friendly directory visualization tool
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
//! Code relationship analyzer - "Semantic X-ray vision for codebases" - Omni
//! Tracks imports, function calls, type usage, and test relationships

use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

/// Types of relationships between files
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RelationType {
    /// Direct import/use/require
    Imports,
    /// Function defined here, called there
    FunctionCall,
    /// Type/struct/class defined here, used there
    TypeUsage,
    /// Test file testing this source
    TestedBy,
    /// Module exports this
    Exports,
    /// Tight coupling detected
    Coupled,
}

/// A relationship between two files
#[derive(Debug, Clone)]
pub struct FileRelation {
    /// Source file path
    pub source: PathBuf,
    /// Target file path
    pub target: PathBuf,
    /// Type of relationship
    pub relation_type: RelationType,
    /// Specific items involved (function names, types, etc.)
    pub items: Vec<String>,
    /// Strength of relationship (1-10)
    pub strength: u8,
}

/// Analyzes code relationships in a project
pub struct RelationAnalyzer {
    /// All discovered relationships
    relations: Vec<FileRelation>,
    /// Language-specific parsers
    parsers: HashMap<String, Box<dyn LanguageParser>>,
    /// File cache to avoid re-reading
    file_cache: HashMap<PathBuf, String>,
}

/// Language-specific parsing trait
trait LanguageParser: Send + Sync {
    /// Parse imports/uses from file content
    fn parse_imports(&self, content: &str, file_path: &Path) -> Vec<(String, Vec<String>)>;

    /// Parse function definitions
    fn parse_functions(&self, content: &str) -> Vec<String>;

    /// Parse function calls
    fn parse_function_calls(&self, content: &str) -> Vec<String>;

    /// Parse type definitions
    fn parse_types(&self, content: &str) -> Vec<String>;

    /// Parse type usages
    fn parse_type_usages(&self, content: &str) -> Vec<String>;
}

/// Rust language parser
struct RustParser;

impl LanguageParser for RustParser {
    fn parse_imports(&self, content: &str, _file_path: &Path) -> Vec<(String, Vec<String>)> {
        let mut imports = Vec::new();

        // First, handle multi-line imports by joining them
        let mut cleaned_content = String::new();
        let mut in_use = false;
        let mut use_buffer = String::new();

        for line in content.lines() {
            if line.trim_start().starts_with("use ") {
                in_use = true;
                use_buffer.push_str(line);
                use_buffer.push(' ');
            } else if in_use {
                if line.contains(';') {
                    use_buffer.push_str(line);
                    cleaned_content.push_str(&use_buffer.replace('\n', " "));
                    cleaned_content.push('\n');
                    use_buffer.clear();
                    in_use = false;
                } else {
                    use_buffer.push_str(line);
                    use_buffer.push(' ');
                }
            } else {
                cleaned_content.push_str(line);
                cleaned_content.push('\n');
            }
        }

        // Handle simple use statements: use module; or use module::item;
        let simple_use_re = Regex::new(r"use\s+([a-zA-Z0-9_:]+)(?:::([a-zA-Z0-9_]+))?;").unwrap();
        for cap in simple_use_re.captures_iter(&cleaned_content) {
            let module = cap.get(1).map_or("", |m| m.as_str());
            let item = cap.get(2).map_or(vec![], |m| vec![m.as_str().to_string()]);
            imports.push((module.to_string(), item));
        }

        // Handle complex imports: use module::{item1, item2, ...}
        let complex_use_re = Regex::new(r"use\s+([a-zA-Z0-9_:]+)::\{([^}]+)\}").unwrap();
        for cap in complex_use_re.captures_iter(&cleaned_content) {
            let module = cap.get(1).map_or("", |m| m.as_str());
            let items = cap.get(2).map_or(vec![], |m| {
                m.as_str()
                    .split(',')
                    .map(|s| {
                        // Handle nested imports like ai::AiFormatter
                        let parts: Vec<&str> = s.trim().split("::").collect();
                        if parts.len() > 1 {
                            // For ai::AiFormatter, we want to track both the submodule and item
                            imports.push((
                                format!("{}::{}", module, parts[0]),
                                vec![parts[1].to_string()],
                            ));
                        }
                        s.trim().to_string()
                    })
                    .collect()
            });
            if !items.is_empty() {
                imports.push((module.to_string(), items));
            }
        }

        // Match mod statements
        let mod_re = Regex::new(r"^\s*(?:pub\s+)?mod\s+([a-zA-Z0-9_]+)").unwrap();
        for cap in mod_re.captures_iter(content) {
            let module = cap.get(1).map_or("", |m| m.as_str());
            imports.push((module.to_string(), vec![]));
        }

        imports
    }

    fn parse_functions(&self, content: &str) -> Vec<String> {
        let fn_re = Regex::new(r"(?:pub\s+)?fn\s+([a-zA-Z0-9_]+)").unwrap();
        fn_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }

    fn parse_function_calls(&self, content: &str) -> Vec<String> {
        let call_re = Regex::new(r"([a-zA-Z0-9_]+)\s*\(").unwrap();
        call_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }

    fn parse_types(&self, content: &str) -> Vec<String> {
        let mut types = Vec::new();

        // Structs
        let struct_re = Regex::new(r"(?:pub\s+)?struct\s+([A-Z][a-zA-Z0-9_]*)").unwrap();
        types.extend(
            struct_re
                .captures_iter(content)
                .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string())),
        );

        // Enums
        let enum_re = Regex::new(r"(?:pub\s+)?enum\s+([A-Z][a-zA-Z0-9_]*)").unwrap();
        types.extend(
            enum_re
                .captures_iter(content)
                .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string())),
        );

        // Traits
        let trait_re = Regex::new(r"(?:pub\s+)?trait\s+([A-Z][a-zA-Z0-9_]*)").unwrap();
        types.extend(
            trait_re
                .captures_iter(content)
                .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string())),
        );

        types
    }

    fn parse_type_usages(&self, content: &str) -> Vec<String> {
        let type_re = Regex::new(r":\s*([A-Z][a-zA-Z0-9_]*)").unwrap();
        type_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }
}

/// Python language parser
struct PythonParser;

impl LanguageParser for PythonParser {
    fn parse_imports(&self, content: &str, _file_path: &Path) -> Vec<(String, Vec<String>)> {
        let mut imports = Vec::new();

        // import module
        let import_re = Regex::new(r"import\s+([a-zA-Z0-9_.]+)").unwrap();
        for cap in import_re.captures_iter(content) {
            let module = cap.get(1).map_or("", |m| m.as_str());
            imports.push((module.to_string(), vec![]));
        }

        // from module import items
        let from_re = Regex::new(r"from\s+([a-zA-Z0-9_.]+)\s+import\s+(.+)").unwrap();
        for cap in from_re.captures_iter(content) {
            let module = cap.get(1).map_or("", |m| m.as_str());
            let items = cap.get(2).map_or(vec![], |m| {
                m.as_str()
                    .split(',')
                    .map(|s| s.split_whitespace().next().unwrap_or("").to_string())
                    .collect()
            });
            imports.push((module.to_string(), items));
        }

        imports
    }

    fn parse_functions(&self, content: &str) -> Vec<String> {
        let fn_re = Regex::new(r"def\s+([a-zA-Z0-9_]+)").unwrap();
        fn_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }

    fn parse_function_calls(&self, content: &str) -> Vec<String> {
        let call_re = Regex::new(r"([a-zA-Z0-9_]+)\s*\(").unwrap();
        call_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .filter(|name| {
                !["if", "while", "for", "print", "len", "str", "int"].contains(&name.as_str())
            })
            .collect()
    }

    fn parse_types(&self, content: &str) -> Vec<String> {
        let class_re = Regex::new(r"class\s+([A-Z][a-zA-Z0-9_]*)").unwrap();
        class_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }

    fn parse_type_usages(&self, content: &str) -> Vec<String> {
        // Python type hints
        let type_re = Regex::new(r":\s*([A-Z][a-zA-Z0-9_\[\]]*)").unwrap();
        type_re
            .captures_iter(content)
            .filter_map(|cap| cap.get(1).map(|m| m.as_str().to_string()))
            .collect()
    }
}

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

impl RelationAnalyzer {
    /// Create a new analyzer
    pub fn new() -> Self {
        let mut parsers: HashMap<String, Box<dyn LanguageParser>> = HashMap::new();
        parsers.insert("rs".to_string(), Box::new(RustParser));
        parsers.insert("py".to_string(), Box::new(PythonParser));

        Self {
            relations: Vec::new(),
            parsers,
            file_cache: HashMap::new(),
        }
    }

    /// Analyze a directory for code relationships
    pub fn analyze_directory(&mut self, path: &Path) -> Result<()> {
        // First pass: collect all source files and their content
        self.collect_files(path)?;

        // Second pass: analyze relationships
        let files: Vec<PathBuf> = self.file_cache.keys().cloned().collect();
        for file in &files {
            self.analyze_file(file)?;
        }

        // Third pass: detect coupling and test relationships
        self.detect_coupling();
        self.detect_test_relationships();

        Ok(())
    }

    /// Collect all source files
    fn collect_files(&mut self, path: &Path) -> Result<()> {
        use walkdir::WalkDir;

        for entry in WalkDir::new(path)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            let path = entry.path();
            if let Some(ext) = path.extension() {
                if self.parsers.contains_key(ext.to_str().unwrap_or("")) {
                    // Skip files that can't be read as UTF-8
                    match fs::read_to_string(path) {
                        Ok(content) => {
                            self.file_cache.insert(path.to_path_buf(), content);
                        }
                        Err(e) => {
                            // Skip files with encoding errors or other read issues
                            eprintln!("⚠️  Skipping {}: {}", path.display(), e);
                        }
                    }
                }
            }
        }

        Ok(())
    }

    /// Analyze a single file for relationships
    fn analyze_file(&mut self, file_path: &Path) -> Result<()> {
        let content = self
            .file_cache
            .get(file_path)
            .ok_or_else(|| anyhow::anyhow!("File not in cache"))?
            .clone();

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

        if let Some(parser) = self.parsers.get(ext) {
            // Parse imports
            let imports = parser.parse_imports(&content, file_path);
            for (module, items) in imports {
                if let Some(target) = self.resolve_import(file_path, &module) {
                    self.relations.push(FileRelation {
                        source: file_path.to_path_buf(),
                        target,
                        relation_type: RelationType::Imports,
                        items,
                        strength: 8,
                    });
                }
            }

            // Parse functions and types for cross-referencing
            let _functions = parser.parse_functions(&content);
            let _types = parser.parse_types(&content);
            let _function_calls = parser.parse_function_calls(&content);
            let _type_usages = parser.parse_type_usages(&content);

            // Store for later cross-referencing
            // (In a real implementation, we'd build an index here to track
            // where functions are called and types are used, enabling deeper
            // analysis like call graphs and type dependency chains)
        }

        Ok(())
    }

    /// Resolve an import to a file path
    fn resolve_import(&self, from_file: &Path, module: &str) -> Option<PathBuf> {
        // Skip external crates
        if !module.starts_with("crate")
            && !module.starts_with("super")
            && !module.starts_with("self")
        {
            // Check if it's an internal module by looking for st:: or our crate name
            if !module.starts_with("st::") && !module.contains("::") {
                return None; // External crate
            }
        }

        // Find the src directory (project root)
        let mut src_dir = from_file.parent()?;
        while src_dir.file_name() != Some(std::ffi::OsStr::new("src")) && src_dir.parent().is_some()
        {
            src_dir = src_dir.parent()?;
        }

        // Clean up the module path
        let clean_module = module
            .trim_start_matches("crate::")
            .trim_start_matches("st::")
            .trim_start_matches("self::")
            .replace("::", "/");

        // Handle super:: imports
        let (base_dir, module_path) = if module.starts_with("super::") {
            let parent = from_file.parent()?.parent()?;
            let path = module.trim_start_matches("super::").replace("::", "/");
            (parent, path)
        } else if module.starts_with("self::") {
            let parent = from_file.parent()?;
            let path = module.trim_start_matches("self::").replace("::", "/");
            (parent, path)
        } else {
            (src_dir, clean_module)
        };

        // Try different file patterns
        let patterns = vec![
            format!("{}.rs", module_path),
            format!("{}/mod.rs", module_path),
            format!(
                "{}.rs",
                module_path.split('/').next_back().unwrap_or(&module_path)
            ),
        ];

        for pattern in patterns {
            let path = base_dir.join(&pattern);
            if self.file_cache.contains_key(&path) {
                return Some(path);
            }
        }

        None
    }

    /// Detect tightly coupled files
    fn detect_coupling(&mut self) {
        // Count bidirectional imports
        let mut import_pairs: HashMap<(PathBuf, PathBuf), u8> = HashMap::new();

        for rel in &self.relations {
            if rel.relation_type == RelationType::Imports {
                let pair = if rel.source < rel.target {
                    (rel.source.clone(), rel.target.clone())
                } else {
                    (rel.target.clone(), rel.source.clone())
                };
                *import_pairs.entry(pair).or_insert(0) += 1;
            }
        }

        // Mark bidirectional imports as coupled
        for ((file1, file2), count) in import_pairs {
            if count >= 2 {
                self.relations.push(FileRelation {
                    source: file1,
                    target: file2,
                    relation_type: RelationType::Coupled,
                    items: vec![],
                    strength: count.min(10),
                });
            }
        }
    }

    /// Detect test relationships
    fn detect_test_relationships(&mut self) {
        for file in self.file_cache.keys() {
            let file_str = file.to_string_lossy();

            // Is this a test file?
            if file_str.contains("test") || file_str.contains("_test") {
                // Find what it's testing
                let base_name = file
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("")
                    .replace("_test", "")
                    .replace("test_", "");

                // Look for matching source file
                for source in self.file_cache.keys() {
                    if source != file
                        && source
                            .file_stem()
                            .and_then(|s| s.to_str())
                            .is_some_and(|s| s == base_name)
                    {
                        self.relations.push(FileRelation {
                            source: source.clone(),
                            target: file.clone(),
                            relation_type: RelationType::TestedBy,
                            items: vec![],
                            strength: 10,
                        });
                    }
                }
            }
        }
    }

    /// Get all relationships
    pub fn get_relations(&self) -> &[FileRelation] {
        &self.relations
    }

    /// Get relationships for a specific file
    pub fn get_file_relations(&self, file: &Path) -> Vec<&FileRelation> {
        self.relations
            .iter()
            .filter(|r| r.source == file || r.target == file)
            .collect()
    }

    /// Get coupling score between two files
    pub fn get_coupling_score(&self, file1: &Path, file2: &Path) -> u8 {
        self.relations
            .iter()
            .filter(|r| {
                (r.source == file1 && r.target == file2) || (r.source == file2 && r.target == file1)
            })
            .map(|r| r.strength)
            .sum()
    }
}

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

    #[test]
    fn test_rust_parser() {
        let parser = RustParser;
        let content = r#"
use std::collections::HashMap;
use crate::scanner::{Scanner, FileInfo};
mod formatters;

pub fn process_file() {
    let scanner = Scanner::new();
}
"#;

        let imports = parser.parse_imports(content, Path::new("test.rs"));
        assert_eq!(imports.len(), 2);

        let functions = parser.parse_functions(content);
        assert_eq!(functions, vec!["process_file"]);
    }
}