aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-07-05 17:40:02 +0100
committerAleksey Kladov <[email protected]>2019-07-05 17:40:02 +0100
commit0b5e39919034d11c36655369f60cf9cc256743d0 (patch)
treec175d87985271961227a1716ce54fd1d2229e254 /crates
parentfd30ec406ba776e670dbd46b964de626f28e2857 (diff)
use correct file for diagnostics
closes #1475
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/diagnostics.rs18
-rw-r--r--crates/ra_ide_api/src/diagnostics.rs13
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 @@
1use std::{any::Any, fmt}; 1use std::{any::Any, fmt};
2 2
3use ra_syntax::{ast, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc}; 3use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange, TreeArc};
4use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
5 5
6use crate::{HirDatabase, HirFileId, Name}; 6use 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
30pub trait AstDiagnostic {
31 type AST;
32 fn ast(&self, db: &impl HirDatabase) -> Self::AST;
33}
34
30impl dyn Diagnostic { 35impl 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
145impl 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 @@
1use std::cell::RefCell; 1use std::cell::RefCell;
2 2
3use hir::{ 3use hir::{
4 diagnostics::{Diagnostic as _, DiagnosticSink}, 4 diagnostics::{AstDiagnostic, Diagnostic as _, DiagnosticSink},
5 source_binder, 5 source_binder,
6}; 6};
7use itertools::Itertools; 7use itertools::Itertools;
@@ -9,7 +9,7 @@ use ra_assists::ast_editor::{AstBuilder, AstEditor};
9use ra_db::SourceDatabase; 9use ra_db::SourceDatabase;
10use ra_prof::profile; 10use ra_prof::profile;
11use ra_syntax::{ 11use 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};
15use ra_text_edit::{TextEdit, TextEditBuilder}; 15use 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 }