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.rs38
1 files changed, 15 insertions, 23 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs
index 0290483b3..f6240830f 100644
--- a/crates/ra_hir/src/diagnostics.rs
+++ b/crates/ra_hir/src/diagnostics.rs
@@ -3,7 +3,7 @@ use std::{any::Any, fmt};
3use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange}; 3use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange};
4use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
5 5
6use crate::{HirDatabase, HirFileId, Name}; 6use crate::{HirDatabase, HirFileId, Name, Source};
7 7
8/// Diagnostic defines hir API for errors and warnings. 8/// Diagnostic defines hir API for errors and warnings.
9/// 9///
@@ -19,10 +19,9 @@ use crate::{HirDatabase, HirFileId, Name};
19/// instance of `Diagnostic` on demand. 19/// instance of `Diagnostic` on demand.
20pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { 20pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
21 fn message(&self) -> String; 21 fn message(&self) -> String;
22 fn file(&self) -> HirFileId; 22 fn source(&self) -> Source<SyntaxNodePtr>;
23 fn syntax_node_ptr(&self) -> SyntaxNodePtr;
24 fn highlight_range(&self) -> TextRange { 23 fn highlight_range(&self) -> TextRange {
25 self.syntax_node_ptr().range() 24 self.source().ast.range()
26 } 25 }
27 fn as_any(&self) -> &(dyn Any + Send + 'static); 26 fn as_any(&self) -> &(dyn Any + Send + 'static);
28} 27}
@@ -34,8 +33,8 @@ pub trait AstDiagnostic {
34 33
35impl dyn Diagnostic { 34impl dyn Diagnostic {
36 pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode { 35 pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode {
37 let node = db.parse_or_expand(self.file()).unwrap(); 36 let node = db.parse_or_expand(self.source().file_id).unwrap();
38 self.syntax_node_ptr().to_node(&node) 37 self.source().ast.to_node(&node)
39 } 38 }
40 39
41 pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { 40 pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
@@ -87,12 +86,11 @@ impl Diagnostic for NoSuchField {
87 fn message(&self) -> String { 86 fn message(&self) -> String {
88 "no such field".to_string() 87 "no such field".to_string()
89 } 88 }
90 fn file(&self) -> HirFileId { 89
91 self.file 90 fn source(&self) -> Source<SyntaxNodePtr> {
92 } 91 Source { file_id: self.file, ast: self.field.into() }
93 fn syntax_node_ptr(&self) -> SyntaxNodePtr {
94 self.field.into()
95 } 92 }
93
96 fn as_any(&self) -> &(dyn Any + Send + 'static) { 94 fn as_any(&self) -> &(dyn Any + Send + 'static) {
97 self 95 self
98 } 96 }
@@ -109,11 +107,8 @@ impl Diagnostic for UnresolvedModule {
109 fn message(&self) -> String { 107 fn message(&self) -> String {
110 "unresolved module".to_string() 108 "unresolved module".to_string()
111 } 109 }
112 fn file(&self) -> HirFileId { 110 fn source(&self) -> Source<SyntaxNodePtr> {
113 self.file 111 Source { file_id: self.file, ast: self.decl.into() }
114 }
115 fn syntax_node_ptr(&self) -> SyntaxNodePtr {
116 self.decl.into()
117 } 112 }
118 fn as_any(&self) -> &(dyn Any + Send + 'static) { 113 fn as_any(&self) -> &(dyn Any + Send + 'static) {
119 self 114 self
@@ -131,11 +126,8 @@ impl Diagnostic for MissingFields {
131 fn message(&self) -> String { 126 fn message(&self) -> String {
132 "fill structure fields".to_string() 127 "fill structure fields".to_string()
133 } 128 }
134 fn file(&self) -> HirFileId { 129 fn source(&self) -> Source<SyntaxNodePtr> {
135 self.file 130 Source { file_id: self.file, ast: self.field_list.into() }
136 }
137 fn syntax_node_ptr(&self) -> SyntaxNodePtr {
138 self.field_list.into()
139 } 131 }
140 fn as_any(&self) -> &(dyn Any + Send + 'static) { 132 fn as_any(&self) -> &(dyn Any + Send + 'static) {
141 self 133 self
@@ -146,8 +138,8 @@ impl AstDiagnostic for MissingFields {
146 type AST = ast::NamedFieldList; 138 type AST = ast::NamedFieldList;
147 139
148 fn ast(&self, db: &impl HirDatabase) -> Self::AST { 140 fn ast(&self, db: &impl HirDatabase) -> Self::AST {
149 let root = db.parse_or_expand(self.file()).unwrap(); 141 let root = db.parse_or_expand(self.source().file_id).unwrap();
150 let node = self.syntax_node_ptr().to_node(&root); 142 let node = self.source().ast.to_node(&root);
151 ast::NamedFieldList::cast(node).unwrap() 143 ast::NamedFieldList::cast(node).unwrap()
152 } 144 }
153} 145}