diff --git a/crates/ide-lsp/src/lib.rs b/crates/ide-lsp/src/lib.rs index ac1fd0b437..a5962ef6f6 100644 --- a/crates/ide-lsp/src/lib.rs +++ b/crates/ide-lsp/src/lib.rs @@ -3405,10 +3405,17 @@ impl LspManager { if changes.is_empty() { continue; } + // jdtls rebuilds a source file that changed on disk but keeps the diagnostics of the + // open files it checked before: a reference to a class another tool adds after + // rewriting the open file stays "cannot be resolved" until the file is reopened. + // Sending the open files again makes it check them against the new sources. + let recheck_open = jdtls && changes.iter().any(|(p, _)| p.extension().is_some_and(|e| e == "java")); let changes: Vec = changes.iter().map(|(p, c)| json!({ "uri": uri::path_to_uri(p), "type": *c as u8 })).collect(); if let Err(e) = run.peer.notify("workspace/didChangeWatchedFiles", json!({ "changes": changes })) { tracing::debug!("{}: didChangeWatchedFiles: {e}", s.name); + } else if recheck_open { + self.resync_documents(&s, run.generation); } } } diff --git a/crates/ide-lsp/tests/mock_server.rs b/crates/ide-lsp/tests/mock_server.rs index 8d17ae1896..6f17730fd5 100644 --- a/crates/ide-lsp/tests/mock_server.rs +++ b/crates/ide-lsp/tests/mock_server.rs @@ -482,6 +482,31 @@ async fn dart_file_watching_requires_an_explicit_registration() { } } +#[tokio::test(flavor = "multi_thread")] +async fn java_sources_changed_on_disk_send_the_open_files_to_jdtls_again() { + let h = harness_for("java", "java", &[("jdtls", incremental_caps())], &[]); + let (_, main) = h.open("Main.java", "class Main { String s = Helper.greet(); }"); + h.wait_status("jdtls", ServerStatus::Ready).await; + let s = h.launcher.state("jdtls"); + let resent = || { + s.notified("textDocument/didChange") + .iter() + .filter(|v| v["textDocument"]["uri"] == uri_of(&main) && v["contentChanges"][0]["text"].is_string()) + .count() + }; + // Another tool adds the class the open file refers to. + let helper = h.root.join("Helper.java"); + h.mgr.watched_files_changed(std::slice::from_ref(&helper), &[], &[], &[]); + eventually(|| s.notified("workspace/didChangeWatchedFiles").len() == 1).await; + eventually(|| resent() == 1).await; + // Files that are not Java sources leave the open files alone. + h.mgr.watched_files_changed(&[], &[h.root.join("notes.txt")], &[], &[]); + h.mgr.watched_files_changed(&[], &[h.root.join("pom.xml")], &[], &[]); + tokio::time::sleep(Duration::from_millis(200)).await; + assert_eq!(resent(), 1); + h.mgr.shutdown_all().await; +} + // ------------------------------------------------------------------ tests #[tokio::test(flavor = "multi_thread")]