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_ty/src/diagnostics.rs | 52 +++++++++++++++++++++++++++++--- crates/ra_hir_ty/src/diagnostics/expr.rs | 1 + 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_ty') 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)), }) } } -- cgit v1.2.3