aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-03-25 06:40:49 +0000
committerAleksey Kladov <[email protected]>2019-03-25 07:52:50 +0000
commit5ce84f3cbc5d5a3106ea89460da4a3f473618f32 (patch)
tree42b6dc5960ef5e588c9609e7c6cc4402a6c0fe22 /crates/ra_hir
parent4c4a714328490d7f2626272663827fd51dfab0bd (diff)
tweak diagnostics API
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/diagnostics.rs17
-rw-r--r--crates/ra_hir/src/mock.rs6
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 @@
1use std::{fmt, any::Any}; 1use std::{fmt, any::Any};
2 2
3use ra_syntax::{SyntaxNodePtr, AstPtr, ast}; 3use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode};
4use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
5 5
6use crate::HirFileId; 6use 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;
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 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
27impl dyn Diagnostic { 30impl 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;
9use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; 9use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
10use rustc_hash::FxHashMap; 10use rustc_hash::FxHashMap;
11 11
12use crate::{db, HirInterner, diagnostics::DiagnosticSink, DefDatabase}; 12use crate::{db, HirInterner, diagnostics::DiagnosticSink};
13 13
14pub const WORKSPACE: SourceRootId = SourceRootId(0); 14pub 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 }