From 12831b74af457ddfc06ebeb929e350574e2f35d5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Jun 2020 12:02:59 +0200 Subject: Cleanup --- crates/rust-analyzer/src/diagnostics.rs | 22 ++++++++++++++-------- crates/rust-analyzer/src/diagnostics/to_proto.rs | 6 +++--- crates/rust-analyzer/src/global_state.rs | 12 +++++++----- crates/rust-analyzer/src/main_loop.rs | 20 +++++++++----------- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/crates/rust-analyzer/src/diagnostics.rs b/crates/rust-analyzer/src/diagnostics.rs index f3cdb842b..1cf50b677 100644 --- a/crates/rust-analyzer/src/diagnostics.rs +++ b/crates/rust-analyzer/src/diagnostics.rs @@ -3,7 +3,6 @@ pub(crate) mod to_proto; 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 @@ impl DiagnosticCollection { 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 @@ impl DiagnosticCollection { 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 @@ impl DiagnosticCollection { } } -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 f379f5ed0..3eed118a9 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 7533bb319..4da094083 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -195,11 +195,7 @@ impl Drop for GlobalState { 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 162c0057e..8fc816cbd 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -16,7 +16,7 @@ use crate::{ 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 @@ impl GlobalState { &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) } } -- cgit v1.2.3