diff --git a/crates/rust-analyzer/src/diagnostics.rs b/crates/rust-analyzer/src/diagnostics.rs index f3cdb842bd6..1cf50b677fc 100644 --- a/crates/rust-analyzer/src/diagnostics.rs +++ b/crates/rust-analyzer/src/diagnostics.rs @@ -3,7 +3,6 @@ use std::{collections::HashMap, mem, sync::Arc}; -use lsp_types::{Diagnostic, Range}; use ra_ide::FileId; use rustc_hash::FxHashSet; @@ -19,15 +18,15 @@ pub struct DiagnosticsConfig { #[derive(Debug, Default, Clone)] pub(crate) struct DiagnosticCollection { - pub(crate) native: HashMap>, - pub(crate) check: HashMap>, + pub(crate) native: HashMap>, + pub(crate) check: HashMap>, pub(crate) check_fixes: CheckFixes, changes: FxHashSet, } #[derive(Debug, Clone)] pub(crate) struct Fix { - pub(crate) range: Range, + pub(crate) range: lsp_types::Range, pub(crate) action: lsp_ext::CodeAction, } @@ -40,7 +39,7 @@ pub(crate) fn clear_check(&mut self) { pub(crate) fn add_check_diagnostic( &mut self, file_id: FileId, - diagnostic: Diagnostic, + diagnostic: lsp_types::Diagnostic, fixes: Vec, ) { let diagnostics = self.check.entry(file_id).or_default(); @@ -59,12 +58,19 @@ pub(crate) fn add_check_diagnostic( self.changes.insert(file_id); } - pub(crate) fn set_native_diagnostics(&mut self, file_id: FileId, diagnostics: Vec) { + pub(crate) fn set_native_diagnostics( + &mut self, + file_id: FileId, + diagnostics: Vec, + ) { self.native.insert(file_id, diagnostics); self.changes.insert(file_id); } - pub(crate) fn diagnostics_for(&self, file_id: FileId) -> impl Iterator { + pub(crate) fn diagnostics_for( + &self, + file_id: FileId, + ) -> impl Iterator { let native = self.native.get(&file_id).into_iter().flatten(); let check = self.check.get(&file_id).into_iter().flatten(); native.chain(check) @@ -78,7 +84,7 @@ pub(crate) fn take_changes(&mut self) -> Option> { } } -fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool { +fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool { left.source == right.source && left.severity == right.severity && left.range == right.range diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs index f379f5ed0ad..3eed118a9bb 100644 --- a/crates/rust-analyzer/src/diagnostics/to_proto.rs +++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs @@ -167,9 +167,9 @@ fn map_rust_child_diagnostic( #[derive(Debug)] pub(crate) struct MappedRustDiagnostic { - pub location: Location, - pub diagnostic: Diagnostic, - pub fixes: Vec, + pub(crate) location: Location, + pub(crate) diagnostic: Diagnostic, + pub(crate) fixes: Vec, } /// Converts a Rust root diagnostic to LSP form diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 7533bb319a5..4da09408317 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -195,11 +195,7 @@ fn drop(&mut self) { impl GlobalStateSnapshot { pub(crate) fn url_to_file_id(&self, url: &Url) -> Result { - let path = from_proto::abs_path(url)?; - let path = path.into(); - let res = - self.vfs.read().0.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?; - Ok(res) + url_to_file_id(&self.vfs.read().0, url) } pub(crate) fn file_id_to_url(&self, id: FileId) -> Url { @@ -239,3 +235,9 @@ pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url { let path = path.as_path().unwrap(); url_from_abs_path(&path) } + +pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result { + let path = from_proto::vfs_path(url)?; + let res = vfs.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?; + Ok(res) +} diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 162c0057e98..8fc816cbd57 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -16,7 +16,7 @@ config::Config, dispatch::{NotificationDispatcher, RequestDispatcher}, from_proto, - global_state::{file_id_to_url, GlobalState, Status}, + global_state::{file_id_to_url, url_to_file_id, GlobalState, Status}, handlers, lsp_ext, lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new}, Result, @@ -200,18 +200,16 @@ fn handle_event(&mut self, event: Event) -> Result<()> { &workspace_root, ); for diag in diagnostics { - let path = from_proto::vfs_path(&diag.location.uri)?; - let file_id = match self.vfs.read().0.file_id(&path) { - Some(file) => FileId(file.0), - None => { - log::error!( - "File with cargo diagnostic not found in VFS: {}", - path - ); - return Ok(()); + match url_to_file_id(&self.vfs.read().0, &diag.location.uri) { + Ok(file_id) => self.diagnostics.add_check_diagnostic( + file_id, + diag.diagnostic, + diag.fixes, + ), + Err(err) => { + log::error!("File with cargo diagnostic not found in VFS: {}", err); } }; - self.diagnostics.add_check_diagnostic(file_id, diag.diagnostic, diag.fixes) } }