From 5ce84f3cbc5d5a3106ea89460da4a3f473618f32 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 25 Mar 2019 09:40:49 +0300 Subject: tweak diagnostics API --- crates/ra_hir/src/diagnostics.rs | 17 ++++++++++++----- crates/ra_hir/src/mock.rs | 6 ++---- 2 files changed, 14 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir') 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 @@ use std::{fmt, any::Any}; -use ra_syntax::{SyntaxNodePtr, AstPtr, ast}; +use ra_syntax::{SyntaxNodePtr, TreeArc, AstPtr, TextRange, ast, SyntaxNode}; use relative_path::RelativePathBuf; -use crate::HirFileId; +use crate::{HirFileId, HirDatabase}; /// Diagnostic defines hir API for errors and warnings. /// @@ -20,11 +20,18 @@ use crate::HirFileId; pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { fn message(&self) -> String; fn file(&self) -> HirFileId; - fn syntax_node(&self) -> SyntaxNodePtr; + fn syntax_node_ptr(&self) -> SyntaxNodePtr; + fn highlight_range(&self) -> TextRange { + self.syntax_node_ptr().range() + } fn as_any(&self) -> &(dyn Any + Send + 'static); } impl dyn Diagnostic { + pub fn syntax_node(&self, db: &impl HirDatabase) -> TreeArc { + let source_file = db.hir_parse(self.file()); + self.syntax_node_ptr().to_node(&source_file).to_owned() + } pub fn downcast_ref(&self) -> Option<&D> { self.as_any().downcast_ref() } @@ -77,7 +84,7 @@ impl Diagnostic for NoSuchField { fn file(&self) -> HirFileId { self.file } - fn syntax_node(&self) -> SyntaxNodePtr { + fn syntax_node_ptr(&self) -> SyntaxNodePtr { self.field.into() } fn as_any(&self) -> &(Any + Send + 'static) { @@ -99,7 +106,7 @@ impl Diagnostic for UnresolvedModule { fn file(&self) -> HirFileId { self.file } - fn syntax_node(&self) -> SyntaxNodePtr { + fn syntax_node_ptr(&self) -> SyntaxNodePtr { self.decl.into() } 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; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; use rustc_hash::FxHashMap; -use crate::{db, HirInterner, diagnostics::DiagnosticSink, DefDatabase}; +use crate::{db, HirInterner, diagnostics::DiagnosticSink}; pub const WORKSPACE: SourceRootId = SourceRootId(0); @@ -79,9 +79,7 @@ impl MockDatabase { module.diagnostics( self, &mut DiagnosticSink::new(|d| { - let source_file = self.hir_parse(d.file()); - let syntax_node = d.syntax_node().to_node(&source_file); - buf += &format!("{:?}: {}\n", syntax_node.text(), d.message()); + buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message()); }), ) } -- cgit v1.2.3