Make disabled diagnostics an argument of corresponding function

This commit is contained in:
Igor Aleksanov 2020-08-18 13:32:29 +03:00
parent c26c911ec1
commit b56cfcca10
7 changed files with 43 additions and 39 deletions

View file

@ -4,7 +4,7 @@
//! macro-expanded files, but we need to present them to the users in terms of //! macro-expanded files, but we need to present them to the users in terms of
//! original files. So we need to map the ranges. //! original files. So we need to map the ranges.
use std::cell::RefCell; use std::{cell::RefCell, collections::HashSet};
use base_db::SourceDatabase; use base_db::SourceDatabase;
use hir::{diagnostics::DiagnosticSinkBuilder, Semantics}; use hir::{diagnostics::DiagnosticSinkBuilder, Semantics};
@ -16,7 +16,7 @@ use syntax::{
}; };
use text_edit::TextEdit; use text_edit::TextEdit;
use crate::{AnalysisConfig, Diagnostic, FileId, Fix, SourceFileEdit}; use crate::{Diagnostic, FileId, Fix, SourceFileEdit};
mod diagnostics_with_fix; mod diagnostics_with_fix;
use diagnostics_with_fix::DiagnosticWithFix; use diagnostics_with_fix::DiagnosticWithFix;
@ -31,7 +31,7 @@ pub(crate) fn diagnostics(
db: &RootDatabase, db: &RootDatabase,
file_id: FileId, file_id: FileId,
enable_experimental: bool, enable_experimental: bool,
analysis_config: &AnalysisConfig, disabled_diagnostics: Option<HashSet<String>>,
) -> Vec<Diagnostic> { ) -> Vec<Diagnostic> {
let _p = profile::span("diagnostics"); let _p = profile::span("diagnostics");
let sema = Semantics::new(db); let sema = Semantics::new(db);
@ -68,10 +68,9 @@ pub(crate) fn diagnostics(
// Only collect experimental diagnostics when they're enabled. // Only collect experimental diagnostics when they're enabled.
.filter(|diag| !diag.is_experimental() || enable_experimental); .filter(|diag| !diag.is_experimental() || enable_experimental);
if !analysis_config.disabled_diagnostics.is_empty() { if let Some(disabled_diagnostics) = disabled_diagnostics {
// Do not collect disabled diagnostics. // Do not collect disabled diagnostics.
sink_builder = sink_builder = sink_builder.filter(move |diag| !disabled_diagnostics.contains(diag.name()));
sink_builder.filter(|diag| !analysis_config.disabled_diagnostics.contains(diag.name()));
} }
// Finalize the `DiagnosticSink` building process. // Finalize the `DiagnosticSink` building process.
@ -192,10 +191,7 @@ mod tests {
use stdx::trim_indent; use stdx::trim_indent;
use test_utils::assert_eq_text; use test_utils::assert_eq_text;
use crate::{ use crate::mock_analysis::{analysis_and_position, single_file, MockAnalysis};
mock_analysis::{analysis_and_position, single_file, MockAnalysis},
AnalysisConfig,
};
use expect::{expect, Expect}; use expect::{expect, Expect};
/// Takes a multi-file input fixture with annotated cursor positions, /// Takes a multi-file input fixture with annotated cursor positions,
@ -207,7 +203,8 @@ mod tests {
let after = trim_indent(ra_fixture_after); let after = trim_indent(ra_fixture_after);
let (analysis, file_position) = analysis_and_position(ra_fixture_before); let (analysis, file_position) = analysis_and_position(ra_fixture_before);
let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap(); let diagnostic =
analysis.diagnostics(file_position.file_id, true, None).unwrap().pop().unwrap();
let mut fix = diagnostic.fix.unwrap(); let mut fix = diagnostic.fix.unwrap();
let edit = fix.source_change.source_file_edits.pop().unwrap().edit; let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
@ -233,7 +230,7 @@ mod tests {
let ra_fixture_after = &trim_indent(ra_fixture_after); let ra_fixture_after = &trim_indent(ra_fixture_after);
let (analysis, file_pos) = analysis_and_position(ra_fixture_before); let (analysis, file_pos) = analysis_and_position(ra_fixture_before);
let current_file_id = file_pos.file_id; let current_file_id = file_pos.file_id;
let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap(); let diagnostic = analysis.diagnostics(current_file_id, true, None).unwrap().pop().unwrap();
let mut fix = diagnostic.fix.unwrap(); let mut fix = diagnostic.fix.unwrap();
let edit = fix.source_change.source_file_edits.pop().unwrap(); let edit = fix.source_change.source_file_edits.pop().unwrap();
let changed_file_id = edit.file_id; let changed_file_id = edit.file_id;
@ -254,7 +251,7 @@ mod tests {
let analysis = mock.analysis(); let analysis = mock.analysis();
let diagnostics = files let diagnostics = files
.into_iter() .into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) .flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
} }
@ -267,13 +264,14 @@ mod tests {
let mock = MockAnalysis::with_files(ra_fixture); let mock = MockAnalysis::with_files(ra_fixture);
let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>(); let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>();
let mut analysis = mock.analysis(); let analysis = mock.analysis();
analysis.set_config(AnalysisConfig { disabled_diagnostics: disabled_diagnostics.clone() });
let diagnostics = files let diagnostics = files
.clone() .clone()
.into_iter() .into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) .flat_map(|file_id| {
analysis.diagnostics(file_id, true, Some(disabled_diagnostics.clone())).unwrap()
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// First, we have to check that diagnostic is not emitted when it's added to the disabled diagnostics list. // First, we have to check that diagnostic is not emitted when it's added to the disabled diagnostics list.
@ -288,11 +286,9 @@ mod tests {
// This is required for tests to not become outdated if e.g. diagnostics name changes: // This is required for tests to not become outdated if e.g. diagnostics name changes:
// without this additional run the test will pass simply because a diagnostic with an old name // without this additional run the test will pass simply because a diagnostic with an old name
// will no longer exist. // will no longer exist.
analysis.set_config(AnalysisConfig { disabled_diagnostics: Default::default() });
let diagnostics = files let diagnostics = files
.into_iter() .into_iter()
.flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) .flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert!( assert!(
@ -306,7 +302,7 @@ mod tests {
fn check_expect(ra_fixture: &str, expect: Expect) { fn check_expect(ra_fixture: &str, expect: Expect) {
let (analysis, file_id) = single_file(ra_fixture); let (analysis, file_id) = single_file(ra_fixture);
let diagnostics = analysis.diagnostics(file_id, true).unwrap(); let diagnostics = analysis.diagnostics(file_id, true, None).unwrap();
expect.assert_debug_eq(&diagnostics) expect.assert_debug_eq(&diagnostics)
} }

View file

@ -151,16 +151,11 @@ impl<T> RangeInfo<T> {
#[derive(Debug)] #[derive(Debug)]
pub struct AnalysisHost { pub struct AnalysisHost {
db: RootDatabase, db: RootDatabase,
config: AnalysisConfig,
} }
impl AnalysisHost { impl AnalysisHost {
pub fn new(lru_capacity: Option<usize>) -> Self { pub fn new(lru_capacity: Option<usize>) -> Self {
Self::with_config(lru_capacity, AnalysisConfig::default()) AnalysisHost { db: RootDatabase::new(lru_capacity) }
}
pub fn with_config(lru_capacity: Option<usize>, config: AnalysisConfig) -> Self {
AnalysisHost { db: RootDatabase::new(lru_capacity), config }
} }
pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) { pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
@ -170,7 +165,7 @@ impl AnalysisHost {
/// Returns a snapshot of the current state, which you can query for /// Returns a snapshot of the current state, which you can query for
/// semantic information. /// semantic information.
pub fn analysis(&self) -> Analysis { pub fn analysis(&self) -> Analysis {
Analysis { db: self.db.snapshot(), config: self.config.clone() } Analysis { db: self.db.snapshot() }
} }
/// Applies changes to the current state of the world. If there are /// Applies changes to the current state of the world. If there are
@ -214,7 +209,6 @@ impl Default for AnalysisHost {
#[derive(Debug)] #[derive(Debug)]
pub struct Analysis { pub struct Analysis {
db: salsa::Snapshot<RootDatabase>, db: salsa::Snapshot<RootDatabase>,
config: AnalysisConfig,
} }
// As a general design guideline, `Analysis` API are intended to be independent // As a general design guideline, `Analysis` API are intended to be independent
@ -509,8 +503,11 @@ impl Analysis {
&self, &self,
file_id: FileId, file_id: FileId,
enable_experimental: bool, enable_experimental: bool,
disabled_diagnostics: Option<HashSet<String>>,
) -> Cancelable<Vec<Diagnostic>> { ) -> Cancelable<Vec<Diagnostic>> {
self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental, &self.config)) self.with_db(|db| {
diagnostics::diagnostics(db, file_id, enable_experimental, disabled_diagnostics)
})
} }
/// Returns the edit required to rename reference at the position to the new /// Returns the edit required to rename reference at the position to the new
@ -539,11 +536,6 @@ impl Analysis {
}) })
} }
/// Sets the provided config.
pub fn set_config(&mut self, config: AnalysisConfig) {
self.config = config;
}
/// Performs an operation on that may be Canceled. /// Performs an operation on that may be Canceled.
fn with_db<F, T>(&self, f: F) -> Cancelable<T> fn with_db<F, T>(&self, f: F) -> Cancelable<T>
where where

View file

@ -71,7 +71,7 @@ impl BenchCmd {
match &self.what { match &self.what {
BenchWhat::Highlight { .. } => { BenchWhat::Highlight { .. } => {
let res = do_work(&mut host, file_id, |analysis| { let res = do_work(&mut host, file_id, |analysis| {
analysis.diagnostics(file_id, true).unwrap(); analysis.diagnostics(file_id, true, None).unwrap();
analysis.highlight_as_html(file_id, false).unwrap() analysis.highlight_as_html(file_id, false).unwrap()
}); });
if verbosity.is_verbose() { if verbosity.is_verbose() {

View file

@ -47,7 +47,7 @@ pub fn diagnostics(
String::from("unknown") String::from("unknown")
}; };
println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id));
for diagnostic in analysis.diagnostics(file_id, true).unwrap() { for diagnostic in analysis.diagnostics(file_id, true, None).unwrap() {
if matches!(diagnostic.severity, Severity::Error) { if matches!(diagnostic.severity, Severity::Error) {
found_error = true; found_error = true;
} }

View file

@ -363,6 +363,14 @@ impl Config {
self.client_caps.status_notification = get_bool("statusNotification"); self.client_caps.status_notification = get_bool("statusNotification");
} }
} }
pub fn disabled_diagnostics(&self) -> Option<HashSet<String>> {
if self.analysis.disabled_diagnostics.is_empty() {
None
} else {
Some(self.analysis.disabled_diagnostics.clone())
}
}
} }
#[derive(Deserialize)] #[derive(Deserialize)]

View file

@ -108,7 +108,7 @@ impl GlobalState {
Handle { handle, receiver } Handle { handle, receiver }
}; };
let analysis_host = AnalysisHost::with_config(config.lru_capacity, config.analysis.clone()); let analysis_host = AnalysisHost::new(config.lru_capacity);
let (flycheck_sender, flycheck_receiver) = unbounded(); let (flycheck_sender, flycheck_receiver) = unbounded();
GlobalState { GlobalState {
sender, sender,

View file

@ -770,7 +770,11 @@ fn handle_fixes(
None => {} None => {}
}; };
let diagnostics = snap.analysis.diagnostics(file_id, snap.config.experimental_diagnostics)?; let diagnostics = snap.analysis.diagnostics(
file_id,
snap.config.experimental_diagnostics,
snap.config.disabled_diagnostics(),
)?;
for fix in diagnostics for fix in diagnostics
.into_iter() .into_iter()
@ -1044,7 +1048,11 @@ pub(crate) fn publish_diagnostics(
let line_index = snap.analysis.file_line_index(file_id)?; let line_index = snap.analysis.file_line_index(file_id)?;
let diagnostics: Vec<Diagnostic> = snap let diagnostics: Vec<Diagnostic> = snap
.analysis .analysis
.diagnostics(file_id, snap.config.experimental_diagnostics)? .diagnostics(
file_id,
snap.config.experimental_diagnostics,
snap.config.disabled_diagnostics(),
)?
.into_iter() .into_iter()
.map(|d| Diagnostic { .map(|d| Diagnostic {
range: to_proto::range(&line_index, d.range), range: to_proto::range(&line_index, d.range),