Use Source in Diagnostic and implement

This commit is contained in:
Evgenii P 2019-08-12 23:06:08 +07:00
parent d5e8fa606d
commit 475a93097f
2 changed files with 16 additions and 24 deletions

View file

@ -3,7 +3,7 @@ use std::{any::Any, fmt};
use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange}; use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange};
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::{HirDatabase, HirFileId, Name}; use crate::{HirDatabase, HirFileId, Name, Source};
/// Diagnostic defines hir API for errors and warnings. /// Diagnostic defines hir API for errors and warnings.
/// ///
@ -19,10 +19,9 @@ use crate::{HirDatabase, HirFileId, Name};
/// instance of `Diagnostic` on demand. /// instance of `Diagnostic` on demand.
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 source(&self) -> Source<SyntaxNodePtr>;
fn syntax_node_ptr(&self) -> SyntaxNodePtr;
fn highlight_range(&self) -> TextRange { fn highlight_range(&self) -> TextRange {
self.syntax_node_ptr().range() self.source().ast.range()
} }
fn as_any(&self) -> &(dyn Any + Send + 'static); fn as_any(&self) -> &(dyn Any + Send + 'static);
} }
@ -34,8 +33,8 @@ pub trait AstDiagnostic {
impl dyn Diagnostic { impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode { pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode {
let node = db.parse_or_expand(self.file()).unwrap(); let node = db.parse_or_expand(self.source().file_id).unwrap();
self.syntax_node_ptr().to_node(&node) self.source().ast.to_node(&node)
} }
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
@ -87,12 +86,11 @@ impl Diagnostic for NoSuchField {
fn message(&self) -> String { fn message(&self) -> String {
"no such field".to_string() "no such field".to_string()
} }
fn file(&self) -> HirFileId {
self.file fn source(&self) -> Source<SyntaxNodePtr> {
} Source { file_id: self.file, ast: self.field.into() }
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
self.field.into()
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
self self
} }
@ -109,11 +107,8 @@ impl Diagnostic for UnresolvedModule {
fn message(&self) -> String { fn message(&self) -> String {
"unresolved module".to_string() "unresolved module".to_string()
} }
fn file(&self) -> HirFileId { fn source(&self) -> Source<SyntaxNodePtr> {
self.file Source { file_id: self.file, ast: self.decl.into() }
}
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
self.decl.into()
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
self self
@ -131,11 +126,8 @@ impl Diagnostic for MissingFields {
fn message(&self) -> String { fn message(&self) -> String {
"fill structure fields".to_string() "fill structure fields".to_string()
} }
fn file(&self) -> HirFileId { fn source(&self) -> Source<SyntaxNodePtr> {
self.file Source { file_id: self.file, ast: self.field_list.into() }
}
fn syntax_node_ptr(&self) -> SyntaxNodePtr {
self.field_list.into()
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
self self
@ -146,8 +138,8 @@ impl AstDiagnostic for MissingFields {
type AST = ast::NamedFieldList; type AST = ast::NamedFieldList;
fn ast(&self, db: &impl HirDatabase) -> Self::AST { fn ast(&self, db: &impl HirDatabase) -> Self::AST {
let root = db.parse_or_expand(self.file()).unwrap(); let root = db.parse_or_expand(self.source().file_id).unwrap();
let node = self.syntax_node_ptr().to_node(&root); let node = self.source().ast.to_node(&root);
ast::NamedFieldList::cast(node).unwrap() ast::NamedFieldList::cast(node).unwrap()
} }
} }

View file

@ -48,7 +48,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
}) })
}) })
.on::<hir::diagnostics::UnresolvedModule, _>(|d| { .on::<hir::diagnostics::UnresolvedModule, _>(|d| {
let source_root = db.file_source_root(d.file().original_file(db)); let source_root = db.file_source_root(d.source().file_id.original_file(db));
let create_file = FileSystemEdit::CreateFile { source_root, path: d.candidate.clone() }; let create_file = FileSystemEdit::CreateFile { source_root, path: d.candidate.clone() };
let fix = SourceChange::file_system_edit("create module", create_file); let fix = SourceChange::file_system_edit("create module", create_file);
res.borrow_mut().push(Diagnostic { res.borrow_mut().push(Diagnostic {