code_analyze_mcp/
completion.rs1use crate::cache::AnalysisCache;
7use ignore::WalkBuilder;
8use std::path::Path;
9use tracing::instrument;
10
11#[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 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_dir.exists() {
32 return Vec::new();
33 }
34
35 let mut results = Vec::new();
36
37 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 if path == search_dir {
53 continue;
54 }
55 if let Some(file_name) = path.file_name().and_then(|n| n.to_str())
57 && file_name.starts_with(&name_prefix)
58 {
59 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#[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 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 let Some(cached) = cache.get(&cache_key) else {
94 return Vec::new();
95 };
96
97 let mut results = Vec::new();
98
99 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 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}