diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/fix_visibility.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/diagnostics.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/diagnostics.rs | 38 | ||||
-rw-r--r-- | crates/ra_ide/src/diagnostics.rs | 8 |
4 files changed, 28 insertions, 31 deletions
diff --git a/crates/ra_assists/src/handlers/fix_visibility.rs b/crates/ra_assists/src/handlers/fix_visibility.rs index 1aefa79cc..a19dbf33f 100644 --- a/crates/ra_assists/src/handlers/fix_visibility.rs +++ b/crates/ra_assists/src/handlers/fix_visibility.rs | |||
@@ -121,7 +121,7 @@ fn add_vis_to_referenced_record_field(acc: &mut Assists, ctx: &AssistContext) -> | |||
121 | Some(cap) => match current_visibility { | 121 | Some(cap) => match current_visibility { |
122 | Some(current_visibility) => builder.replace_snippet( | 122 | Some(current_visibility) => builder.replace_snippet( |
123 | cap, | 123 | cap, |
124 | dbg!(current_visibility.syntax()).text_range(), | 124 | current_visibility.syntax().text_range(), |
125 | format!("$0{}", missing_visibility), | 125 | format!("$0{}", missing_visibility), |
126 | ), | 126 | ), |
127 | None => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), | 127 | None => builder.insert_snippet(cap, offset, format!("$0{} ", missing_visibility)), |
diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index e889f070f..ffeca5e82 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs | |||
@@ -16,13 +16,16 @@ | |||
16 | 16 | ||
17 | use std::{any::Any, fmt}; | 17 | use std::{any::Any, fmt}; |
18 | 18 | ||
19 | use ra_syntax::{SyntaxNode, SyntaxNodePtr}; | 19 | use ra_syntax::SyntaxNodePtr; |
20 | 20 | ||
21 | use crate::{db::AstDatabase, InFile}; | 21 | use crate::{db::AstDatabase, InFile}; |
22 | 22 | ||
23 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | 23 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { |
24 | fn message(&self) -> String; | 24 | fn message(&self) -> String; |
25 | fn source(&self) -> InFile<SyntaxNodePtr>; | 25 | fn source(&self) -> InFile<SyntaxNodePtr>; |
26 | fn highlighting_source(&self) -> InFile<SyntaxNodePtr> { | ||
27 | self.source() | ||
28 | } | ||
26 | fn as_any(&self) -> &(dyn Any + Send + 'static); | 29 | fn as_any(&self) -> &(dyn Any + Send + 'static); |
27 | fn is_experimental(&self) -> bool { | 30 | fn is_experimental(&self) -> bool { |
28 | false | 31 | false |
@@ -35,12 +38,6 @@ pub trait AstDiagnostic { | |||
35 | } | 38 | } |
36 | 39 | ||
37 | impl dyn Diagnostic { | 40 | impl dyn Diagnostic { |
38 | pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { | ||
39 | let source = self.source(); | ||
40 | let node = db.parse_or_expand(source.file_id).unwrap(); | ||
41 | source.value.to_node(&node) | ||
42 | } | ||
43 | |||
44 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { | 41 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { |
45 | self.as_any().downcast_ref() | 42 | self.as_any().downcast_ref() |
46 | } | 43 | } |
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index a5b00ed48..73d241434 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs | |||
@@ -9,7 +9,7 @@ use hir_def::DefWithBodyId; | |||
9 | use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; | 9 | use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; |
10 | use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; | 10 | use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; |
11 | use ra_prof::profile; | 11 | use ra_prof::profile; |
12 | use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr}; | 12 | use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; |
13 | use stdx::format_to; | 13 | use stdx::format_to; |
14 | 14 | ||
15 | use crate::db::HirDatabase; | 15 | use crate::db::HirDatabase; |
@@ -64,16 +64,6 @@ pub struct MissingFields { | |||
64 | pub list_parent_path: Option<AstPtr<ast::Path>>, | 64 | pub list_parent_path: Option<AstPtr<ast::Path>>, |
65 | } | 65 | } |
66 | 66 | ||
67 | impl MissingFields { | ||
68 | fn root(&self, db: &dyn AstDatabase) -> SyntaxNode { | ||
69 | db.parse_or_expand(self.file).unwrap() | ||
70 | } | ||
71 | |||
72 | pub fn list_parent_ast(&self, db: &dyn AstDatabase) -> Option<ast::Path> { | ||
73 | self.list_parent_path.as_ref().map(|path| path.to_node(&self.root(db))) | ||
74 | } | ||
75 | } | ||
76 | |||
77 | impl Diagnostic for MissingFields { | 67 | impl Diagnostic for MissingFields { |
78 | fn message(&self) -> String { | 68 | fn message(&self) -> String { |
79 | let mut buf = String::from("Missing structure fields:\n"); | 69 | let mut buf = String::from("Missing structure fields:\n"); |
@@ -85,16 +75,25 @@ impl Diagnostic for MissingFields { | |||
85 | fn source(&self) -> InFile<SyntaxNodePtr> { | 75 | fn source(&self) -> InFile<SyntaxNodePtr> { |
86 | InFile { file_id: self.file, value: self.field_list.clone().into() } | 76 | InFile { file_id: self.file, value: self.field_list.clone().into() } |
87 | } | 77 | } |
78 | |||
88 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | 79 | fn as_any(&self) -> &(dyn Any + Send + 'static) { |
89 | self | 80 | self |
90 | } | 81 | } |
82 | |||
83 | fn highlighting_source(&self) -> InFile<SyntaxNodePtr> { | ||
84 | self.list_parent_path | ||
85 | .clone() | ||
86 | .map(|path| InFile { file_id: self.file, value: path.into() }) | ||
87 | .unwrap_or_else(|| self.source()) | ||
88 | } | ||
91 | } | 89 | } |
92 | 90 | ||
93 | impl AstDiagnostic for MissingFields { | 91 | impl AstDiagnostic for MissingFields { |
94 | type AST = ast::RecordExprFieldList; | 92 | type AST = ast::RecordExprFieldList; |
95 | 93 | ||
96 | fn ast(&self, db: &dyn AstDatabase) -> Self::AST { | 94 | fn ast(&self, db: &dyn AstDatabase) -> Self::AST { |
97 | self.field_list.to_node(&self.root(db)) | 95 | let root = db.parse_or_expand(self.file).unwrap(); |
96 | self.field_list.to_node(&root) | ||
98 | } | 97 | } |
99 | } | 98 | } |
100 | 99 | ||
@@ -260,7 +259,10 @@ impl AstDiagnostic for MismatchedArgCount { | |||
260 | #[cfg(test)] | 259 | #[cfg(test)] |
261 | mod tests { | 260 | mod tests { |
262 | use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId}; | 261 | use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId}; |
263 | use hir_expand::diagnostics::{Diagnostic, DiagnosticSinkBuilder}; | 262 | use hir_expand::{ |
263 | db::AstDatabase, | ||
264 | diagnostics::{Diagnostic, DiagnosticSinkBuilder}, | ||
265 | }; | ||
264 | use ra_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; | 266 | use ra_db::{fixture::WithFixture, FileId, SourceDatabase, SourceDatabaseExt}; |
265 | use ra_syntax::{TextRange, TextSize}; | 267 | use ra_syntax::{TextRange, TextSize}; |
266 | use rustc_hash::FxHashMap; | 268 | use rustc_hash::FxHashMap; |
@@ -307,7 +309,9 @@ mod tests { | |||
307 | db.diagnostics(|d| { | 309 | db.diagnostics(|d| { |
308 | // FXIME: macros... | 310 | // FXIME: macros... |
309 | let file_id = d.source().file_id.original_file(&db); | 311 | let file_id = d.source().file_id.original_file(&db); |
310 | let range = d.syntax_node(&db).text_range(); | 312 | let highlighting_source = d.highlighting_source(); |
313 | let node = db.parse_or_expand(highlighting_source.file_id).unwrap(); | ||
314 | let range = highlighting_source.value.to_node(&node).text_range(); | ||
311 | let message = d.message().to_owned(); | 315 | let message = d.message().to_owned(); |
312 | actual.entry(file_id).or_default().push((range, message)); | 316 | actual.entry(file_id).or_default().push((range, message)); |
313 | }); | 317 | }); |
@@ -345,7 +349,7 @@ struct Beefy { | |||
345 | } | 349 | } |
346 | fn baz() { | 350 | fn baz() { |
347 | let zz = Beefy { | 351 | let zz = Beefy { |
348 | //^^^^^... Missing structure fields: | 352 | //^^^^^ Missing structure fields: |
349 | // | - seven | 353 | // | - seven |
350 | one: (), | 354 | one: (), |
351 | two: (), | 355 | two: (), |
@@ -370,8 +374,8 @@ struct S { foo: i32, bar: () } | |||
370 | impl S { | 374 | impl S { |
371 | fn new() -> S { | 375 | fn new() -> S { |
372 | S { | 376 | S { |
373 | //^... Missing structure fields: | 377 | //^ Missing structure fields: |
374 | //| - bar | 378 | //| - bar |
375 | foo: 92, | 379 | foo: 92, |
376 | baz: 62, | 380 | baz: 62, |
377 | //^^^^^^^ no such field | 381 | //^^^^^^^ no such field |
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 7ae4bda0b..e847df6ea 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs | |||
@@ -69,7 +69,6 @@ pub(crate) fn diagnostics( | |||
69 | }) | 69 | }) |
70 | }) | 70 | }) |
71 | .on::<hir::diagnostics::MissingFields, _>(|d| { | 71 | .on::<hir::diagnostics::MissingFields, _>(|d| { |
72 | let range = sema.diagnostics_range(d).range; | ||
73 | // Note that although we could add a diagnostics to | 72 | // Note that although we could add a diagnostics to |
74 | // fill the missing tuple field, e.g : | 73 | // fill the missing tuple field, e.g : |
75 | // `struct A(usize);` | 74 | // `struct A(usize);` |
@@ -95,15 +94,12 @@ pub(crate) fn diagnostics( | |||
95 | }; | 94 | }; |
96 | Some(( | 95 | Some(( |
97 | Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()), | 96 | Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()), |
98 | range, | 97 | sema.diagnostics_range(d).range, |
99 | )) | 98 | )) |
100 | }; | 99 | }; |
101 | 100 | ||
102 | res.borrow_mut().push(Diagnostic { | 101 | res.borrow_mut().push(Diagnostic { |
103 | range: d | 102 | range: d.highlighting_source().file_syntax(db).text_range(), |
104 | .list_parent_ast(db) | ||
105 | .map(|path| path.syntax().text_range()) | ||
106 | .unwrap_or(range), | ||
107 | message: d.message(), | 103 | message: d.message(), |
108 | severity: Severity::Error, | 104 | severity: Severity::Error, |
109 | fix, | 105 | fix, |