Skip to content

Commit 285d225

Browse files
committed
fix watcher sync for cached files and gate lsp sync
1 parent d26e6de commit 285d225

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

anycode-backend/src/handlers/watch_handler.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ async fn is_file_opened(
6464
.any(|data| data.opened_files.contains(path_str))
6565
}
6666

67+
async fn is_file_cached(
68+
path_str: &str, file2code: &Arc<Mutex<HashMap<String, Code>>>
69+
) -> bool {
70+
let f2c = file2code.lock().await;
71+
f2c.contains_key(path_str)
72+
}
73+
6774
fn classify_watch_transition(
6875
last_state: FileState,
6976
current_state: FileState,
@@ -189,10 +196,14 @@ async fn process_watch_event(
189196
.unwrap_or(FileState::DoesNotExist)
190197
};
191198
let is_opened_file = is_file_opened(path_str, socket2data).await;
199+
let is_cached_in_file2code = is_file_cached(path_str, file2code).await;
192200
let is_parent_opened = is_parent_dir_opened(path, socket2data).await;
193201

194202
let watch_action = classify_watch_transition(last_state, current_state, is_opened_file);
195-
info!("watch action: {:?} for path: {:?} ", watch_action, path);
203+
info!(
204+
"watch action: is_opened_file:{is_opened_file} is_cached_in_file2code:{is_cached_in_file2code} is_parent_opened:{is_parent_opened} {:?} for path: {:?}",
205+
watch_action, path
206+
);
196207

197208
match watch_action {
198209
WatchAction::Create => {
@@ -218,8 +229,15 @@ async fn process_watch_event(
218229
}
219230
}
220231
WatchAction::Modify => {
221-
if is_opened_file {
222-
let _ = handle_file_modification(path, socket, file2code, lsp_manager).await;
232+
if is_opened_file || is_cached_in_file2code {
233+
let _ = handle_file_modification(
234+
path,
235+
socket,
236+
file2code,
237+
lsp_manager,
238+
is_opened_file,
239+
)
240+
.await;
223241
}
224242
}
225243
WatchAction::Ignore => {}
@@ -290,13 +308,12 @@ async fn handle_changes_update(
290308
}
291309
}
292310

293-
294-
295311
async fn handle_file_modification(
296312
path: &PathBuf,
297313
socket: &Arc<socketioxide::SocketIo>,
298314
file2code: &Arc<Mutex<HashMap<String, Code>>>,
299315
lsp_manager: &Arc<Mutex<LspManager>>,
316+
sync_lsp: bool,
300317
) -> Result<()> {
301318
let path_str = path
302319
.to_str()
@@ -349,8 +366,8 @@ async fn handle_file_modification(
349366
.await
350367
.map_err(|e| anyhow::anyhow!("Failed to emit edits: {}", e))?;
351368

352-
// Sync LSP
353-
if !lsp_changes.is_empty() {
369+
// Sync LSP only when file is opened by at least one live socket.
370+
if sync_lsp && !lsp_changes.is_empty() {
354371
let mut lsp = lsp_manager.lock().await;
355372
if let Some(lsp) = lsp.get(&lang).await {
356373
if let Err(e) = lsp.did_change_multi(path_str, lsp_changes).await {

0 commit comments

Comments
 (0)