diff options
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r-- | crates/ide/src/diagnostics.rs | 36 |
1 files changed, 16 insertions, 20 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 | ||
7 | use std::cell::RefCell; | 7 | use std::{cell::RefCell, collections::HashSet}; |
8 | 8 | ||
9 | use base_db::SourceDatabase; | 9 | use base_db::SourceDatabase; |
10 | use hir::{diagnostics::DiagnosticSinkBuilder, Semantics}; | 10 | use hir::{diagnostics::DiagnosticSinkBuilder, Semantics}; |
@@ -16,7 +16,7 @@ use syntax::{ | |||
16 | }; | 16 | }; |
17 | use text_edit::TextEdit; | 17 | use text_edit::TextEdit; |
18 | 18 | ||
19 | use crate::{AnalysisConfig, Diagnostic, FileId, Fix, SourceFileEdit}; | 19 | use crate::{Diagnostic, FileId, Fix, SourceFileEdit}; |
20 | 20 | ||
21 | mod diagnostics_with_fix; | 21 | mod diagnostics_with_fix; |
22 | use diagnostics_with_fix::DiagnosticWithFix; | 22 | use 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 | ||