Skip to content

Commit 4b026c2

Browse files
authored
Fix missing diagnostics for notebooks (#21156)
1 parent 4b758b3 commit 4b026c2

File tree

7 files changed

+35
-28
lines changed

7 files changed

+35
-28
lines changed

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use lsp_types::Url;
2-
31
use crate::{
4-
Session,
52
lint::DiagnosticsMap,
63
session::{Client, DocumentQuery, DocumentSnapshot},
74
};
@@ -22,21 +19,10 @@ pub(super) fn generate_diagnostics(snapshot: &DocumentSnapshot) -> DiagnosticsMa
2219
}
2320

2421
pub(super) fn publish_diagnostics_for_document(
25-
session: &Session,
26-
url: &Url,
22+
snapshot: &DocumentSnapshot,
2723
client: &Client,
2824
) -> crate::server::Result<()> {
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) {
25+
for (uri, diagnostics) in generate_diagnostics(snapshot) {
4026
client
4127
.send_notification::<lsp_types::notification::PublishDiagnostics>(
4228
lsp_types::PublishDiagnosticsParams {
@@ -52,14 +38,9 @@ pub(super) fn publish_diagnostics_for_document(
5238
}
5339

5440
pub(super) fn clear_diagnostics_for_document(
55-
session: &Session,
5641
query: &DocumentQuery,
5742
client: &Client,
5843
) -> crate::server::Result<()> {
59-
if session.resolved_client_capabilities().pull_diagnostics {
60-
return Ok(());
61-
}
62-
6344
client
6445
.send_notification::<lsp_types::notification::PublishDiagnostics>(
6546
lsp_types::PublishDiagnosticsParams {

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

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

34-
publish_diagnostics_for_document(session, &key.into_url(), client)?;
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+
}
3539

3640
Ok(())
3741
}

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

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

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

3235
Ok(())
3336
}

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

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

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

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(session, snapshot.query(), client)?;
30+
clear_diagnostics_for_document(snapshot.query(), client)?;
3131

3232
session
3333
.close_document(&key)

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::TextDocument;
22
use crate::server::Result;
3+
use crate::server::api::LSPResult;
34
use crate::server::api::diagnostics::publish_diagnostics_for_document;
45
use crate::session::{Client, Session};
56
use lsp_types as types;
@@ -29,7 +30,16 @@ impl super::SyncNotificationHandler for DidOpen {
2930

3031
session.open_text_document(uri.clone(), document);
3132

32-
publish_diagnostics_for_document(session, &uri, client)?;
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+
}
3343

3444
Ok(())
3545
}

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

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

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

4548
Ok(())
4649
}

0 commit comments

Comments
 (0)