Require source implementations for Diagnostic

This commit is contained in:
Kirill Bulatov 2020-07-28 10:24:59 +03:00
parent 21184a1b2a
commit cfbbd91a88
3 changed files with 15 additions and 12 deletions

View file

@ -18,7 +18,7 @@ impl Diagnostic for UnresolvedModule {
fn message(&self) -> String { fn message(&self) -> String {
"unresolved module".to_string() "unresolved module".to_string()
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.decl.clone().into()) InFile::new(self.file, self.decl.clone().into())
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {

View file

@ -22,9 +22,11 @@ use crate::{db::AstDatabase, InFile};
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 fix_source(&self) -> InFile<SyntaxNodePtr>; /// A source to be used in highlighting and other visual representations
fn source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr>;
self.fix_source() /// A source to be used during the fix application
fn fix_source(&self) -> InFile<SyntaxNodePtr> {
self.source()
} }
fn as_any(&self) -> &(dyn Any + Send + 'static); fn as_any(&self) -> &(dyn Any + Send + 'static);
fn is_experimental(&self) -> bool { fn is_experimental(&self) -> bool {
@ -39,8 +41,9 @@ pub trait AstDiagnostic {
impl dyn Diagnostic { impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode {
let node = db.parse_or_expand(self.source().file_id).unwrap(); let source = self.source();
self.source().value.to_node(&node) let node = db.parse_or_expand(source.file_id).unwrap();
source.value.to_node(&node)
} }
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> { pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {

View file

@ -37,7 +37,7 @@ impl Diagnostic for NoSuchField {
"no such field".to_string() "no such field".to_string()
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile::new(self.file, self.field.clone().into()) InFile::new(self.file, self.field.clone().into())
} }
@ -137,7 +137,7 @@ impl Diagnostic for MissingMatchArms {
fn message(&self) -> String { fn message(&self) -> String {
String::from("Missing match arm") String::from("Missing match arm")
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.match_expr.clone().into() } InFile { file_id: self.file, value: self.match_expr.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
@ -155,7 +155,7 @@ impl Diagnostic for MissingOkInTailExpr {
fn message(&self) -> String { fn message(&self) -> String {
"wrap return expression in Ok".to_string() "wrap return expression in Ok".to_string()
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.expr.clone().into() } InFile { file_id: self.file, value: self.expr.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
@ -182,7 +182,7 @@ impl Diagnostic for BreakOutsideOfLoop {
fn message(&self) -> String { fn message(&self) -> String {
"break outside of loop".to_string() "break outside of loop".to_string()
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.expr.clone().into() } InFile { file_id: self.file, value: self.expr.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
@ -209,7 +209,7 @@ impl Diagnostic for MissingUnsafe {
fn message(&self) -> String { fn message(&self) -> String {
format!("This operation is unsafe and requires an unsafe function or block") format!("This operation is unsafe and requires an unsafe function or block")
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.expr.clone().into() } InFile { file_id: self.file, value: self.expr.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {
@ -239,7 +239,7 @@ impl Diagnostic for MismatchedArgCount {
let s = if self.expected == 1 { "" } else { "s" }; let s = if self.expected == 1 { "" } else { "s" };
format!("Expected {} argument{}, found {}", self.expected, s, self.found) format!("Expected {} argument{}, found {}", self.expected, s, self.found)
} }
fn fix_source(&self) -> InFile<SyntaxNodePtr> { fn source(&self) -> InFile<SyntaxNodePtr> {
InFile { file_id: self.file, value: self.call_expr.clone().into() } InFile { file_id: self.file, value: self.call_expr.clone().into() }
} }
fn as_any(&self) -> &(dyn Any + Send + 'static) { fn as_any(&self) -> &(dyn Any + Send + 'static) {