cs/search/
pattern_match.rs1use crate::config::default_patterns;
2use crate::error::Result;
3use crate::parse::translation::TranslationEntry;
4use crate::search::text_search::TextSearcher;
5use regex::Regex;
6use std::path::PathBuf;
7
8#[derive(Debug, Clone, PartialEq)]
10pub struct CodeReference {
11 pub file: PathBuf,
13 pub line: usize,
15 pub pattern: String,
17 pub context: String,
19 pub key_path: String,
21}
22
23pub struct PatternMatcher {
25 exclusions: Vec<String>,
26 searcher: TextSearcher,
27 patterns: Vec<Regex>,
28}
29
30impl PatternMatcher {
31 pub fn new(base_dir: PathBuf) -> Self {
33 Self {
34 exclusions: Vec::new(),
35 searcher: TextSearcher::new(base_dir),
36 patterns: default_patterns(),
37 }
38 }
39
40 pub fn with_patterns(patterns: Vec<Regex>, base_dir: PathBuf) -> Self {
42 Self {
43 exclusions: Vec::new(),
44 searcher: TextSearcher::new(base_dir),
45 patterns,
46 }
47 }
48
49 pub fn set_exclusions(&mut self, exclusions: Vec<String>) {
51 self.exclusions = exclusions;
52 }
53
54 pub fn find_usages(&self, key_path: &str) -> Result<Vec<CodeReference>> {
56 let matches = self.searcher.search(key_path)?;
58
59 let mut code_refs = Vec::new();
60
61 for m in matches {
62 let file_str = m.file.to_string_lossy();
64 if self.exclusions.iter().any(|ex| file_str.contains(ex)) {
65 continue;
66 }
67
68 let file_str = m.file.to_string_lossy().to_lowercase();
70 if file_str.starts_with("src/")
71 || (file_str.starts_with("tests/") && !file_str.starts_with("tests/fixtures/"))
72 || file_str.ends_with("readme.md")
73 || file_str.ends_with("evaluation.md")
74 || file_str.ends_with(".md")
75 {
76 continue;
77 }
78
79 for pattern in &self.patterns {
81 if let Some(captures) = pattern.captures(&m.content) {
82 if let Some(captured_key) = captures.get(1) {
84 if captured_key.as_str() == key_path {
85 code_refs.push(CodeReference {
86 file: m.file.clone(),
87 line: m.line,
88 pattern: pattern.as_str().to_string(),
89 context: m.content.clone(),
90 key_path: key_path.to_string(),
91 });
92 break; }
94 }
95 }
96 }
97 }
98
99 Ok(code_refs)
100 }
101
102 pub fn find_usages_batch(&self, entries: &[TranslationEntry]) -> Result<Vec<CodeReference>> {
104 let mut all_refs = Vec::new();
105
106 for entry in entries {
107 let refs = self.find_usages(&entry.key)?;
108 all_refs.extend(refs);
109 }
110
111 Ok(all_refs)
112 }
113}
114
115impl Default for PatternMatcher {
116 fn default() -> Self {
117 Self::new(std::env::current_dir().unwrap())
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn test_pattern_matcher_creation() {
127 let matcher = PatternMatcher::new(std::env::current_dir().unwrap());
128 assert!(!matcher.patterns.is_empty());
129 }
130
131 #[test]
132 fn test_code_reference_creation() {
133 let code_ref = CodeReference {
134 file: PathBuf::from("test.rb"),
135 line: 10,
136 pattern: r#"I18n\.t\(['"]([^'"]+)['"]\)"#.to_string(),
137 context: r#"I18n.t('invoice.labels.add_new')"#.to_string(),
138 key_path: "invoice.labels.add_new".to_string(),
139 };
140
141 assert_eq!(code_ref.file, PathBuf::from("test.rb"));
142 assert_eq!(code_ref.line, 10);
143 assert_eq!(code_ref.key_path, "invoice.labels.add_new");
144 }
145
146 #[test]
147 fn test_pattern_matcher_with_custom_patterns() {
148 let custom_patterns = vec['"]\)"#).unwrap()];
149 let matcher =
150 PatternMatcher::with_patterns(custom_patterns, std::env::current_dir().unwrap());
151 assert_eq!(matcher.patterns.len(), 1);
152 }
153}