aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics.rs')
-rw-r--r--crates/ide/src/diagnostics.rs41
1 files changed, 35 insertions, 6 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 337a904b6..075aae8d5 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -4,6 +4,8 @@
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
7mod unresolved_module;
8
7mod fixes; 9mod fixes;
8mod field_shorthand; 10mod field_shorthand;
9mod unlinked_file; 11mod unlinked_file;
@@ -12,7 +14,7 @@ use std::cell::RefCell;
12 14
13use hir::{ 15use hir::{
14 db::AstDatabase, 16 db::AstDatabase,
15 diagnostics::{Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder}, 17 diagnostics::{AnyDiagnostic, Diagnostic as _, DiagnosticCode, DiagnosticSinkBuilder},
16 InFile, Semantics, 18 InFile, Semantics,
17}; 19};
18use ide_assists::AssistResolveStrategy; 20use ide_assists::AssistResolveStrategy;
@@ -42,6 +44,12 @@ pub struct Diagnostic {
42} 44}
43 45
44impl Diagnostic { 46impl Diagnostic {
47 fn new(code: &'static str, message: impl Into<String>, range: TextRange) -> Diagnostic {
48 let message = message.into();
49 let code = Some(DiagnosticCode(code));
50 Self { message, range, severity: Severity::Error, fixes: None, unused: false, code }
51 }
52
45 fn error(range: TextRange, message: String) -> Self { 53 fn error(range: TextRange, message: String) -> Self {
46 Self { message, range, severity: Severity::Error, fixes: None, unused: false, code: None } 54 Self { message, range, severity: Severity::Error, fixes: None, unused: false, code: None }
47 } 55 }
@@ -82,6 +90,13 @@ pub struct DiagnosticsConfig {
82 pub disabled: FxHashSet<String>, 90 pub disabled: FxHashSet<String>,
83} 91}
84 92
93struct DiagnosticsContext<'a> {
94 config: &'a DiagnosticsConfig,
95 sema: Semantics<'a, RootDatabase>,
96 #[allow(unused)]
97 resolve: &'a AssistResolveStrategy,
98}
99
85pub(crate) fn diagnostics( 100pub(crate) fn diagnostics(
86 db: &RootDatabase, 101 db: &RootDatabase,
87 config: &DiagnosticsConfig, 102 config: &DiagnosticsConfig,
@@ -108,9 +123,6 @@ pub(crate) fn diagnostics(
108 } 123 }
109 let res = RefCell::new(res); 124 let res = RefCell::new(res);
110 let sink_builder = DiagnosticSinkBuilder::new() 125 let sink_builder = DiagnosticSinkBuilder::new()
111 .on::<hir::diagnostics::UnresolvedModule, _>(|d| {
112 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
113 })
114 .on::<hir::diagnostics::MissingFields, _>(|d| { 126 .on::<hir::diagnostics::MissingFields, _>(|d| {
115 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve)); 127 res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve));
116 }) 128 })
@@ -204,16 +216,33 @@ pub(crate) fn diagnostics(
204 ); 216 );
205 }); 217 });
206 218
219 let mut diags = Vec::new();
207 let internal_diagnostics = cfg!(test); 220 let internal_diagnostics = cfg!(test);
208 match sema.to_module_def(file_id) { 221 match sema.to_module_def(file_id) {
209 Some(m) => m.diagnostics(db, &mut sink, internal_diagnostics), 222 Some(m) => diags = m.diagnostics(db, &mut sink, internal_diagnostics),
210 None => { 223 None => {
211 sink.push(UnlinkedFile { file_id, node: SyntaxNodePtr::new(parse.tree().syntax()) }); 224 sink.push(UnlinkedFile { file_id, node: SyntaxNodePtr::new(parse.tree().syntax()) });
212 } 225 }
213 } 226 }
214 227
215 drop(sink); 228 drop(sink);
216 res.into_inner() 229
230 let mut res = res.into_inner();
231
232 let ctx = DiagnosticsContext { config, sema, resolve };
233 for diag in diags {
234 let d = match diag {
235 AnyDiagnostic::UnresolvedModule(d) => unresolved_module::render(&ctx, &d),
236 };
237 if let Some(code) = d.code {
238 if ctx.config.disabled.contains(code.as_str()) {
239 continue;
240 }
241 }
242 res.push(d)
243 }
244
245 res
217} 246}
218 247
219fn diagnostic_with_fix<D: DiagnosticWithFixes>( 248fn diagnostic_with_fix<D: DiagnosticWithFixes>(