Skip to content

Commit 4690890

Browse files
authored
ruff server: In 'publish diagnostics' mode, document diagnostics are cleared properly when a file is closed (#11137)
## Summary Fixes #11114. As part of the `onClose` handler, we publish an empty array of diagnostics for the document being closed, similar to [`ruff-lsp`](https://github.com/astral-sh/ruff-lsp/blob/187d7790be0783b9ac41ce025a724cf389bf575c/ruff_lsp/server.py#L459-L464). This prevent phantom diagnostics from lingering after a document is closed. We'll only do this if the client doesn't support pull diagnostics, because otherwise clearing diagnostics is their responsibility. ## Test Plan Diagnostics should no longer appear for a document in the Problems tab after the document is closed.
1 parent 19baabb commit 4690890

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

crates/ruff_server/src/server/api/diagnostics.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ pub(super) fn publish_diagnostics_for_document(
3232

3333
Ok(())
3434
}
35+
36+
pub(super) fn clear_diagnostics_for_document(
37+
snapshot: &DocumentSnapshot,
38+
notifier: &Notifier,
39+
) -> crate::server::Result<()> {
40+
notifier
41+
.notify::<lsp_types::notification::PublishDiagnostics>(
42+
lsp_types::PublishDiagnosticsParams {
43+
uri: snapshot.url().clone(),
44+
diagnostics: vec![],
45+
version: Some(snapshot.document().version()),
46+
},
47+
)
48+
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
49+
50+
Ok(())
51+
}

crates/ruff_server/src/server/api/notifications/did_close.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::server::api::diagnostics::clear_diagnostics_for_document;
12
use crate::server::api::LSPResult;
23
use crate::server::client::{Notifier, Requester};
34
use crate::server::Result;
@@ -15,12 +16,24 @@ impl super::SyncNotificationHandler for DidClose {
1516
#[tracing::instrument(skip_all, fields(file=%uri))]
1617
fn run(
1718
session: &mut Session,
18-
_notifier: Notifier,
19+
notifier: Notifier,
1920
_requester: &mut Requester,
2021
types::DidCloseTextDocumentParams {
2122
text_document: types::TextDocumentIdentifier { uri },
2223
}: types::DidCloseTextDocumentParams,
2324
) -> Result<()> {
25+
// Publish an empty diagnostic report for the document if the client does not support pull diagnostics.
26+
// This will de-register any existing diagnostics.
27+
if !session.resolved_client_capabilities().pull_diagnostics {
28+
let snapshot = session
29+
.take_snapshot(&uri)
30+
.ok_or_else(|| {
31+
anyhow::anyhow!("Unable to take snapshot for document with URL {uri}")
32+
})
33+
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
34+
clear_diagnostics_for_document(&snapshot, &notifier)?;
35+
}
36+
2437
session
2538
.close_document(&uri)
2639
.with_failure_code(lsp_server::ErrorCode::InternalError)

0 commit comments

Comments
 (0)