From 475a93097f60ff1aa4fd725f86d89a831fcc9288 Mon Sep 17 00:00:00 2001 From: Evgenii P Date: Mon, 12 Aug 2019 23:06:08 +0700 Subject: Use Source in Diagnostic and implement --- crates/ra_hir/src/diagnostics.rs | 38 ++++++++++++++---------------------- crates/ra_ide_api/src/diagnostics.rs | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/crates/ra_hir/src/diagnostics.rs b/crates/ra_hir/src/diagnostics.rs index 0290483b3..f6240830f 100644 --- a/crates/ra_hir/src/diagnostics.rs +++ b/crates/ra_hir/src/diagnostics.rs @@ -3,7 +3,7 @@ use std::{any::Any, fmt}; use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, TextRange}; use relative_path::RelativePathBuf; -use crate::{HirDatabase, HirFileId, Name}; +use crate::{HirDatabase, HirFileId, Name, Source}; /// Diagnostic defines hir API for errors and warnings. /// @@ -19,10 +19,9 @@ use crate::{HirDatabase, HirFileId, Name}; /// instance of `Diagnostic` on demand. pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { fn message(&self) -> String; - fn file(&self) -> HirFileId; - fn syntax_node_ptr(&self) -> SyntaxNodePtr; + fn source(&self) -> Source; fn highlight_range(&self) -> TextRange { - self.syntax_node_ptr().range() + self.source().ast.range() } fn as_any(&self) -> &(dyn Any + Send + 'static); } @@ -34,8 +33,8 @@ pub trait AstDiagnostic { impl dyn Diagnostic { pub fn syntax_node(&self, db: &impl HirDatabase) -> SyntaxNode { - let node = db.parse_or_expand(self.file()).unwrap(); - self.syntax_node_ptr().to_node(&node) + let node = db.parse_or_expand(self.source().file_id).unwrap(); + self.source().ast.to_node(&node) } pub fn downcast_ref(&self) -> Option<&D> { @@ -87,12 +86,11 @@ impl Diagnostic for NoSuchField { fn message(&self) -> String { "no such field".to_string() } - fn file(&self) -> HirFileId { - self.file - } - fn syntax_node_ptr(&self) -> SyntaxNodePtr { - self.field.into() + + fn source(&self) -> Source { + Source { file_id: self.file, ast: self.field.into() } } + fn as_any(&self) -> &(dyn Any + Send + 'static) { self } @@ -109,11 +107,8 @@ impl Diagnostic for UnresolvedModule { fn message(&self) -> String { "unresolved module".to_string() } - fn file(&self) -> HirFileId { - self.file - } - fn syntax_node_ptr(&self) -> SyntaxNodePtr { - self.decl.into() + fn source(&self) -> Source { + Source { file_id: self.file, ast: self.decl.into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { self @@ -131,11 +126,8 @@ impl Diagnostic for MissingFields { fn message(&self) -> String { "fill structure fields".to_string() } - fn file(&self) -> HirFileId { - self.file - } - fn syntax_node_ptr(&self) -> SyntaxNodePtr { - self.field_list.into() + fn source(&self) -> Source { + Source { file_id: self.file, ast: self.field_list.into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { self @@ -146,8 +138,8 @@ impl AstDiagnostic for MissingFields { type AST = ast::NamedFieldList; fn ast(&self, db: &impl HirDatabase) -> Self::AST { - let root = db.parse_or_expand(self.file()).unwrap(); - let node = self.syntax_node_ptr().to_node(&root); + let root = db.parse_or_expand(self.source().file_id).unwrap(); + let node = self.source().ast.to_node(&root); ast::NamedFieldList::cast(node).unwrap() } } diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index 028dc3d4f..98b840b26 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -48,7 +48,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec }) }) .on::(|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 fix = SourceChange::file_system_edit("create module", create_file); res.borrow_mut().push(Diagnostic { -- cgit v1.2.3