tweak diagnostics API
This commit is contained in:
parent
4c4a714328
commit
5ce84f3cbc
3 changed files with 16 additions and 11 deletions
|
@ -1,9 +1,9 @@
|
||||||
use std::{fmt, any::Any};
|
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 relative_path::RelativePathBuf;
|
||||||
|
|
||||||
use crate::HirFileId;
|
use crate::{HirFileId, HirDatabase};
|
||||||
|
|
||||||
/// Diagnostic defines hir API for errors and warnings.
|
/// Diagnostic defines hir API for errors and warnings.
|
||||||
///
|
///
|
||||||
|
@ -20,11 +20,18 @@ use crate::HirFileId;
|
||||||
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
|
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
|
||||||
fn message(&self) -> String;
|
fn message(&self) -> String;
|
||||||
fn file(&self) -> HirFileId;
|
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);
|
fn as_any(&self) -> &(dyn Any + Send + 'static);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl dyn Diagnostic {
|
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> {
|
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
|
||||||
self.as_any().downcast_ref()
|
self.as_any().downcast_ref()
|
||||||
}
|
}
|
||||||
|
@ -77,7 +84,7 @@ impl Diagnostic for NoSuchField {
|
||||||
fn file(&self) -> HirFileId {
|
fn file(&self) -> HirFileId {
|
||||||
self.file
|
self.file
|
||||||
}
|
}
|
||||||
fn syntax_node(&self) -> SyntaxNodePtr {
|
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
|
||||||
self.field.into()
|
self.field.into()
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(Any + Send + 'static) {
|
fn as_any(&self) -> &(Any + Send + 'static) {
|
||||||
|
@ -99,7 +106,7 @@ impl Diagnostic for UnresolvedModule {
|
||||||
fn file(&self) -> HirFileId {
|
fn file(&self) -> HirFileId {
|
||||||
self.file
|
self.file
|
||||||
}
|
}
|
||||||
fn syntax_node(&self) -> SyntaxNodePtr {
|
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
|
||||||
self.decl.into()
|
self.decl.into()
|
||||||
}
|
}
|
||||||
fn as_any(&self) -> &(Any + Send + 'static) {
|
fn as_any(&self) -> &(Any + Send + 'static) {
|
||||||
|
|
|
@ -9,7 +9,7 @@ use relative_path::RelativePathBuf;
|
||||||
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
|
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::{db, HirInterner, diagnostics::DiagnosticSink, DefDatabase};
|
use crate::{db, HirInterner, diagnostics::DiagnosticSink};
|
||||||
|
|
||||||
pub const WORKSPACE: SourceRootId = SourceRootId(0);
|
pub const WORKSPACE: SourceRootId = SourceRootId(0);
|
||||||
|
|
||||||
|
@ -79,9 +79,7 @@ impl MockDatabase {
|
||||||
module.diagnostics(
|
module.diagnostics(
|
||||||
self,
|
self,
|
||||||
&mut DiagnosticSink::new(|d| {
|
&mut DiagnosticSink::new(|d| {
|
||||||
let source_file = self.hir_parse(d.file());
|
buf += &format!("{:?}: {}\n", d.syntax_node(self).text(), d.message());
|
||||||
let syntax_node = d.syntax_node().to_node(&source_file);
|
|
||||||
buf += &format!("{:?}: {}\n", syntax_node.text(), d.message());
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
||||||
let mut sink = DiagnosticSink::new(|d| {
|
let mut sink = DiagnosticSink::new(|d| {
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
range: d.syntax_node().range(),
|
range: d.highlight_range(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: None,
|
fix: None,
|
||||||
})
|
})
|
||||||
|
@ -46,7 +46,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
|
||||||
cursor_position: None,
|
cursor_position: None,
|
||||||
};
|
};
|
||||||
res.borrow_mut().push(Diagnostic {
|
res.borrow_mut().push(Diagnostic {
|
||||||
range: d.syntax_node().range(),
|
range: d.highlight_range(),
|
||||||
message: d.message(),
|
message: d.message(),
|
||||||
severity: Severity::Error,
|
severity: Severity::Error,
|
||||||
fix: Some(fix),
|
fix: Some(fix),
|
||||||
|
|
Loading…
Add table
Reference in a new issue