Skip to main content

code_analyze_mcp/
completion.rs

1//! Path completion support for file and directory paths.
2//!
3//! Provides completion suggestions for partial paths within a directory tree,
4//! respecting .gitignore and .ignore files.
5
6use crate::cache::AnalysisCache;
7use ignore::WalkBuilder;
8use std::path::Path;
9use tracing::instrument;
10
11/// Get path completions for a given prefix within a root directory.
12/// Uses ignore crate with standard filters to respect `.gitignore`.
13/// Returns matching file and directory paths up to 100 results.
14#[instrument(skip_all, fields(prefix = %prefix))]
15pub fn path_completions(root: &Path, prefix: &str) -> Vec<String> {
16    if prefix.is_empty() {
17        return Vec::new();
18    }
19
20    // Determine the search directory and filename prefix
21    let (search_dir, name_prefix) = if let Some(last_slash) = prefix.rfind('/') {
22        let dir_part = &prefix[..=last_slash];
23        let name_part = &prefix[last_slash + 1..];
24        let full_path = root.join(dir_part);
25        (full_path, name_part.to_string())
26    } else {
27        (root.to_path_buf(), prefix.to_string())
28    };
29
30    // If search directory doesn't exist, return empty
31    if !search_dir.exists() {
32        return Vec::new();
33    }
34
35    let mut results = Vec::new();
36
37    // Walk with depth 1 to get immediate children
38    let mut builder = WalkBuilder::new(&search_dir);
39    builder
40        .hidden(true)
41        .standard_filters(true)
42        .max_depth(Some(1));
43
44    for result in builder.build() {
45        if results.len() >= 100 {
46            break;
47        }
48
49        let Ok(entry) = result else { continue };
50        let path = entry.path();
51        // Skip the root directory itself
52        if path == search_dir {
53            continue;
54        }
55        // Get the filename
56        if let Some(file_name) = path.file_name().and_then(|n| n.to_str())
57            && file_name.starts_with(&name_prefix)
58        {
59            // Construct relative path from root
60            if let Ok(rel_path) = path.strip_prefix(root) {
61                let rel_str = rel_path.to_string_lossy().to_string();
62                results.push(rel_str);
63            }
64        }
65    }
66
67    results
68}
69
70/// Get symbol completions (function and class names) for a given file path.
71/// Looks up cached [`AnalysisCache`] and extracts matching symbols.
72/// Returns matching function and class names up to 100 results.
73#[instrument(skip(cache), fields(path = %path.display(), prefix = %prefix))]
74pub fn symbol_completions(cache: &AnalysisCache, path: &Path, prefix: &str) -> Vec<String> {
75    if prefix.is_empty() {
76        return Vec::new();
77    }
78
79    // Get file metadata for cache key
80    let cache_key = match std::fs::metadata(path) {
81        Ok(meta) => match meta.modified() {
82            Ok(mtime) => crate::cache::CacheKey {
83                path: path.to_path_buf(),
84                modified: mtime,
85                mode: crate::types::AnalysisMode::FileDetails,
86            },
87            Err(_) => return Vec::new(),
88        },
89        Err(_) => return Vec::new(),
90    };
91
92    // Look up in cache
93    let Some(cached) = cache.get(&cache_key) else {
94        return Vec::new();
95    };
96
97    let mut results = Vec::new();
98
99    // Extract function names matching prefix
100    for func in &cached.semantic.functions {
101        if results.len() >= 100 {
102            break;
103        }
104        if func.name.starts_with(prefix) {
105            results.push(func.name.clone());
106        }
107    }
108
109    // Extract class names matching prefix
110    for class in &cached.semantic.classes {
111        if results.len() >= 100 {
112            break;
113        }
114        if class.name.starts_with(prefix) {
115            results.push(class.name.clone());
116        }
117    }
118
119    results
120}