From 90857ff8b08d73945598bac12a841559e86402b1 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Fri, 7 Aug 2020 14:25:55 +0300 Subject: Add an AnalysisConfig structure and use it to configure diagnostics run --- crates/ra_ide/src/diagnostics.rs | 24 +++++++++++++++++++++--- crates/ra_ide/src/lib.rs | 28 +++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 8 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 73c0b8275..33e4f1743 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -21,7 +21,7 @@ use ra_syntax::{ }; use ra_text_edit::{TextEdit, TextEditBuilder}; -use crate::{Diagnostic, FileId, FileSystemEdit, Fix, SourceFileEdit}; +use crate::{AnalysisConfig, Diagnostic, FileId, FileSystemEdit, Fix, SourceFileEdit}; #[derive(Debug, Copy, Clone)] pub enum Severity { @@ -33,6 +33,7 @@ pub(crate) fn diagnostics( db: &RootDatabase, file_id: FileId, enable_experimental: bool, + analysis_config: &AnalysisConfig, ) -> Vec { let _p = profile("diagnostics"); let sema = Semantics::new(db); @@ -41,6 +42,7 @@ pub(crate) fn diagnostics( // [#34344] Only take first 128 errors to prevent slowing down editor/ide, the number 128 is chosen arbitrarily. res.extend(parse.errors().iter().take(128).map(|err| Diagnostic { + name: None, range: err.range(), message: format!("Syntax Error: {}", err), severity: Severity::Error, @@ -52,7 +54,7 @@ pub(crate) fn diagnostics( check_struct_shorthand_initialization(&mut res, file_id, &node); } let res = RefCell::new(res); - let mut sink = DiagnosticSinkBuilder::new() + let mut sink_builder = DiagnosticSinkBuilder::new() .on::(|d| { let original_file = d.source().file_id.original_file(db); let fix = Fix::new( @@ -61,6 +63,7 @@ pub(crate) fn diagnostics( .into(), ); res.borrow_mut().push(Diagnostic { + name: Some(d.name()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -95,6 +98,7 @@ pub(crate) fn diagnostics( }; res.borrow_mut().push(Diagnostic { + name: Some(d.name()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -108,6 +112,7 @@ pub(crate) fn diagnostics( let source_change = SourceFileEdit { file_id, edit }.into(); let fix = Fix::new("Wrap with ok", source_change); res.borrow_mut().push(Diagnostic { + name: Some(d.name()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -116,6 +121,7 @@ pub(crate) fn diagnostics( }) .on::(|d| { res.borrow_mut().push(Diagnostic { + name: Some(d.name()), range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, @@ -123,10 +129,20 @@ pub(crate) fn diagnostics( }) }) // 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() { + // Do not collect disabled diagnostics. + sink_builder = sink_builder + .filter(|diag| !analysis_config.disabled_diagnostics.contains(&diag.name())); + } + + // Finalize the `DiagnosticSink` building process. + let mut sink = sink_builder // Diagnostics not handled above get no fix and default treatment. .build(|d| { res.borrow_mut().push(Diagnostic { + name: Some(d.name()), message: d.message(), range: sema.diagnostics_range(d).range, severity: Severity::Error, @@ -234,6 +250,7 @@ fn check_unnecessary_braces_in_use_statement( }); acc.push(Diagnostic { + name: None, range, message: "Unnecessary braces in use statement".to_string(), severity: Severity::WeakWarning, @@ -279,6 +296,7 @@ fn check_struct_shorthand_initialization( let edit = edit_builder.finish(); acc.push(Diagnostic { + name: None, range: record_field.syntax().text_range(), message: "Shorthand struct initialization".to_string(), severity: Severity::WeakWarning, diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index 0fede0d87..3822b9409 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -45,7 +45,7 @@ mod syntax_highlighting; mod syntax_tree; mod typing; -use std::sync::Arc; +use std::{collections::HashSet, sync::Arc}; use ra_cfg::CfgOptions; use ra_db::{ @@ -100,8 +100,15 @@ pub use ra_text_edit::{Indel, TextEdit}; pub type Cancelable = Result; +/// Configuration parameters for the analysis run. +#[derive(Debug, Default, Clone)] +pub struct AnalysisConfig { + pub disabled_diagnostics: HashSet, +} + #[derive(Debug)] pub struct Diagnostic { + pub name: Option, pub message: String, pub range: TextRange, pub severity: Severity, @@ -139,11 +146,16 @@ impl RangeInfo { #[derive(Debug)] pub struct AnalysisHost { db: RootDatabase, + config: AnalysisConfig, } impl AnalysisHost { - pub fn new(lru_capacity: Option) -> AnalysisHost { - AnalysisHost { db: RootDatabase::new(lru_capacity) } + pub fn new(lru_capacity: Option) -> Self { + Self::with_config(lru_capacity, AnalysisConfig::default()) + } + + pub fn with_config(lru_capacity: Option, config: AnalysisConfig) -> Self { + AnalysisHost { db: RootDatabase::new(lru_capacity), config } } pub fn update_lru_capacity(&mut self, lru_capacity: Option) { @@ -153,7 +165,7 @@ impl AnalysisHost { /// Returns a snapshot of the current state, which you can query for /// semantic information. pub fn analysis(&self) -> Analysis { - Analysis { db: self.db.snapshot() } + Analysis { db: self.db.snapshot(), config: self.config.clone() } } /// Applies changes to the current state of the world. If there are @@ -197,6 +209,7 @@ impl Default for AnalysisHost { #[derive(Debug)] pub struct Analysis { db: salsa::Snapshot, + config: AnalysisConfig, } // As a general design guideline, `Analysis` API are intended to be independent @@ -492,7 +505,7 @@ impl Analysis { file_id: FileId, enable_experimental: bool, ) -> Cancelable> { - self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental)) + self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental, &self.config)) } /// Returns the edit required to rename reference at the position to the new @@ -518,6 +531,11 @@ 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. fn with_db T + std::panic::UnwindSafe, T>( &self, -- cgit v1.2.3