diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-05 17:43:23 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-07-05 17:43:23 +0100 |
commit | a9dcd2cdca014d30e35638db4edeacbbf6eae6b3 (patch) | |
tree | 94bf1d637fe613360619b862f0d6126d478ec433 /crates | |
parent | 3775e5e27d39301770d396f320aac97b48cd47c0 (diff) | |
parent | 0b5e39919034d11c36655369f60cf9cc256743d0 (diff) |
Merge #1495
1495: use correct file for diagnostics r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/diagnostics.rs | 18 | ||||
-rw-r--r-- | crates/ra_ide_api/src/diagnostics.rs | 13 |
2 files changed, 22 insertions, 9 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index f5f2e65f3..c97f0656d 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::{any::Any, fmt}; | 1 | use std::{any::Any, fmt}; |
2 | 2 | ||
3 | use ra_syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc}; | 3 | use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc}; |
4 | use relative_path::RelativePathBuf; | 4 | use relative_path::RelativePathBuf; |
5 | 5 | ||
6 | use crate::{HirDatabase, HirFileId, Name}; | 6 | use crate::{HirDatabase, HirFileId, Name}; |
@@ -27,11 +27,17 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | |||
27 | fn as_any(&self) -> &(dyn Any + Send + 'static); | 27 | fn as_any(&self) -> &(dyn Any + Send + 'static); |
28 | } | 28 | } |
29 | 29 | ||
30 | pub trait AstDiagnostic { | ||
31 | type AST; | ||
32 | fn ast(&self, db: &impl HirDatabase) -> Self::AST; | ||
33 | } | ||
34 | |||
30 | impl dyn Diagnostic { | 35 | impl dyn Diagnostic { |
31 | pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { | 36 | pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { |
32 | let node = db.parse_or_expand(self.file()).unwrap(); | 37 | let node = db.parse_or_expand(self.file()).unwrap(); |
33 | self.syntax_node_ptr().to_node(&*node).to_owned() | 38 | self.syntax_node_ptr().to_node(&*node).to_owned() |
34 | } | 39 | } |
40 | |||
35 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { | 41 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { |
36 | self.as_any().downcast_ref() | 42 | self.as_any().downcast_ref() |
37 | } | 43 | } |
@@ -135,3 +141,13 @@ impl Diagnostic for MissingFields { | |||
135 | self | 141 | self |
136 | } | 142 | } |
137 | } | 143 | } |
144 | |||
145 | impl AstDiagnostic for MissingFields { | ||
146 | type AST = TreeArc<ast::NamedFieldList>; | ||
147 | |||
148 | fn ast(&self, db: &impl HirDatabase) -> Self::AST { | ||
149 | let root = db.parse_or_expand(self.file()).unwrap(); | ||
150 | let node = self.syntax_node_ptr().to_node(&*root); | ||
151 | ast::NamedFieldList::cast(&node).unwrap().to_owned() | ||
152 | } | ||
153 | } | ||
diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 9ab455b0e..a46289cba 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use std::cell::RefCell; | 1 | use std::cell::RefCell; |
2 | 2 | ||
3 | use hir::{ | 3 | use hir::{ |
4 | diagnostics::{Diagnostic as _, DiagnosticSink}, | 4 | diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink}, |
5 | source_binder, | 5 | source_binder, |
6 | }; | 6 | }; |
7 | use itertools::Itertools; | 7 | use itertools::Itertools; |
@@ -9,7 +9,7 @@ use ra_assists::ast_editor::{AstBuilder, AstEditor}; | |||
9 | use ra_db::SourceDatabase; | 9 | use ra_db::SourceDatabase; |
10 | use ra_prof::profile; | 10 | use ra_prof::profile; |
11 | use ra_syntax::{ | 11 | use ra_syntax::{ |
12 | ast::{self, AstNode, NamedField, NamedFieldList}, | 12 | ast::{self, AstNode, NamedField}, |
13 | Location, SyntaxNode, TextRange, T, | 13 | Location, SyntaxNode, TextRange, T, |
14 | }; | 14 | }; |
15 | use ra_text_edit::{TextEdit, TextEditBuilder}; | 15 | use ra_text_edit::{TextEdit, TextEditBuilder}; |
@@ -34,9 +34,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> | |||
34 | fix: None, | 34 | fix: None, |
35 | })); | 35 | })); |
36 | 36 | ||
37 | let source_file = parse.tree; | 37 | for node in parse.tree.syntax().descendants() { |
38 | |||
39 | for node in source_file.syntax().descendants() { | ||
40 | check_unnecessary_braces_in_use_statement(&mut res, file_id, node); | 38 | check_unnecessary_braces_in_use_statement(&mut res, file_id, node); |
41 | check_struct_shorthand_initialization(&mut res, file_id, node); | 39 | check_struct_shorthand_initialization(&mut res, file_id, node); |
42 | } | 40 | } |
@@ -61,9 +59,8 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> | |||
61 | }) | 59 | }) |
62 | }) | 60 | }) |
63 | .on::<hir::diagnostics::MissingFields, _>(|d| { | 61 | .on::<hir::diagnostics::MissingFields, _>(|d| { |
64 | let syntax_node = d.syntax_node_ptr(); | 62 | let node = d.ast(db); |
65 | let node = NamedFieldList::cast(syntax_node.to_node(source_file.syntax())).unwrap(); | 63 | let mut ast_editor = AstEditor::new(&*node); |
66 | let mut ast_editor = AstEditor::new(node); | ||
67 | for f in d.missed_fields.iter() { | 64 | for f in d.missed_fields.iter() { |
68 | ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f)); | 65 | ast_editor.append_field(&AstBuilder::<NamedField>::from_name(f)); |
69 | } | 66 | } |