Skip to content

Commit 7b959ef

Browse files
authored
Avoid sending an unnecessary "clear diagnostics" message for clients supporting pull diagnostics (#21105)
1 parent 4c4ddc8 commit 7b959ef

File tree

7 files changed

+28
-35
lines changed

7 files changed

+28
-35
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
use lsp_types::Url;
2+
13
use crate::{
4+
Session,
25
lint::DiagnosticsMap,
36
session::{Client, DocumentQuery, DocumentSnapshot},
47
};
@@ -19,10 +22,21 @@ pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> DiagnosticsMa
1922
}
2023

2124
pub(super) fn publish_diagnostics_for_document(
22-
snapshot: &DocumentSnapshot,
25+
session: &Session,
26+
url: &Url,
2327
client: &Client,
2428
) -> crate::server::Result<()> {
25-
for (uri, diagnostics) in generate_diagnostics(snapshot) {
29+
// Publish diagnostics if the client doesn't support pull diagnostics
30+
if session.resolved_client_capabilities().pull_diagnostics {
31+
return Ok(());
32+
}
33+
34+
let snapshot = session
35+
.take_snapshot(url.clone())
36+
.ok_or_else(|| anyhow::anyhow!("Unable to take snapshot for document with URL {url}"))
37+
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
38+
39+
for (uri, diagnostics) in generate_diagnostics(&snapshot) {
2640
client
2741
.send_notification::<lsp_types::notification::PublishDiagnostics>(
2842
lsp_types::PublishDiagnosticsParams {
@@ -38,9 +52,14 @@ pub(super) fn publish_diagnostics_for_document(
3852
}
3953

4054
pub(super) fn clear_diagnostics_for_document(
55+
session: &Session,
4156
query: &DocumentQuery,
4257
client: &Client,
4358
) -> crate::server::Result<()> {
59+
if session.resolved_client_capabilities().pull_diagnostics {
60+
return Ok(());
61+
}
62+
4463
client
4564
.send_notification::<lsp_types::notification::PublishDiagnostics>(
4665
lsp_types::PublishDiagnosticsParams {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ impl super::SyncNotificationHandler for DidChange {
3131
.update_text_document(&key, content_changes, new_version)
3232
.with_failure_code(ErrorCode::InternalError)?;
3333

34-
// Publish diagnostics if the client doesn't support pull diagnostics
35-
if !session.resolved_client_capabilities().pull_diagnostics {
36-
let snapshot = session.take_snapshot(key.into_url()).unwrap();
37-
publish_diagnostics_for_document(&snapshot, client)?;
38-
}
34+
publish_diagnostics_for_document(session, &key.into_url(), client)?;
3935

4036
Ok(())
4137
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ impl super::SyncNotificationHandler for DidChangeNotebook {
2727
.with_failure_code(ErrorCode::InternalError)?;
2828

2929
// publish new diagnostics
30-
let snapshot = session
31-
.take_snapshot(key.into_url())
32-
.expect("snapshot should be available");
33-
publish_diagnostics_for_document(&snapshot, client)?;
30+
publish_diagnostics_for_document(session, &key.into_url(), client)?;
3431

3532
Ok(())
3633
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,13 @@ impl super::SyncNotificationHandler for DidChangeWatchedFiles {
3131
} else {
3232
// publish diagnostics for text documents
3333
for url in session.text_document_urls() {
34-
let snapshot = session
35-
.take_snapshot(url.clone())
36-
.expect("snapshot should be available");
37-
publish_diagnostics_for_document(&snapshot, client)?;
34+
publish_diagnostics_for_document(session, url, client)?;
3835
}
3936
}
4037

4138
// always publish diagnostics for notebook files (since they don't use pull diagnostics)
4239
for url in session.notebook_document_urls() {
43-
let snapshot = session
44-
.take_snapshot(url.clone())
45-
.expect("snapshot should be available");
46-
publish_diagnostics_for_document(&snapshot, client)?;
40+
publish_diagnostics_for_document(session, url, client)?;
4741
}
4842
}
4943

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl super::SyncNotificationHandler for DidClose {
2727
);
2828
return Ok(());
2929
};
30-
clear_diagnostics_for_document(snapshot.query(), client)?;
30+
clear_diagnostics_for_document(session, snapshot.query(), client)?;
3131

3232
session
3333
.close_document(&key)

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::TextDocument;
22
use crate::server::Result;
3-
use crate::server::api::LSPResult;
43
use crate::server::api::diagnostics::publish_diagnostics_for_document;
54
use crate::session::{Client, Session};
65
use lsp_types as types;
@@ -30,16 +29,7 @@ impl super::SyncNotificationHandler for DidOpen {
3029

3130
session.open_text_document(uri.clone(), document);
3231

33-
// Publish diagnostics if the client doesn't support pull diagnostics
34-
if !session.resolved_client_capabilities().pull_diagnostics {
35-
let snapshot = session
36-
.take_snapshot(uri.clone())
37-
.ok_or_else(|| {
38-
anyhow::anyhow!("Unable to take snapshot for document with URL {uri}")
39-
})
40-
.with_failure_code(lsp_server::ErrorCode::InternalError)?;
41-
publish_diagnostics_for_document(&snapshot, client)?;
42-
}
32+
publish_diagnostics_for_document(session, &uri, client)?;
4333

4434
Ok(())
4535
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ impl super::SyncNotificationHandler for DidOpenNotebook {
4040
session.open_notebook_document(uri.clone(), notebook);
4141

4242
// publish diagnostics
43-
let snapshot = session
44-
.take_snapshot(uri)
45-
.expect("snapshot should be available");
46-
publish_diagnostics_for_document(&snapshot, client)?;
43+
publish_diagnostics_for_document(session, &uri, client)?;
4744

4845
Ok(())
4946
}

0 commit comments

Comments
 (0)