From ee1586c1ed058ff0f090b552d52fe6bbe2dd7f7f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 27 Jul 2020 22:46:25 +0300 Subject: Better naming --- crates/ra_hir/src/semantics.rs | 11 +++++++ crates/ra_hir_def/src/diagnostics.rs | 2 +- crates/ra_hir_expand/src/diagnostics.rs | 6 ++-- crates/ra_hir_ty/src/diagnostics.rs | 55 +++++++++++++++------------------ crates/ra_ide/src/diagnostics.rs | 31 ++++++++++--------- 5 files changed, 57 insertions(+), 48 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/semantics.rs b/crates/ra_hir/src/semantics.rs index e392130ab..1c5dc3d51 100644 --- a/crates/ra_hir/src/semantics.rs +++ b/crates/ra_hir/src/semantics.rs @@ -145,6 +145,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> { self.imp.original_range(node) } + pub fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + self.imp.diagnostics_fix_range(diagnostics) + } + pub fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { self.imp.diagnostics_range(diagnostics) } @@ -376,6 +380,13 @@ impl<'db> SemanticsImpl<'db> { original_range(self.db, node.as_ref()) } + fn diagnostics_fix_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { + let src = diagnostics.fix_source(); + let root = self.db.parse_or_expand(src.file_id).unwrap(); + let node = src.value.to_node(&root); + original_range(self.db, src.with_value(&node)) + } + fn diagnostics_range(&self, diagnostics: &dyn Diagnostic) -> FileRange { let src = diagnostics.source(); let root = self.db.parse_or_expand(src.file_id).unwrap(); diff --git a/crates/ra_hir_def/src/diagnostics.rs b/crates/ra_hir_def/src/diagnostics.rs index 30db48f86..e53269589 100644 --- a/crates/ra_hir_def/src/diagnostics.rs +++ b/crates/ra_hir_def/src/diagnostics.rs @@ -18,7 +18,7 @@ impl Diagnostic for UnresolvedModule { fn message(&self) -> String { "unresolved module".to_string() } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile::new(self.file, self.decl.clone().into()) } fn as_any(&self) -> &(dyn Any + Send + 'static) { diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index ffeca5e82..074a8c45e 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs @@ -22,9 +22,9 @@ use crate::{db::AstDatabase, InFile}; pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { fn message(&self) -> String; - fn source(&self) -> InFile; - fn highlighting_source(&self) -> InFile { - self.source() + fn fix_source(&self) -> InFile; + fn source(&self) -> InFile { + self.fix_source() } fn as_any(&self) -> &(dyn Any + Send + 'static); fn is_experimental(&self) -> bool { diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 73d241434..a4cede81d 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -37,7 +37,7 @@ impl Diagnostic for NoSuchField { "no such field".to_string() } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile::new(self.file, self.field.clone().into()) } @@ -50,9 +50,8 @@ impl AstDiagnostic for NoSuchField { type AST = ast::RecordExprField; fn ast(&self, db: &dyn AstDatabase) -> Self::AST { - let root = db.parse_or_expand(self.source().file_id).unwrap(); - let node = self.source().value.to_node(&root); - ast::RecordExprField::cast(node).unwrap() + let root = db.parse_or_expand(self.file).unwrap(); + self.field.to_node(&root) } } @@ -72,19 +71,19 @@ impl Diagnostic for MissingFields { } buf } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } - fn as_any(&self) -> &(dyn Any + Send + 'static) { - self - } - - fn highlighting_source(&self) -> InFile { + fn source(&self) -> InFile { self.list_parent_path .clone() .map(|path| InFile { file_id: self.file, value: path.into() }) - .unwrap_or_else(|| self.source()) + .unwrap_or_else(|| self.fix_source()) + } + + fn as_any(&self) -> &(dyn Any + Send + 'static) { + self } } @@ -112,7 +111,7 @@ impl Diagnostic for MissingPatFields { } buf } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.field_list.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -131,7 +130,7 @@ impl Diagnostic for MissingMatchArms { fn message(&self) -> String { String::from("Missing match arm") } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.match_expr.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -149,7 +148,7 @@ impl Diagnostic for MissingOkInTailExpr { fn message(&self) -> String { "wrap return expression in Ok".to_string() } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -162,8 +161,7 @@ impl AstDiagnostic for MissingOkInTailExpr { fn ast(&self, db: &dyn AstDatabase) -> Self::AST { let root = db.parse_or_expand(self.file).unwrap(); - let node = self.source().value.to_node(&root); - ast::Expr::cast(node).unwrap() + self.expr.to_node(&root) } } @@ -177,7 +175,7 @@ impl Diagnostic for BreakOutsideOfLoop { fn message(&self) -> String { "break outside of loop".to_string() } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -190,8 +188,7 @@ impl AstDiagnostic for BreakOutsideOfLoop { fn ast(&self, db: &dyn AstDatabase) -> Self::AST { let root = db.parse_or_expand(self.file).unwrap(); - let node = self.source().value.to_node(&root); - ast::Expr::cast(node).unwrap() + self.expr.to_node(&root) } } @@ -205,7 +202,7 @@ impl Diagnostic for MissingUnsafe { fn message(&self) -> String { format!("This operation is unsafe and requires an unsafe function or block") } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.expr.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -217,9 +214,8 @@ impl AstDiagnostic for MissingUnsafe { type AST = ast::Expr; fn ast(&self, db: &dyn AstDatabase) -> Self::AST { - let root = db.parse_or_expand(self.source().file_id).unwrap(); - let node = self.source().value.to_node(&root); - ast::Expr::cast(node).unwrap() + let root = db.parse_or_expand(self.file).unwrap(); + self.expr.to_node(&root) } } @@ -236,7 +232,7 @@ impl Diagnostic for MismatchedArgCount { let s = if self.expected == 1 { "" } else { "s" }; format!("Expected {} argument{}, found {}", self.expected, s, self.found) } - fn source(&self) -> InFile { + fn fix_source(&self) -> InFile { InFile { file_id: self.file, value: self.call_expr.clone().into() } } fn as_any(&self) -> &(dyn Any + Send + 'static) { @@ -250,7 +246,7 @@ impl Diagnostic for MismatchedArgCount { impl AstDiagnostic for MismatchedArgCount { type AST = ast::CallExpr; fn ast(&self, db: &dyn AstDatabase) -> Self::AST { - let root = db.parse_or_expand(self.source().file_id).unwrap(); + let root = db.parse_or_expand(self.file).unwrap(); let node = self.source().value.to_node(&root); ast::CallExpr::cast(node).unwrap() } @@ -308,12 +304,11 @@ mod tests { let mut actual: FxHashMap> = FxHashMap::default(); db.diagnostics(|d| { // FXIME: macros... - let file_id = d.source().file_id.original_file(&db); - let highlighting_source = d.highlighting_source(); - let node = db.parse_or_expand(highlighting_source.file_id).unwrap(); - let range = highlighting_source.value.to_node(&node).text_range(); + let source = d.source(); + let root = db.parse_or_expand(source.file_id).unwrap(); + let range = source.value.to_node(&root).text_range(); let message = d.message().to_owned(); - actual.entry(file_id).or_default().push((range, message)); + actual.entry(source.file_id.original_file(&db)).or_default().push((range, message)); }); for (file_id, diags) in actual.iter_mut() { diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index e847df6ea..0d2ff17e1 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -54,18 +54,19 @@ pub(crate) fn diagnostics( let res = RefCell::new(res); let mut sink = DiagnosticSinkBuilder::new() .on::(|d| { - let original_file = d.source().file_id.original_file(db); let fix = Fix::new( "Create module", - FileSystemEdit::CreateFile { anchor: original_file, dst: d.candidate.clone() } - .into(), + FileSystemEdit::CreateFile { + anchor: d.file.original_file(db), + dst: d.candidate.clone(), + } + .into(), ); - let range = sema.diagnostics_range(d).range; res.borrow_mut().push(Diagnostic { - range, + range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, - fix: Some((fix, range)), + fix: Some((fix, sema.diagnostics_fix_range(d).range)), }) }) .on::(|d| { @@ -94,12 +95,12 @@ pub(crate) fn diagnostics( }; Some(( Fix::new("Fill struct fields", SourceFileEdit { file_id, edit }.into()), - sema.diagnostics_range(d).range, + sema.diagnostics_fix_range(d).range, )) }; res.borrow_mut().push(Diagnostic { - range: d.highlighting_source().file_syntax(db).text_range(), + range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, fix, @@ -110,21 +111,23 @@ pub(crate) fn diagnostics( let replacement = format!("Ok({})", node.syntax()); let edit = TextEdit::replace(node.syntax().text_range(), replacement); let source_change = SourceFileEdit { file_id, edit }.into(); - let range = sema.diagnostics_range(d).range; res.borrow_mut().push(Diagnostic { - range, + range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, - fix: Some((Fix::new("Wrap with ok", source_change), range)), + fix: Some(( + Fix::new("Wrap with ok", source_change), + sema.diagnostics_fix_range(d).range, + )), }) }) .on::(|d| { - let range = sema.diagnostics_range(d).range; res.borrow_mut().push(Diagnostic { - range, + range: sema.diagnostics_range(d).range, message: d.message(), severity: Severity::Error, - fix: missing_struct_field_fix(&sema, file_id, d).map(|fix| (fix, range)), + fix: missing_struct_field_fix(&sema, file_id, d) + .map(|fix| (fix, sema.diagnostics_fix_range(d).range)), }) }) // Only collect experimental diagnostics when they're enabled. -- cgit v1.2.3