From 21e5224484b9214648826e1b15aa9150c79a407c Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 27 Jul 2020 18:45:08 +0300 Subject: Custom ranges for missing fields --- crates/ra_hir_expand/src/diagnostics.rs | 5 +-- crates/ra_hir_ty/src/diagnostics.rs | 52 +++++++++++++++++++++++++++++--- crates/ra_hir_ty/src/diagnostics/expr.rs | 1 + crates/ra_ide/src/diagnostics.rs | 6 ++-- 4 files changed, 56 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index 84ba97b14..e889f070f 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs @@ -36,8 +36,9 @@ pub trait AstDiagnostic { impl dyn Diagnostic { pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode { - let node = db.parse_or_expand(self.source().file_id).unwrap(); - self.source().value.to_node(&node) + let source = self.source(); + let node = db.parse_or_expand(source.file_id).unwrap(); + source.value.to_node(&node) } pub fn downcast_ref(&self) -> Option<&D> { diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs index 977c0525b..a5b00ed48 100644 --- a/crates/ra_hir_ty/src/diagnostics.rs +++ b/crates/ra_hir_ty/src/diagnostics.rs @@ -9,7 +9,7 @@ use hir_def::DefWithBodyId; use hir_expand::diagnostics::{AstDiagnostic, Diagnostic, DiagnosticSink}; use hir_expand::{db::AstDatabase, name::Name, HirFileId, InFile}; use ra_prof::profile; -use ra_syntax::{ast, AstNode, AstPtr, SyntaxNodePtr}; +use ra_syntax::{ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr}; use stdx::format_to; use crate::db::HirDatabase; @@ -61,6 +61,17 @@ pub struct MissingFields { pub file: HirFileId, pub field_list: AstPtr, pub missed_fields: Vec, + pub list_parent_path: Option>, +} + +impl MissingFields { + fn root(&self, db: &dyn AstDatabase) -> SyntaxNode { + db.parse_or_expand(self.file).unwrap() + } + + pub fn list_parent_ast(&self, db: &dyn AstDatabase) -> Option { + self.list_parent_path.as_ref().map(|path| path.to_node(&self.root(db))) + } } impl Diagnostic for MissingFields { @@ -83,9 +94,7 @@ impl AstDiagnostic for MissingFields { type AST = ast::RecordExprFieldList; 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::RecordExprFieldList::cast(node).unwrap() + self.field_list.to_node(&self.root(db)) } } @@ -318,6 +327,41 @@ mod tests { assert_eq!(annotations, actual); } + #[test] + fn structure_name_highlighted_for_missing_fields() { + check_diagnostics( + r#" +struct Beefy { + one: i32, + two: i32, + three: i32, + four: i32, + five: i32, + six: i32, + seven: i32, + eight: i32, + nine: i32, + ten: i32, +} +fn baz() { + let zz = Beefy { + //^^^^^... Missing structure fields: + // | - seven + one: (), + two: (), + three: (), + four: (), + five: (), + six: (), + eight: (), + nine: (), + ten: (), + }; +} +"#, + ); + } + #[test] fn no_such_field_diagnostics() { check_diagnostics( diff --git a/crates/ra_hir_ty/src/diagnostics/expr.rs b/crates/ra_hir_ty/src/diagnostics/expr.rs index 95bbf2d95..3c37fc58e 100644 --- a/crates/ra_hir_ty/src/diagnostics/expr.rs +++ b/crates/ra_hir_ty/src/diagnostics/expr.rs @@ -111,6 +111,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> { file: source_ptr.file_id, field_list: AstPtr::new(&field_list), missed_fields, + list_parent_path: record_lit.path().map(|path| AstPtr::new(&path)), }) } } diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 5c8ea46ab..7ae4bda0b 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -100,8 +100,10 @@ pub(crate) fn diagnostics( }; res.borrow_mut().push(Diagnostic { - // TODO kb use a smaller range here - range, + range: d + .list_parent_ast(db) + .map(|path| path.syntax().text_range()) + .unwrap_or(range), message: d.message(), severity: Severity::Error, fix, -- cgit v1.2.3