From c6509a4592b67acc4a99a7ffd6dd688bc6cd29be Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 13 Jun 2021 15:27:15 +0300 Subject: internal: move missing_fields diagnostics --- crates/ide/src/diagnostics.rs | 7 +- crates/ide/src/diagnostics/fixes.rs | 1 - .../src/diagnostics/fixes/fill_missing_fields.rs | 217 ------------------- crates/ide/src/diagnostics/missing_fields.rs | 241 +++++++++++++++++++++ crates/ide/src/diagnostics/unresolved_module.rs | 5 +- 5 files changed, 248 insertions(+), 223 deletions(-) delete mode 100644 crates/ide/src/diagnostics/fixes/fill_missing_fields.rs create mode 100644 crates/ide/src/diagnostics/missing_fields.rs (limited to 'crates/ide/src') diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs index 075aae8d5..ef8c8044c 100644 --- a/crates/ide/src/diagnostics.rs +++ b/crates/ide/src/diagnostics.rs @@ -5,6 +5,7 @@ //! original files. So we need to map the ranges. mod unresolved_module; +mod missing_fields; mod fixes; mod field_shorthand; @@ -123,9 +124,6 @@ pub(crate) fn diagnostics( } let res = RefCell::new(res); let sink_builder = DiagnosticSinkBuilder::new() - .on::(|d| { - res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve)); - }) .on::(|d| { res.borrow_mut().push(diagnostic_with_fix(d, &sema, resolve)); }) @@ -232,7 +230,8 @@ pub(crate) fn diagnostics( let ctx = DiagnosticsContext { config, sema, resolve }; for diag in diags { let d = match diag { - AnyDiagnostic::UnresolvedModule(d) => unresolved_module::render(&ctx, &d), + AnyDiagnostic::UnresolvedModule(d) => unresolved_module::unresolved_module(&ctx, &d), + AnyDiagnostic::MissingFields(d) => missing_fields::missing_fields(&ctx, &d), }; if let Some(code) = d.code { if ctx.config.disabled.contains(code.as_str()) { diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 8640d7231..a2e792b3b 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs @@ -2,7 +2,6 @@ //! The same module also has all curret custom fixes for the diagnostics implemented. mod change_case; mod create_field; -mod fill_missing_fields; mod remove_semicolon; mod replace_with_find_map; mod wrap_tail_expr; diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs deleted file mode 100644 index c76f6008a..000000000 --- a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs +++ /dev/null @@ -1,217 +0,0 @@ -use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics}; -use ide_assists::AssistResolveStrategy; -use ide_db::{source_change::SourceChange, RootDatabase}; -use syntax::{algo, ast::make, AstNode}; -use text_edit::TextEdit; - -use crate::{ - diagnostics::{fix, fixes::DiagnosticWithFixes}, - Assist, -}; - -impl DiagnosticWithFixes for MissingFields { - fn fixes( - &self, - sema: &Semantics, - _resolve: &AssistResolveStrategy, - ) -> Option> { - // Note that although we could add a diagnostics to - // fill the missing tuple field, e.g : - // `struct A(usize);` - // `let a = A { 0: () }` - // but it is uncommon usage and it should not be encouraged. - if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { - return None; - } - - let root = sema.db.parse_or_expand(self.file)?; - let field_list_parent = self.field_list_parent.to_node(&root); - let old_field_list = field_list_parent.record_expr_field_list()?; - let new_field_list = old_field_list.clone_for_update(); - for f in self.missed_fields.iter() { - let field = - make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit())) - .clone_for_update(); - new_field_list.add_field(field); - } - - let edit = { - let mut builder = TextEdit::builder(); - algo::diff(old_field_list.syntax(), new_field_list.syntax()) - .into_text_edit(&mut builder); - builder.finish() - }; - Some(vec![fix( - "fill_missing_fields", - "Fill struct fields", - SourceChange::from_text_edit(self.file.original_file(sema.db), edit), - sema.original_range(field_list_parent.syntax()).range, - )]) - } -} - -#[cfg(test)] -mod tests { - use crate::diagnostics::tests::{check_fix, check_no_diagnostics}; - - #[test] - fn test_fill_struct_fields_empty() { - check_fix( - r#" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let s = TestStruct {$0}; -} -"#, - r#" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let s = TestStruct { one: (), two: () }; -} -"#, - ); - } - - #[test] - fn test_fill_struct_fields_self() { - check_fix( - r#" -struct TestStruct { one: i32 } - -impl TestStruct { - fn test_fn() { let s = Self {$0}; } -} -"#, - r#" -struct TestStruct { one: i32 } - -impl TestStruct { - fn test_fn() { let s = Self { one: () }; } -} -"#, - ); - } - - #[test] - fn test_fill_struct_fields_enum() { - check_fix( - r#" -enum Expr { - Bin { lhs: Box, rhs: Box } -} - -impl Expr { - fn new_bin(lhs: Box, rhs: Box) -> Expr { - Expr::Bin {$0 } - } -} -"#, - r#" -enum Expr { - Bin { lhs: Box, rhs: Box } -} - -impl Expr { - fn new_bin(lhs: Box, rhs: Box) -> Expr { - Expr::Bin { lhs: (), rhs: () } - } -} -"#, - ); - } - - #[test] - fn test_fill_struct_fields_partial() { - check_fix( - r#" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let s = TestStruct{ two: 2$0 }; -} -"#, - r" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let s = TestStruct{ two: 2, one: () }; -} -", - ); - } - - #[test] - fn test_fill_struct_fields_raw_ident() { - check_fix( - r#" -struct TestStruct { r#type: u8 } - -fn test_fn() { - TestStruct { $0 }; -} -"#, - r" -struct TestStruct { r#type: u8 } - -fn test_fn() { - TestStruct { r#type: () }; -} -", - ); - } - - #[test] - fn test_fill_struct_fields_no_diagnostic() { - check_no_diagnostics( - r#" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let one = 1; - let s = TestStruct{ one, two: 2 }; -} - "#, - ); - } - - #[test] - fn test_fill_struct_fields_no_diagnostic_on_spread() { - check_no_diagnostics( - r#" -struct TestStruct { one: i32, two: i64 } - -fn test_fn() { - let one = 1; - let s = TestStruct{ ..a }; -} -"#, - ); - } - - #[test] - fn test_fill_struct_fields_blank_line() { - check_fix( - r#" -struct S { a: (), b: () } - -fn f() { - S { - $0 - }; -} -"#, - r#" -struct S { a: (), b: () } - -fn f() { - S { - a: (), - b: (), - }; -} -"#, - ); - } -} diff --git a/crates/ide/src/diagnostics/missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs new file mode 100644 index 000000000..aee780972 --- /dev/null +++ b/crates/ide/src/diagnostics/missing_fields.rs @@ -0,0 +1,241 @@ +use hir::{db::AstDatabase, InFile}; +use ide_assists::Assist; +use ide_db::source_change::SourceChange; +use stdx::format_to; +use syntax::{algo, ast::make, AstNode, SyntaxNodePtr}; +use text_edit::TextEdit; + +use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; + +// Diagnostic: missing-structure-fields +// +// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. +// +// Example: +// +// ```rust +// struct A { a: u8, b: u8 } +// +// let a = A { a: 10 }; +// ``` +pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic { + let mut message = String::from("Missing structure fields:\n"); + for field in &d.missed_fields { + format_to!(message, "- {}\n", field); + } + + let ptr = InFile::new( + d.file, + d.field_list_parent_path + .clone() + .map(SyntaxNodePtr::from) + .unwrap_or_else(|| d.field_list_parent.clone().into()), + ); + + Diagnostic::new( + "missing-structure-fields", + message, + ctx.sema.diagnostics_display_range(ptr).range, + ) + .with_fixes(fixes(ctx, d)) +} + +fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option> { + // Note that although we could add a diagnostics to + // fill the missing tuple field, e.g : + // `struct A(usize);` + // `let a = A { 0: () }` + // but it is uncommon usage and it should not be encouraged. + if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { + return None; + } + + let root = ctx.sema.db.parse_or_expand(d.file)?; + let field_list_parent = d.field_list_parent.to_node(&root); + let old_field_list = field_list_parent.record_expr_field_list()?; + let new_field_list = old_field_list.clone_for_update(); + for f in d.missed_fields.iter() { + let field = + make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit())) + .clone_for_update(); + new_field_list.add_field(field); + } + + let edit = { + let mut builder = TextEdit::builder(); + algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder); + builder.finish() + }; + Some(vec![fix( + "fill_missing_fields", + "Fill struct fields", + SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit), + ctx.sema.original_range(field_list_parent.syntax()).range, + )]) +} + +#[cfg(test)] +mod tests { + use crate::diagnostics::tests::{check_fix, check_no_diagnostics}; + + #[test] + fn test_fill_struct_fields_empty() { + check_fix( + r#" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let s = TestStruct {$0}; +} +"#, + r#" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let s = TestStruct { one: (), two: () }; +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_self() { + check_fix( + r#" +struct TestStruct { one: i32 } + +impl TestStruct { + fn test_fn() { let s = Self {$0}; } +} +"#, + r#" +struct TestStruct { one: i32 } + +impl TestStruct { + fn test_fn() { let s = Self { one: () }; } +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_enum() { + check_fix( + r#" +enum Expr { + Bin { lhs: Box, rhs: Box } +} + +impl Expr { + fn new_bin(lhs: Box, rhs: Box) -> Expr { + Expr::Bin {$0 } + } +} +"#, + r#" +enum Expr { + Bin { lhs: Box, rhs: Box } +} + +impl Expr { + fn new_bin(lhs: Box, rhs: Box) -> Expr { + Expr::Bin { lhs: (), rhs: () } + } +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_partial() { + check_fix( + r#" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let s = TestStruct{ two: 2$0 }; +} +"#, + r" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let s = TestStruct{ two: 2, one: () }; +} +", + ); + } + + #[test] + fn test_fill_struct_fields_raw_ident() { + check_fix( + r#" +struct TestStruct { r#type: u8 } + +fn test_fn() { + TestStruct { $0 }; +} +"#, + r" +struct TestStruct { r#type: u8 } + +fn test_fn() { + TestStruct { r#type: () }; +} +", + ); + } + + #[test] + fn test_fill_struct_fields_no_diagnostic() { + check_no_diagnostics( + r#" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let one = 1; + let s = TestStruct{ one, two: 2 }; +} + "#, + ); + } + + #[test] + fn test_fill_struct_fields_no_diagnostic_on_spread() { + check_no_diagnostics( + r#" +struct TestStruct { one: i32, two: i64 } + +fn test_fn() { + let one = 1; + let s = TestStruct{ ..a }; +} +"#, + ); + } + + #[test] + fn test_fill_struct_fields_blank_line() { + check_fix( + r#" +struct S { a: (), b: () } + +fn f() { + S { + $0 + }; +} +"#, + r#" +struct S { a: (), b: () } + +fn f() { + S { + a: (), + b: (), + }; +} +"#, + ); + } +} diff --git a/crates/ide/src/diagnostics/unresolved_module.rs b/crates/ide/src/diagnostics/unresolved_module.rs index abf53a57c..4c8c74ff7 100644 --- a/crates/ide/src/diagnostics/unresolved_module.rs +++ b/crates/ide/src/diagnostics/unresolved_module.rs @@ -8,7 +8,10 @@ use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext}; // Diagnostic: unresolved-module // // This diagnostic is triggered if rust-analyzer is unable to discover referred module. -pub(super) fn render(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Diagnostic { +pub(super) fn unresolved_module( + ctx: &DiagnosticsContext<'_>, + d: &hir::UnresolvedModule, +) -> Diagnostic { Diagnostic::new( "unresolved-module", "unresolved module", -- cgit v1.2.3