diff options
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/diagnostics.rs | 17 | ||||
-rw-r--r-- | crates/ra_hir/src/mock.rs | 6 |
2 files changed, 14 insertions, 9 deletions
diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index a6ca68d86..d6a51b833 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs | |||
@@ -1,9 +1,9 @@ | |||
1 | use std::{fmt, any::Any}; | 1 | use std::{fmt, any::Any}; |
2 | 2 | ||
3 | use ra_syntax::{SyntaxNodePtr, AstPtr, ast}; | 3 | use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode}; |
4 | use relative_path::RelativePathBuf; | 4 | use relative_path::RelativePathBuf; |
5 | 5 | ||
6 | use crate::HirFileId; | 6 | use crate::{HirFileId, HirDatabase}; |
7 | 7 | ||
8 | /// Diagnostic defines hir API for errors and warnings. | 8 | /// Diagnostic defines hir API for errors and warnings. |
9 | /// | 9 | /// |
@@ -20,11 +20,18 @@ use crate::HirFileId; | |||
20 | pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { | 20 | pub 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 file(&self) -> HirFileId; |
23 | fn syntax_node(&self) -> SyntaxNodePtr; | 23 | fn syntax_node_ptr(&self) -> SyntaxNodePtr; |
24 | fn highlight_range(&self) -> TextRange { | ||
25 | self.syntax_node_ptr().range() | ||
26 | } | ||
24 | fn as_any(&self) -> &(dyn Any + Send + 'static); | 27 | fn as_any(&self) -> &(dyn Any + Send + 'static); |
25 | } | 28 | } |
26 | 29 | ||
27 | impl dyn Diagnostic { | 30 | impl dyn Diagnostic { |
31 | pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc<SyntaxNode> { | ||
32 | let source_file = db.hir_parse(self.file()); | ||
33 | self.syntax_node_ptr().to_node(&source_file).to_owned() | ||
34 | } | ||
28 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { | 35 | pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { |
29 | self.as_any().downcast_ref() | 36 | self.as_any().downcast_ref() |
30 | } | 37 | } |
@@ -77,7 +84,7 @@ impl Diagnostic for NoSuchField { | |||
77 | fn file(&self) -> HirFileId { | 84 | fn file(&self) -> HirFileId { |
78 | self.file | 85 | self.file |
79 | } | 86 | } |
80 | fn syntax_node(&self) -> SyntaxNodePtr { | 87 | fn syntax_node_ptr(&self) -> SyntaxNodePtr { |
81 | self.field.into() | 88 | self.field.into() |
82 | } | 89 | } |
83 | fn as_any(&self) -> &(Any + Send + 'static) { | 90 | fn as_any(&self) -> &(Any + Send + 'static) { |
@@ -99,7 +106,7 @@ impl Diagnostic for UnresolvedModule { | |||
99 | fn file(&self) -> HirFileId { | 106 | fn file(&self) -> HirFileId { |
100 | self.file | 107 | self.file |
101 | } | 108 | } |
102 | fn syntax_node(&self) -> SyntaxNodePtr { | 109 | fn syntax_node_ptr(&self) -> SyntaxNodePtr { |
103 | self.decl.into() | 110 | self.decl.into() |
104 | } | 111 | } |
105 | fn as_any(&self) -> &(Any + Send + 'static) { | 112 | fn as_any(&self) -> &(Any + Send + 'static) { |
diff --git a/crates/ra_hir/src/mock.rs b/crates/ra_hir/src/mock.rs index 08f927bb4..aeab6b180 100644 --- a/crates/ra_hir/src/mock.rs +++ b/crates/ra_hir/src/mock.rs | |||
@@ -9,7 +9,7 @@ use relative_path::RelativePathBuf; | |||
9 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; | 9 | use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; |
10 | use rustc_hash::FxHashMap; | 10 | use rustc_hash::FxHashMap; |
11 | 11 | ||
12 | use crate::{db, HirInterner, diagnostics::DiagnosticSink, DefDatabase}; | 12 | use crate::{db, HirInterner, diagnostics::DiagnosticSink}; |
13 | 13 | ||
14 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | 14 | pub const WORKSPACE: SourceRootId = SourceRootId(0); |
15 | 15 | ||
@@ -79,9 +79,7 @@ impl MockDatabase { | |||
79 | module.diagnostics( | 79 | module.diagnostics( |
80 | self, | 80 | self, |
81 | &mut DiagnosticSink::new(|d| { | 81 | &mut DiagnosticSink::new(|d| { |
82 | let source_file = self.hir_parse(d.file()); | 82 | buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); |
83 | let syntax_node = d.syntax_node().to_node(&source_file); | ||
84 | buf += &format!("{:?}: {}\n", syntax_node.text(), d.message()); | ||
85 | }), | 83 | }), |
86 | ) | 84 | ) |
87 | } | 85 | } |