diff --git a/crates/ruff_server/src/server/api/diagnostics.rs b/crates/ruff_server/src/server/api/diagnostics.rs index 6d2893028de1c..ecd153aa5fd84 100644 --- a/crates/ruff_server/src/server/api/diagnostics.rs +++ b/crates/ruff_server/src/server/api/diagnostics.rs @@ -32,3 +32,20 @@ pub(super) fn publish_diagnostics_for_document( Ok(()) } + +pub(super) fn clear_diagnostics_for_document( + snapshot: &DocumentSnapshot, + notifier: &Notifier, +) -> crate::server::Result<()> { + notifier + .notify::( + lsp_types::PublishDiagnosticsParams { + uri: snapshot.url().clone(), + diagnostics: vec![], + version: Some(snapshot.document().version()), + }, + ) + .with_failure_code(lsp_server::ErrorCode::InternalError)?; + + Ok(()) +} diff --git a/crates/ruff_server/src/server/api/notifications/did_close.rs b/crates/ruff_server/src/server/api/notifications/did_close.rs index 85dd51d5a122a..ac394597e959c 100644 --- a/crates/ruff_server/src/server/api/notifications/did_close.rs +++ b/crates/ruff_server/src/server/api/notifications/did_close.rs @@ -1,3 +1,4 @@ +use crate::server::api::diagnostics::clear_diagnostics_for_document; use crate::server::api::LSPResult; use crate::server::client::{Notifier, Requester}; use crate::server::Result; @@ -15,12 +16,24 @@ impl super::SyncNotificationHandler for DidClose { #[tracing::instrument(skip_all, fields(file=%uri))] fn run( session: &mut Session, - _notifier: Notifier, + notifier: Notifier, _requester: &mut Requester, types::DidCloseTextDocumentParams { text_document: types::TextDocumentIdentifier { uri }, }: types::DidCloseTextDocumentParams, ) -> Result<()> { + // Publish an empty diagnostic report for the document if the client does not support pull diagnostics. + // This will de-register any existing diagnostics. + if !session.resolved_client_capabilities().pull_diagnostics { + let snapshot = session + .take_snapshot(&uri) + .ok_or_else(|| { + anyhow::anyhow!("Unable to take snapshot for document with URL {uri}") + }) + .with_failure_code(lsp_server::ErrorCode::InternalError)?; + clear_diagnostics_for_document(&snapshot, ¬ifier)?; + } + session .close_document(&uri) .with_failure_code(lsp_server::ErrorCode::InternalError)