1use anyhow::{Context, Result};
7use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
8use std::collections::HashSet;
9use std::path::{Path, PathBuf};
10use std::sync::mpsc::{RecvTimeoutError, channel};
11use std::time::{Duration, Instant};
12
13use crate::indexer::Indexer;
14use crate::models::Language;
15use crate::output;
16
17#[derive(Debug, Clone)]
19pub struct WatchConfig {
20 pub debounce_ms: u64,
23 pub quiet: bool,
25}
26
27impl Default for WatchConfig {
28 fn default() -> Self {
29 Self {
30 debounce_ms: 15000, quiet: false,
32 }
33 }
34}
35
36pub fn watch(path: &Path, indexer: Indexer, config: WatchConfig) -> Result<()> {
62 log::info!(
63 "Starting file watcher for {:?} with {}ms debounce",
64 path,
65 config.debounce_ms
66 );
67
68 let (tx, rx) = channel();
70
71 let mut watcher =
73 RecommendedWatcher::new(tx, Config::default()).context("Failed to create file watcher")?;
74
75 watcher
77 .watch(path, RecursiveMode::Recursive)
78 .context("Failed to start watching directory")?;
79
80 if !config.quiet {
81 println!(
82 "Watching for changes (debounce: {}s)...",
83 config.debounce_ms / 1000
84 );
85 }
86
87 let mut pending_files: HashSet<PathBuf> = HashSet::new();
89 let mut pending_deletions: HashSet<PathBuf> = HashSet::new();
93 let mut last_event_time: Option<Instant> = None;
94 let debounce_duration = Duration::from_millis(config.debounce_ms);
95
96 loop {
98 match rx.recv_timeout(Duration::from_millis(100)) {
100 Ok(Ok(event)) => {
101 if let Some((changed_path, is_removal)) = process_event_typed(&event) {
103 if is_removal {
104 let ext = changed_path
109 .extension()
110 .and_then(|e| e.to_str())
111 .unwrap_or("");
112 let is_code = ext.is_empty()
113 || crate::models::Language::from_extension(ext).is_supported();
114 if is_code {
115 log::debug!("Detected removal: {:?}", changed_path);
116 pending_deletions.insert(changed_path);
117 last_event_time = Some(Instant::now());
118 }
119 } else if should_watch_file(&changed_path) {
120 log::debug!("Detected change: {:?}", changed_path);
121 pending_files.insert(changed_path);
122 last_event_time = Some(Instant::now());
123 }
124 }
125 }
126 Ok(Err(e)) => {
127 log::warn!("Watch error: {}", e);
128 }
129 Err(RecvTimeoutError::Timeout) => {
130 let has_pending = !pending_files.is_empty() || !pending_deletions.is_empty();
132 if let Some(last_time) = last_event_time {
133 if has_pending && last_time.elapsed() >= debounce_duration {
134 let total_changes = pending_files.len() + pending_deletions.len();
136 if !config.quiet {
137 if pending_deletions.is_empty() {
138 println!(
139 "\nDetected {} changed file(s), reindexing...",
140 pending_files.len()
141 );
142 } else {
143 println!(
144 "\nDetected {} change(s) ({} deleted), reindexing...",
145 total_changes,
146 pending_deletions.len()
147 );
148 }
149 }
150
151 let start = Instant::now();
152 match indexer.index(path, false) {
153 Ok(stats) => {
154 let elapsed = start.elapsed();
155 if !config.quiet {
156 println!(
157 "✓ Reindexed {} files in {:.1}ms\n",
158 stats.total_files,
159 elapsed.as_secs_f64() * 1000.0
160 );
161 }
162 log::info!(
163 "Reindexed {} files in {:?}",
164 stats.total_files,
165 elapsed
166 );
167 }
168 Err(e) => {
169 output::error(&format!("✗ Reindex failed: {}", e));
170 log::error!("Reindex failed: {}", e);
171 }
172 }
173
174 pending_files.clear();
176 pending_deletions.clear();
177 last_event_time = None;
178 }
179 }
180 }
181 Err(RecvTimeoutError::Disconnected) => {
182 log::info!("Watcher channel disconnected, stopping...");
183 break;
184 }
185 }
186 }
187
188 if !config.quiet {
189 println!("Watcher stopped.");
190 }
191
192 Ok(())
193}
194
195fn process_event_typed(event: &Event) -> Option<(PathBuf, bool)> {
201 match event.kind {
202 EventKind::Remove(_) => event.paths.first().cloned().map(|p| (p, true)),
203 EventKind::Create(_) | EventKind::Modify(_) => {
204 event.paths.first().cloned().map(|p| (p, false))
205 }
206 _ => None,
207 }
208}
209
210fn process_event(event: &Event) -> Option<PathBuf> {
214 process_event_typed(event).map(|(p, _)| p)
215}
216
217fn should_watch_file(path: &Path) -> bool {
221 if let Some(file_name) = path.file_name() {
223 if file_name.to_string_lossy().starts_with('.') {
224 return false;
225 }
226 }
227
228 if path.is_dir() {
230 return false;
231 }
232
233 if let Some(ext) = path.extension() {
235 let ext_str = ext.to_string_lossy();
236 let lang = Language::from_extension(&ext_str);
237 return lang.is_supported();
238 }
239
240 false
241}
242
243#[cfg(test)]
244mod tests {
245 use super::*;
246 use std::fs;
247 use tempfile::TempDir;
248
249 #[test]
250 fn test_should_watch_rust_file() {
251 let temp = TempDir::new().unwrap();
252 let rust_file = temp.path().join("test.rs");
253 fs::write(&rust_file, "fn main() {}").unwrap();
254
255 assert!(should_watch_file(&rust_file));
256 }
257
258 #[test]
259 fn test_should_not_watch_unsupported_file() {
260 let temp = TempDir::new().unwrap();
261 let txt_file = temp.path().join("test.txt");
262 fs::write(&txt_file, "plain text").unwrap();
263
264 assert!(!should_watch_file(&txt_file));
265 }
266
267 #[test]
268 fn test_should_not_watch_hidden_file() {
269 let temp = TempDir::new().unwrap();
270 let hidden_file = temp.path().join(".hidden.rs");
271 fs::write(&hidden_file, "fn main() {}").unwrap();
272
273 assert!(!should_watch_file(&hidden_file));
274 }
275
276 #[test]
277 fn test_should_not_watch_directory() {
278 let temp = TempDir::new().unwrap();
279 let dir = temp.path().join("src");
280 fs::create_dir(&dir).unwrap();
281
282 assert!(!should_watch_file(&dir));
283 }
284
285 #[test]
286 fn test_watch_config_default() {
287 let config = WatchConfig::default();
288 assert_eq!(config.debounce_ms, 15000);
289 assert!(!config.quiet);
290 }
291
292 #[test]
293 fn test_process_event_create() {
294 let event = Event {
295 kind: EventKind::Create(notify::event::CreateKind::File),
296 paths: vec![PathBuf::from("/test/file.rs")],
297 attrs: Default::default(),
298 };
299
300 let path = process_event(&event);
301 assert!(path.is_some());
302 assert_eq!(path.unwrap(), PathBuf::from("/test/file.rs"));
303 }
304
305 #[test]
306 fn test_process_event_modify() {
307 let event = Event {
308 kind: EventKind::Modify(notify::event::ModifyKind::Data(
309 notify::event::DataChange::Any,
310 )),
311 paths: vec![PathBuf::from("/test/file.rs")],
312 attrs: Default::default(),
313 };
314
315 let path = process_event(&event);
316 assert!(path.is_some());
317 assert_eq!(path.unwrap(), PathBuf::from("/test/file.rs"));
318 }
319
320 #[test]
321 fn test_process_event_access_ignored() {
322 let event = Event {
323 kind: EventKind::Access(notify::event::AccessKind::Read),
324 paths: vec![PathBuf::from("/test/file.rs")],
325 attrs: Default::default(),
326 };
327
328 let path = process_event(&event);
329 assert!(path.is_none());
330 }
331}