aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/diagnostics.rs18
1 files changed, 17 insertions, 1 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}