aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src
diff options
context:
space:
mode:
authorIgor Aleksanov <[email protected]>2020-08-18 11:32:29 +0100
committerIgor Aleksanov <[email protected]>2020-08-18 11:32:29 +0100
commitb56cfcca10994ec2bf1878f222afdb375459f8d3 (patch)
treec23565cfeb56d8ffcac7f56ca35e22f10b19c210 /crates/ide/src
parentc26c911ec1e6c2ad1dcb7d155a6a1d528839ad1a (diff)
Make disabled diagnostics an argument of corresponding function
Diffstat (limited to 'crates/ide/src')
-rw-r--r--crates/ide/src/diagnostics.rs36
-rw-r--r--crates/ide/src/lib.rs20
2 files changed, 22 insertions, 34 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index a54df37db..606a6064b 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -4,7 +4,7 @@
4//! macro-expanded files, but we need to present them to the users in terms of 4//! macro-expanded files, but we need to present them to the users in terms of
5//! original files. So we need to map the ranges. 5//! original files. So we need to map the ranges.
6 6
7use std::cell::RefCell; 7use std::{cell::RefCell, collections::HashSet};
8 8
9use base_db::SourceDatabase; 9use base_db::SourceDatabase;
10use hir::{diagnostics::DiagnosticSinkBuilder, Semantics}; 10use hir::{diagnostics::DiagnosticSinkBuilder, Semantics};
@@ -16,7 +16,7 @@ use syntax::{
16}; 16};
17use text_edit::TextEdit; 17use text_edit::TextEdit;
18 18
19use crate::{AnalysisConfig, Diagnostic, FileId, Fix, SourceFileEdit}; 19use crate::{Diagnostic, FileId, Fix, SourceFileEdit};
20 20
21mod diagnostics_with_fix; 21mod diagnostics_with_fix;
22use diagnostics_with_fix::DiagnosticWithFix; 22use diagnostics_with_fix::DiagnosticWithFix;
@@ -31,7 +31,7 @@ pub(crate) fn diagnostics(
31 db: &RootDatabase, 31 db: &RootDatabase,
32 file_id: FileId, 32 file_id: FileId,
33 enable_experimental: bool, 33 enable_experimental: bool,
34 analysis_config: &AnalysisConfig, 34 disabled_diagnostics: Option<HashSet<String>>,
35) -> Vec<Diagnostic> { 35) -> Vec<Diagnostic> {
36 let _p = profile::span("diagnostics"); 36 let _p = profile::span("diagnostics");
37 let sema = Semantics::new(db); 37 let sema = Semantics::new(db);
@@ -68,10 +68,9 @@ pub(crate) fn diagnostics(
68 // Only collect experimental diagnostics when they're enabled. 68 // Only collect experimental diagnostics when they're enabled.
69 .filter(|diag| !diag.is_experimental() || enable_experimental); 69 .filter(|diag| !diag.is_experimental() || enable_experimental);
70 70
71 if !analysis_config.disabled_diagnostics.is_empty() { 71 if let Some(disabled_diagnostics) = disabled_diagnostics {
72 // Do not collect disabled diagnostics. 72 // Do not collect disabled diagnostics.
73 sink_builder = 73 sink_builder = sink_builder.filter(move |diag| !disabled_diagnostics.contains(diag.name()));
74 sink_builder.filter(|diag| !analysis_config.disabled_diagnostics.contains(diag.name()));
75 } 74 }
76 75
77 // Finalize the `DiagnosticSink` building process. 76 // Finalize the `DiagnosticSink` building process.
@@ -192,10 +191,7 @@ mod tests {
192 use stdx::trim_indent; 191 use stdx::trim_indent;
193 use test_utils::assert_eq_text; 192 use test_utils::assert_eq_text;
194 193
195 use crate::{ 194 use crate::mock_analysis::{analysis_and_position, single_file, MockAnalysis};
196 mock_analysis::{analysis_and_position, single_file, MockAnalysis},
197 AnalysisConfig,
198 };
199 use expect::{expect, Expect}; 195 use expect::{expect, Expect};
200 196
201 /// Takes a multi-file input fixture with annotated cursor positions, 197 /// Takes a multi-file input fixture with annotated cursor positions,
@@ -207,7 +203,8 @@ mod tests {
207 let after = trim_indent(ra_fixture_after); 203 let after = trim_indent(ra_fixture_after);
208 204
209 let (analysis, file_position) = analysis_and_position(ra_fixture_before); 205 let (analysis, file_position) = analysis_and_position(ra_fixture_before);
210 let diagnostic = analysis.diagnostics(file_position.file_id, true).unwrap().pop().unwrap(); 206 let diagnostic =
207 analysis.diagnostics(file_position.file_id, true, None).unwrap().pop().unwrap();
211 let mut fix = diagnostic.fix.unwrap(); 208 let mut fix = diagnostic.fix.unwrap();
212 let edit = fix.source_change.source_file_edits.pop().unwrap().edit; 209 let edit = fix.source_change.source_file_edits.pop().unwrap().edit;
213 let target_file_contents = analysis.file_text(file_position.file_id).unwrap(); 210 let target_file_contents = analysis.file_text(file_position.file_id).unwrap();
@@ -233,7 +230,7 @@ mod tests {
233 let ra_fixture_after = &trim_indent(ra_fixture_after); 230 let ra_fixture_after = &trim_indent(ra_fixture_after);
234 let (analysis, file_pos) = analysis_and_position(ra_fixture_before); 231 let (analysis, file_pos) = analysis_and_position(ra_fixture_before);
235 let current_file_id = file_pos.file_id; 232 let current_file_id = file_pos.file_id;
236 let diagnostic = analysis.diagnostics(current_file_id, true).unwrap().pop().unwrap(); 233 let diagnostic = analysis.diagnostics(current_file_id, true, None).unwrap().pop().unwrap();
237 let mut fix = diagnostic.fix.unwrap(); 234 let mut fix = diagnostic.fix.unwrap();
238 let edit = fix.source_change.source_file_edits.pop().unwrap(); 235 let edit = fix.source_change.source_file_edits.pop().unwrap();
239 let changed_file_id = edit.file_id; 236 let changed_file_id = edit.file_id;
@@ -254,7 +251,7 @@ mod tests {
254 let analysis = mock.analysis(); 251 let analysis = mock.analysis();
255 let diagnostics = files 252 let diagnostics = files
256 .into_iter() 253 .into_iter()
257 .flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) 254 .flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
258 .collect::<Vec<_>>(); 255 .collect::<Vec<_>>();
259 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics); 256 assert_eq!(diagnostics.len(), 0, "unexpected diagnostics:\n{:#?}", diagnostics);
260 } 257 }
@@ -267,13 +264,14 @@ mod tests {
267 264
268 let mock = MockAnalysis::with_files(ra_fixture); 265 let mock = MockAnalysis::with_files(ra_fixture);
269 let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>(); 266 let files = mock.files().map(|(it, _)| it).collect::<Vec<_>>();
270 let mut analysis = mock.analysis(); 267 let analysis = mock.analysis();
271 analysis.set_config(AnalysisConfig { disabled_diagnostics: disabled_diagnostics.clone() });
272 268
273 let diagnostics = files 269 let diagnostics = files
274 .clone() 270 .clone()
275 .into_iter() 271 .into_iter()
276 .flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) 272 .flat_map(|file_id| {
273 analysis.diagnostics(file_id, true, Some(disabled_diagnostics.clone())).unwrap()
274 })
277 .collect::<Vec<_>>(); 275 .collect::<Vec<_>>();
278 276
279 // First, we have to check that diagnostic is not emitted when it's added to the disabled diagnostics list. 277 // 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 {
288 // This is required for tests to not become outdated if e.g. diagnostics name changes: 286 // This is required for tests to not become outdated if e.g. diagnostics name changes:
289 // without this additional run the test will pass simply because a diagnostic with an old name 287 // without this additional run the test will pass simply because a diagnostic with an old name
290 // will no longer exist. 288 // will no longer exist.
291 analysis.set_config(AnalysisConfig { disabled_diagnostics: Default::default() });
292
293 let diagnostics = files 289 let diagnostics = files
294 .into_iter() 290 .into_iter()
295 .flat_map(|file_id| analysis.diagnostics(file_id, true).unwrap()) 291 .flat_map(|file_id| analysis.diagnostics(file_id, true, None).unwrap())
296 .collect::<Vec<_>>(); 292 .collect::<Vec<_>>();
297 293
298 assert!( 294 assert!(
@@ -306,7 +302,7 @@ mod tests {
306 302
307 fn check_expect(ra_fixture: &str, expect: Expect) { 303 fn check_expect(ra_fixture: &str, expect: Expect) {
308 let (analysis, file_id) = single_file(ra_fixture); 304 let (analysis, file_id) = single_file(ra_fixture);
309 let diagnostics = analysis.diagnostics(file_id, true).unwrap(); 305 let diagnostics = analysis.diagnostics(file_id, true, None).unwrap();
310 expect.assert_debug_eq(&diagnostics) 306 expect.assert_debug_eq(&diagnostics)
311 } 307 }
312 308
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index b762c994e..a19a379c6 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -151,16 +151,11 @@ impl<T> RangeInfo<T> {
151#[derive(Debug)] 151#[derive(Debug)]
152pub struct AnalysisHost { 152pub struct AnalysisHost {
153 db: RootDatabase, 153 db: RootDatabase,
154 config: AnalysisConfig,
155} 154}
156 155
157impl AnalysisHost { 156impl AnalysisHost {
158 pub fn new(lru_capacity: Option<usize>) -> Self { 157 pub fn new(lru_capacity: Option<usize>) -> Self {
159 Self::with_config(lru_capacity, AnalysisConfig::default()) 158 AnalysisHost { db: RootDatabase::new(lru_capacity) }
160 }
161
162 pub fn with_config(lru_capacity: Option<usize>, config: AnalysisConfig) -> Self {
163 AnalysisHost { db: RootDatabase::new(lru_capacity), config }
164 } 159 }
165 160
166 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) { 161 pub fn update_lru_capacity(&mut self, lru_capacity: Option<usize>) {
@@ -170,7 +165,7 @@ impl AnalysisHost {
170 /// Returns a snapshot of the current state, which you can query for 165 /// Returns a snapshot of the current state, which you can query for
171 /// semantic information. 166 /// semantic information.
172 pub fn analysis(&self) -> Analysis { 167 pub fn analysis(&self) -> Analysis {
173 Analysis { db: self.db.snapshot(), config: self.config.clone() } 168 Analysis { db: self.db.snapshot() }
174 } 169 }
175 170
176 /// Applies changes to the current state of the world. If there are 171 /// Applies changes to the current state of the world. If there are
@@ -214,7 +209,6 @@ impl Default for AnalysisHost {
214#[derive(Debug)] 209#[derive(Debug)]
215pub struct Analysis { 210pub struct Analysis {
216 db: salsa::Snapshot<RootDatabase>, 211 db: salsa::Snapshot<RootDatabase>,
217 config: AnalysisConfig,
218} 212}
219 213
220// As a general design guideline, `Analysis` API are intended to be independent 214// As a general design guideline, `Analysis` API are intended to be independent
@@ -509,8 +503,11 @@ impl Analysis {
509 &self, 503 &self,
510 file_id: FileId, 504 file_id: FileId,
511 enable_experimental: bool, 505 enable_experimental: bool,
506 disabled_diagnostics: Option<HashSet<String>>,
512 ) -> Cancelable<Vec<Diagnostic>> { 507 ) -> Cancelable<Vec<Diagnostic>> {
513 self.with_db(|db| diagnostics::diagnostics(db, file_id, enable_experimental, &self.config)) 508 self.with_db(|db| {
509 diagnostics::diagnostics(db, file_id, enable_experimental, disabled_diagnostics)
510 })
514 } 511 }
515 512
516 /// Returns the edit required to rename reference at the position to the new 513 /// Returns the edit required to rename reference at the position to the new
@@ -539,11 +536,6 @@ impl Analysis {
539 }) 536 })
540 } 537 }
541 538
542 /// Sets the provided config.
543 pub fn set_config(&mut self, config: AnalysisConfig) {
544 self.config = config;
545 }
546
547 /// Performs an operation on that may be Canceled. 539 /// Performs an operation on that may be Canceled.
548 fn with_db<F, T>(&self, f: F) -> Cancelable<T> 540 fn with_db<F, T>(&self, f: F) -> Cancelable<T>
549 where 541 where