tweak diagnostics API

This commit is contained in:
Aleksey Kladov 2019-03-25 09:40:49 +03:00
parent 4c4a714328
commit 5ce84f3cbc
3 changed files with 16 additions and 11 deletions

View file

@ -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<SyntaxNode> {
let source_file = db.hir_parse(self.file());
self.syntax_node_ptr().to_node(&source_file).to_owned()
}
pub fn downcast_ref<D: Diagnostic>(&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) {

View file

@ -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());
}),
)
}

View file

@ -31,7 +31,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
let mut sink = DiagnosticSink::new(|d| {
res.borrow_mut().push(Diagnostic {
message: d.message(),
range: d.syntax_node().range(),
range: d.highlight_range(),
severity: Severity::Error,
fix: None,
})
@ -46,7 +46,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
cursor_position: None,
};
res.borrow_mut().push(Diagnostic {
range: d.syntax_node().range(),
range: d.highlight_range(),
message: d.message(),
severity: Severity::Error,
fix: Some(fix),