Skip to content

Commit 5edc223

Browse files
committed
Nits
1 parent 5f8027a commit 5edc223

File tree

6 files changed

+18
-41
lines changed

6 files changed

+18
-41
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use ruff_db::system::SystemPathBuf;
1414
use ty_project::{Db, ProjectDatabase};
1515

1616
use crate::document::{FileRangeExt, ToRangeExt};
17+
use crate::session::DocumentSnapshot;
1718
use crate::session::client::Client;
18-
use crate::session::{DocumentHandle, DocumentSnapshot};
1919
use crate::system::{AnySystemPath, file_to_url};
2020
use crate::{DocumentRef, PositionEncoding, Session};
2121

@@ -115,18 +115,18 @@ impl LspDiagnostics {
115115
}
116116
}
117117

118-
/// Clears the diagnostics for the document identified by `key`.
118+
/// Clears the diagnostics for the document identified by `uri`.
119119
///
120120
/// This is done by notifying the client with an empty list of diagnostics for the document.
121121
/// For notebook cells, this clears diagnostics for the specific cell.
122122
/// For other document types, this clears diagnostics for the main document.
123-
pub(super) fn clear_diagnostics(session: &Session, document: &DocumentHandle, client: &Client) {
123+
pub(super) fn clear_diagnostics(session: &Session, uri: &lsp_types::Url, client: &Client) {
124124
if session.client_capabilities().supports_pull_diagnostics() {
125125
return;
126126
}
127127

128128
client.send_notification::<PublishDiagnostics>(PublishDiagnosticsParams {
129-
uri: document.url().clone(),
129+
uri: uri.clone(),
130130
diagnostics: vec![],
131131
version: None,
132132
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::document::DocumentKey;
12
use crate::server::Result;
23
use crate::server::api::diagnostics::{publish_diagnostics, publish_settings_diagnostics};
34
use crate::server::api::traits::{NotificationHandler, SyncNotificationHandler};
@@ -25,7 +26,8 @@ impl SyncNotificationHandler for DidChangeWatchedFiles {
2526
let mut events_by_db: FxHashMap<_, Vec<ChangeEvent>> = FxHashMap::default();
2627

2728
for change in params.changes {
28-
let path = AnySystemPath::from_url(&change.uri);
29+
let key = DocumentKey::from_url(&change.uri);
30+
let path = key.to_file_path();
2931

3032
let system_path = match path {
3133
AnySystemPath::System(system) => system,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl SyncNotificationHandler for DidCloseTextDocumentHandler {
5656
.diagnostic_mode()
5757
.is_open_files_only()
5858
{
59-
clear_diagnostics(session, &document, client);
59+
clear_diagnostics(session, document.url(), client);
6060
}
6161
}
6262
AnySystemPath::SystemVirtual(virtual_path) => {
@@ -69,7 +69,7 @@ impl SyncNotificationHandler for DidCloseTextDocumentHandler {
6969

7070
// Always clear diagnostics for virtual files, as they don't really exist on disk
7171
// which means closing them is like deleting the file.
72-
clear_diagnostics(session, &document, client);
72+
clear_diagnostics(session, document.url(), client);
7373
}
7474
}
7575

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::server::{Action, Result};
99
use crate::session::client::Client;
1010
use crate::session::index::Index;
1111
use crate::session::{SessionSnapshot, SuspendedWorkspaceDiagnosticRequest};
12-
use crate::system::{AnySystemPath, file_to_url};
12+
use crate::system::file_to_url;
1313
use lsp_server::RequestId;
1414
use lsp_types::request::WorkspaceDiagnosticRequest;
1515
use lsp_types::{
@@ -318,7 +318,7 @@ struct ResponseWriter<'a> {
318318
// It's important that we use `AnySystemPath` over `Url` here because
319319
// `file_to_url` isn't guaranteed to return the exact same URL as the one provided
320320
// by the client.
321-
previous_result_ids: FxHashMap<AnySystemPath, (Url, String)>,
321+
previous_result_ids: FxHashMap<DocumentKey, (Url, String)>,
322322
}
323323

324324
impl<'a> ResponseWriter<'a> {
@@ -347,7 +347,7 @@ impl<'a> ResponseWriter<'a> {
347347

348348
let previous_result_ids = previous_result_ids
349349
.into_iter()
350-
.map(|prev| (AnySystemPath::from_url(&prev.uri), (prev.uri, prev.value)))
350+
.map(|prev| (DocumentKey::from_url(&prev.uri), (prev.uri, prev.value)))
351351
.collect();
352352

353353
Self {
@@ -364,18 +364,16 @@ impl<'a> ResponseWriter<'a> {
364364
return;
365365
};
366366

367+
let key = DocumentKey::from_url(&url);
367368
let version = self
368369
.index
369-
.make_document_ref(&DocumentKey::from_url(&url))
370+
.make_document_ref(&key)
370371
.map(|doc| i64::from(doc.version()))
371372
.ok();
372373

373374
let result_id = Diagnostics::result_id_from_hash(diagnostics);
374375

375-
let previous_result_id = self
376-
.previous_result_ids
377-
.remove(&AnySystemPath::from_url(&url))
378-
.map(|(_url, id)| id);
376+
let previous_result_id = self.previous_result_ids.remove(&key).map(|(_url, id)| id);
379377

380378
let report = match result_id {
381379
Some(new_id) if Some(&new_id) == previous_result_id.as_ref() => {
@@ -439,11 +437,11 @@ impl<'a> ResponseWriter<'a> {
439437

440438
// Handle files that had diagnostics in previous request but no longer have any
441439
// Any remaining entries in previous_results are files that were fixed
442-
for (previous_url, previous_result_id) in self.previous_result_ids.into_values() {
440+
for (key, (previous_url, previous_result_id)) in self.previous_result_ids {
443441
// This file had diagnostics before but doesn't now, so we need to report it as having no diagnostics
444442
let version = self
445443
.index
446-
.make_document_ref(&DocumentKey::from_url(&previous_url))
444+
.make_document_ref(&key)
447445
.ok()
448446
.map(|doc| i64::from(doc.version()));
449447

crates/ty_server/src/session/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hash::FxHashMap;
1717
#[derive(Debug)]
1818
pub(crate) struct Index {
1919
/// Maps all document file paths to the associated document controller
20-
pub(super) documents: FxHashMap<DocumentKey, Document>,
20+
documents: FxHashMap<DocumentKey, Document>,
2121

2222
/// Maps opaque cell URLs to a notebook path (document)
2323
notebook_cells: FxHashMap<String, DocumentKey>,

crates/ty_server/src/system.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,6 @@ pub(crate) enum AnySystemPath {
4040
}
4141

4242
impl AnySystemPath {
43-
/// Converts the given [`Url`] to an [`AnySystemPath`].
44-
///
45-
/// If the URL scheme is `file`, then the path is converted to a [`SystemPathBuf`] unless
46-
/// the url isn't a valid file path.
47-
///
48-
/// In all other cases, the URL is converted to a [`SystemVirtualPathBuf`].
49-
pub(crate) fn from_url(url: &Url) -> Self {
50-
if url.scheme() == "file" {
51-
if let Ok(path) = url.to_file_path() {
52-
AnySystemPath::System(
53-
SystemPathBuf::from_path_buf(path).expect("URL to be valid UTF-8"),
54-
)
55-
} else {
56-
tracing::warn!(
57-
"Treating `file:` url `{url}` as virtual path as it isn't a valid file path"
58-
);
59-
AnySystemPath::SystemVirtual(SystemVirtualPath::new(url.as_str()).to_path_buf())
60-
}
61-
} else {
62-
AnySystemPath::SystemVirtual(SystemVirtualPath::new(url.as_str()).to_path_buf())
63-
}
64-
}
65-
6643
pub(crate) const fn as_system(&self) -> Option<&SystemPathBuf> {
6744
match self {
6845
AnySystemPath::System(system_path_buf) => Some(system_path_buf),

0 commit comments

Comments
 (0)