Skip to content

Commit 657fc12

Browse files
committed
Pass DocumentHandle to publish_diagnostics
1 parent 32f425f commit 657fc12

File tree

5 files changed

+16
-24
lines changed

5 files changed

+16
-24
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ty_project::{Db as _, ProjectDatabase};
1414

1515
use crate::Db;
1616
use crate::document::{FileRangeExt, ToRangeExt};
17-
use crate::session::DocumentSnapshot;
17+
use crate::session::DocumentHandle;
1818
use crate::session::client::Client;
1919
use crate::system::{AnySystemPath, file_to_url};
2020
use crate::{PositionEncoding, Session};
@@ -147,24 +147,15 @@ pub(super) fn clear_diagnostics(uri: &lsp_types::Url, client: &Client) {
147147
/// This function is a no-op for simple text files when the client supports pull diagnostics.
148148
///
149149
/// [publish diagnostics notification]: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics
150-
pub(super) fn publish_diagnostics(session: &Session, url: &lsp_types::Url, client: &Client) {
151-
let snapshot = match session.snapshot_document(url) {
152-
Ok(document) => document,
153-
Err(err) => {
154-
tracing::debug!("Failed to resolve document for URL `{}`: {}", url, err);
155-
return;
156-
}
157-
};
158-
159-
if !snapshot.document().is_cell_or_notebook()
160-
&& session.client_capabilities().supports_pull_diagnostics()
150+
pub(super) fn publish_diagnostics(session: &Session, document: &DocumentHandle, client: &Client) {
151+
if !document.is_cell_or_notebook() && session.client_capabilities().supports_pull_diagnostics()
161152
{
162153
return;
163154
}
164155

165-
let db = session.project_db(snapshot.notebook_or_file_path());
156+
let db = session.project_db(document.notebook_or_file_path());
166157

167-
let Some(diagnostics) = compute_diagnostics(db, &snapshot) else {
158+
let Some(diagnostics) = compute_diagnostics(db, document, session.position_encoding()) else {
168159
return;
169160
};
170161

@@ -173,13 +164,13 @@ pub(super) fn publish_diagnostics(session: &Session, url: &lsp_types::Url, clien
173164
client.send_notification::<PublishDiagnostics>(PublishDiagnosticsParams {
174165
uri,
175166
diagnostics,
176-
version: Some(snapshot.document().version()),
167+
version: Some(document.version()),
177168
});
178169
};
179170

180171
match diagnostics.to_lsp_diagnostics(db) {
181172
LspDiagnostics::TextDocument(diagnostics) => {
182-
publish_diagnostics_notification(url.clone(), diagnostics);
173+
publish_diagnostics_notification(document.url().clone(), diagnostics);
183174
}
184175
LspDiagnostics::NotebookDocument(cell_diagnostics) => {
185176
for (cell_url, diagnostics) in cell_diagnostics {
@@ -262,12 +253,13 @@ pub(crate) fn publish_settings_diagnostics(
262253

263254
pub(super) fn compute_diagnostics(
264255
db: &ProjectDatabase,
265-
snapshot: &DocumentSnapshot,
256+
document: &DocumentHandle,
257+
encoding: PositionEncoding,
266258
) -> Option<Diagnostics> {
267-
let Some(file) = snapshot.to_notebook_or_file(db) else {
259+
let Some(file) = document.notebook_or_file(db) else {
268260
tracing::info!(
269261
"No file found for snapshot for `{}`",
270-
snapshot.notebook_or_file_path()
262+
document.notebook_or_file_path()
271263
);
272264
return None;
273265
};
@@ -276,7 +268,7 @@ pub(super) fn compute_diagnostics(
276268

277269
Some(Diagnostics {
278270
items: diagnostics,
279-
encoding: snapshot.encoding(),
271+
encoding,
280272
file_or_notebook: file,
281273
})
282274
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ pub(super) fn file_changed(document: &DocumentHandle, session: &mut Session, cli
5555

5656
session.apply_changes(path, changes);
5757

58-
publish_diagnostics(session, document.url(), client);
58+
publish_diagnostics(session, document, client);
5959
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl SyncNotificationHandler for DidChangeWatchedFiles {
9292
);
9393
} else {
9494
for key in session.text_document_handles() {
95-
publish_diagnostics(session, key.url(), client);
95+
publish_diagnostics(session, &key, client);
9696
}
9797
}
9898

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,5 @@ pub(super) fn opened_document(document: &DocumentHandle, session: &mut Session,
8383
}
8484
}
8585

86-
publish_diagnostics(session, document.url(), client);
86+
publish_diagnostics(session, document, client);
8787
}

crates/ty_server/src/server/api/requests/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl BackgroundDocumentRequestHandler for DocumentDiagnosticRequestHandler {
3333
_client: &Client,
3434
params: DocumentDiagnosticParams,
3535
) -> Result<DocumentDiagnosticReportResult> {
36-
let diagnostics = compute_diagnostics(db, snapshot);
36+
let diagnostics = compute_diagnostics(db, snapshot.document(), snapshot.encoding());
3737

3838
let Some(diagnostics) = diagnostics else {
3939
return Ok(DocumentDiagnosticReportResult::Report(

0 commit comments

Comments
 (0)