mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 13:33:31 +00:00
Cleanup
This commit is contained in:
parent
b039f0d1ba
commit
12831b74af
4 changed files with 33 additions and 27 deletions
|
@ -3,7 +3,6 @@ pub(crate) mod to_proto;
|
||||||
|
|
||||||
use std::{collections::HashMap, mem, sync::Arc};
|
use std::{collections::HashMap, mem, sync::Arc};
|
||||||
|
|
||||||
use lsp_types::{Diagnostic, Range};
|
|
||||||
use ra_ide::FileId;
|
use ra_ide::FileId;
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
|
@ -19,15 +18,15 @@ pub struct DiagnosticsConfig {
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub(crate) struct DiagnosticCollection {
|
pub(crate) struct DiagnosticCollection {
|
||||||
pub(crate) native: HashMap<FileId, Vec<Diagnostic>>,
|
pub(crate) native: HashMap<FileId, Vec<lsp_types::Diagnostic>>,
|
||||||
pub(crate) check: HashMap<FileId, Vec<Diagnostic>>,
|
pub(crate) check: HashMap<FileId, Vec<lsp_types::Diagnostic>>,
|
||||||
pub(crate) check_fixes: CheckFixes,
|
pub(crate) check_fixes: CheckFixes,
|
||||||
changes: FxHashSet<FileId>,
|
changes: FxHashSet<FileId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct Fix {
|
pub(crate) struct Fix {
|
||||||
pub(crate) range: Range,
|
pub(crate) range: lsp_types::Range,
|
||||||
pub(crate) action: lsp_ext::CodeAction,
|
pub(crate) action: lsp_ext::CodeAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,7 +39,7 @@ impl DiagnosticCollection {
|
||||||
pub(crate) fn add_check_diagnostic(
|
pub(crate) fn add_check_diagnostic(
|
||||||
&mut self,
|
&mut self,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
diagnostic: Diagnostic,
|
diagnostic: lsp_types::Diagnostic,
|
||||||
fixes: Vec<lsp_ext::CodeAction>,
|
fixes: Vec<lsp_ext::CodeAction>,
|
||||||
) {
|
) {
|
||||||
let diagnostics = self.check.entry(file_id).or_default();
|
let diagnostics = self.check.entry(file_id).or_default();
|
||||||
|
@ -59,12 +58,19 @@ impl DiagnosticCollection {
|
||||||
self.changes.insert(file_id);
|
self.changes.insert(file_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_native_diagnostics(&mut self, file_id: FileId, diagnostics: Vec<Diagnostic>) {
|
pub(crate) fn set_native_diagnostics(
|
||||||
|
&mut self,
|
||||||
|
file_id: FileId,
|
||||||
|
diagnostics: Vec<lsp_types::Diagnostic>,
|
||||||
|
) {
|
||||||
self.native.insert(file_id, diagnostics);
|
self.native.insert(file_id, diagnostics);
|
||||||
self.changes.insert(file_id);
|
self.changes.insert(file_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn diagnostics_for(&self, file_id: FileId) -> impl Iterator<Item = &Diagnostic> {
|
pub(crate) fn diagnostics_for(
|
||||||
|
&self,
|
||||||
|
file_id: FileId,
|
||||||
|
) -> impl Iterator<Item = &lsp_types::Diagnostic> {
|
||||||
let native = self.native.get(&file_id).into_iter().flatten();
|
let native = self.native.get(&file_id).into_iter().flatten();
|
||||||
let check = self.check.get(&file_id).into_iter().flatten();
|
let check = self.check.get(&file_id).into_iter().flatten();
|
||||||
native.chain(check)
|
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.source == right.source
|
||||||
&& left.severity == right.severity
|
&& left.severity == right.severity
|
||||||
&& left.range == right.range
|
&& left.range == right.range
|
||||||
|
|
|
@ -167,9 +167,9 @@ fn map_rust_child_diagnostic(
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct MappedRustDiagnostic {
|
pub(crate) struct MappedRustDiagnostic {
|
||||||
pub location: Location,
|
pub(crate) location: Location,
|
||||||
pub diagnostic: Diagnostic,
|
pub(crate) diagnostic: Diagnostic,
|
||||||
pub fixes: Vec<lsp_ext::CodeAction>,
|
pub(crate) fixes: Vec<lsp_ext::CodeAction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a Rust root diagnostic to LSP form
|
/// Converts a Rust root diagnostic to LSP form
|
||||||
|
|
|
@ -195,11 +195,7 @@ impl Drop for GlobalState {
|
||||||
|
|
||||||
impl GlobalStateSnapshot {
|
impl GlobalStateSnapshot {
|
||||||
pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
|
pub(crate) fn url_to_file_id(&self, url: &Url) -> Result<FileId> {
|
||||||
let path = from_proto::abs_path(url)?;
|
url_to_file_id(&self.vfs.read().0, url)
|
||||||
let path = path.into();
|
|
||||||
let res =
|
|
||||||
self.vfs.read().0.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?;
|
|
||||||
Ok(res)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn file_id_to_url(&self, id: FileId) -> 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();
|
let path = path.as_path().unwrap();
|
||||||
url_from_abs_path(&path)
|
url_from_abs_path(&path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> Result<FileId> {
|
||||||
|
let path = from_proto::vfs_path(url)?;
|
||||||
|
let res = vfs.file_id(&path).ok_or_else(|| format!("file not found: {}", path))?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ use crate::{
|
||||||
config::Config,
|
config::Config,
|
||||||
dispatch::{NotificationDispatcher, RequestDispatcher},
|
dispatch::{NotificationDispatcher, RequestDispatcher},
|
||||||
from_proto,
|
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,
|
handlers, lsp_ext,
|
||||||
lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new},
|
lsp_utils::{apply_document_changes, is_canceled, notification_is, notification_new},
|
||||||
Result,
|
Result,
|
||||||
|
@ -200,18 +200,16 @@ impl GlobalState {
|
||||||
&workspace_root,
|
&workspace_root,
|
||||||
);
|
);
|
||||||
for diag in diagnostics {
|
for diag in diagnostics {
|
||||||
let path = from_proto::vfs_path(&diag.location.uri)?;
|
match url_to_file_id(&self.vfs.read().0, &diag.location.uri) {
|
||||||
let file_id = match self.vfs.read().0.file_id(&path) {
|
Ok(file_id) => self.diagnostics.add_check_diagnostic(
|
||||||
Some(file) => FileId(file.0),
|
file_id,
|
||||||
None => {
|
diag.diagnostic,
|
||||||
log::error!(
|
diag.fixes,
|
||||||
"File with cargo diagnostic not found in VFS: {}",
|
),
|
||||||
path
|
Err(err) => {
|
||||||
);
|
log::error!("File with cargo diagnostic not found in VFS: {}", err);
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.diagnostics.add_check_diagnostic(file_id, diag.diagnostic, diag.fixes)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue