1pub mod config;
2pub mod error;
3pub mod output;
4pub mod parse;
5pub mod search;
6pub mod trace;
7pub mod tree;
8
9use std::path::PathBuf;
10
11pub use config::default_patterns;
13pub use error::{Result, SearchError};
14pub use output::TreeFormatter;
15pub use parse::{KeyExtractor, TranslationEntry, YamlParser};
16pub use search::{CodeReference, Match, PatternMatcher, TextSearcher};
17pub use trace::{
18 CallExtractor, CallGraphBuilder, CallNode, CallTree, FunctionDef, FunctionFinder,
19 TraceDirection,
20};
21pub use tree::{Location, NodeType, ReferenceTree, ReferenceTreeBuilder, TreeNode};
22
23#[derive(Debug, Clone)]
25pub struct TraceQuery {
26 pub function_name: String,
27 pub direction: TraceDirection,
28 pub max_depth: usize,
29 pub base_dir: Option<PathBuf>,
30 pub exclude_patterns: Vec<String>,
31}
32
33impl TraceQuery {
34 pub fn new(function_name: String, direction: TraceDirection, max_depth: usize) -> Self {
35 Self {
36 function_name,
37 direction,
38 max_depth,
39 base_dir: None,
40 exclude_patterns: Vec::new(),
41 }
42 }
43
44 pub fn with_base_dir(mut self, base_dir: PathBuf) -> Self {
45 self.base_dir = Some(base_dir);
46 self
47 }
48
49 pub fn with_exclusions(mut self, exclusions: Vec<String>) -> Self {
50 self.exclude_patterns = exclusions;
51 self
52 }
53}
54
55#[derive(Debug, Clone)]
57pub struct SearchQuery {
58 pub text: String,
59 pub case_sensitive: bool,
60 pub base_dir: Option<PathBuf>,
61 pub exclude_patterns: Vec<String>,
62}
63
64impl SearchQuery {
65 pub fn new(text: String) -> Self {
66 Self {
67 text,
68 case_sensitive: false,
69 base_dir: None,
70 exclude_patterns: Vec::new(),
71 }
72 }
73
74 pub fn with_case_sensitive(mut self, case_sensitive: bool) -> Self {
75 self.case_sensitive = case_sensitive;
76 self
77 }
78
79 pub fn with_base_dir(mut self, base_dir: PathBuf) -> Self {
80 self.base_dir = Some(base_dir);
81 self
82 }
83
84 pub fn with_exclusions(mut self, exclusions: Vec<String>) -> Self {
85 self.exclude_patterns = exclusions;
86 self
87 }
88}
89
90#[derive(Debug)]
92pub struct SearchResult {
93 pub query: String,
94 pub translation_entries: Vec<TranslationEntry>,
95 pub code_references: Vec<CodeReference>,
96}
97
98pub fn run_search(query: SearchQuery) -> Result<SearchResult> {
106 let base_dir = query
108 .base_dir
109 .clone()
110 .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
111
112 let project_type = config::detect_project_type(&base_dir);
114 let mut exclusions: Vec<String> = config::get_default_exclusions(project_type)
115 .iter()
116 .map(|&s| s.to_string())
117 .collect();
118 exclusions.extend(query.exclude_patterns.clone());
119
120 let mut extractor = KeyExtractor::new();
122 extractor.set_exclusions(exclusions.clone());
123 let translation_entries = extractor.extract(&base_dir, &query.text)?;
124
125 let mut matcher = PatternMatcher::new(base_dir.clone());
128 matcher.set_exclusions(exclusions.clone());
129 let mut all_code_refs = Vec::new();
130
131 for entry in &translation_entries {
132 let key_variations = generate_partial_keys(&entry.key);
134
135 for key in &key_variations {
137 let code_refs = matcher.find_usages(key)?;
138 all_code_refs.extend(code_refs);
139 }
140 }
141
142 let text_searcher = TextSearcher::new(base_dir.clone())
145 .case_sensitive(query.case_sensitive)
146 .respect_gitignore(true); if let Ok(direct_matches) = text_searcher.search(&query.text) {
149 for m in direct_matches {
150 let path_str = m.file.to_string_lossy();
152 if path_str.ends_with(".yml") || path_str.ends_with(".yaml") {
153 continue;
154 }
155
156 if exclusions.iter().any(|ex| path_str.contains(ex)) {
158 continue;
159 }
160
161 all_code_refs.push(CodeReference {
163 file: m.file,
164 line: m.line,
165 pattern: "Direct Match".to_string(),
166 context: m.content,
167 key_path: query.text.clone(), });
169 }
170 }
171
172 all_code_refs.sort_by(|a, b| a.file.cmp(&b.file).then(a.line.cmp(&b.line)));
174 all_code_refs.dedup_by(|a, b| a.file == b.file && a.line == b.line);
175
176 Ok(SearchResult {
177 query: query.text,
178 translation_entries,
179 code_references: all_code_refs,
180 })
181}
182
183pub fn run_trace(query: TraceQuery) -> Result<Option<CallTree>> {
196 let base_dir = query
197 .base_dir
198 .clone()
199 .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));
200
201 let finder = FunctionFinder::new(base_dir.clone());
202 if let Some(start_fn) = finder.find_function(&query.function_name) {
203 let extractor = CallExtractor::new(base_dir);
204 let builder = CallGraphBuilder::new(query.direction, query.max_depth, &finder, &extractor);
205 builder.build_trace(&start_fn)
206 } else {
207 Ok(None)
208 }
209}
210
211pub fn filter_translation_files(matches: &[Match]) -> Vec<PathBuf> {
213 matches
214 .iter()
215 .filter(|m| {
216 let path = m.file.to_string_lossy();
217 path.ends_with(".yml") || path.ends_with(".yaml")
218 })
219 .map(|m| m.file.clone())
220 .collect()
221}
222
223pub fn generate_partial_keys(full_key: &str) -> Vec<String> {
230 let mut keys = Vec::new();
231
232 keys.push(full_key.to_string());
234
235 let segments: Vec<&str> = full_key.split('.').collect();
236
237 if segments.len() >= 2 {
239 if segments.len() > 1 {
242 let without_first = segments[1..].join(".");
243 keys.push(without_first);
244 }
245
246 if segments.len() > 1 {
249 let without_last = segments[..segments.len() - 1].join(".");
250 keys.push(without_last);
251 }
252 }
253
254 keys
255}