aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/diagnostics
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/diagnostics')
-rw-r--r--crates/ide/src/diagnostics/fixes.rs1
-rw-r--r--crates/ide/src/diagnostics/missing_fields.rs (renamed from crates/ide/src/diagnostics/fixes/fill_missing_fields.rs)118
-rw-r--r--crates/ide/src/diagnostics/unresolved_module.rs5
3 files changed, 75 insertions, 49 deletions
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 @@
2//! The same module also has all curret custom fixes for the diagnostics implemented. 2//! The same module also has all curret custom fixes for the diagnostics implemented.
3mod change_case; 3mod change_case;
4mod create_field; 4mod create_field;
5mod fill_missing_fields;
6mod remove_semicolon; 5mod remove_semicolon;
7mod replace_with_find_map; 6mod replace_with_find_map;
8mod wrap_tail_expr; 7mod wrap_tail_expr;
diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/missing_fields.rs
index c76f6008a..aee780972 100644
--- a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
+++ b/crates/ide/src/diagnostics/missing_fields.rs
@@ -1,53 +1,77 @@
1use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics}; 1use hir::{db::AstDatabase, InFile};
2use ide_assists::AssistResolveStrategy; 2use ide_assists::Assist;
3use ide_db::{source_change::SourceChange, RootDatabase}; 3use ide_db::source_change::SourceChange;
4use syntax::{algo, ast::make, AstNode}; 4use stdx::format_to;
5use syntax::{algo, ast::make, AstNode, SyntaxNodePtr};
5use text_edit::TextEdit; 6use text_edit::TextEdit;
6 7
7use crate::{ 8use crate::diagnostics::{fix, Diagnostic, DiagnosticsContext};
8 diagnostics::{fix, fixes::DiagnosticWithFixes}, 9
9 Assist, 10// Diagnostic: missing-structure-fields
10}; 11//
11 12// This diagnostic is triggered if record lacks some fields that exist in the corresponding structure.
12impl DiagnosticWithFixes for MissingFields { 13//
13 fn fixes( 14// Example:
14 &self, 15//
15 sema: &Semantics<RootDatabase>, 16// ```rust
16 _resolve: &AssistResolveStrategy, 17// struct A { a: u8, b: u8 }
17 ) -> Option<Vec<Assist>> { 18//
18 // Note that although we could add a diagnostics to 19// let a = A { a: 10 };
19 // fill the missing tuple field, e.g : 20// ```
20 // `struct A(usize);` 21pub(super) fn missing_fields(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Diagnostic {
21 // `let a = A { 0: () }` 22 let mut message = String::from("Missing structure fields:\n");
22 // but it is uncommon usage and it should not be encouraged. 23 for field in &d.missed_fields {
23 if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { 24 format_to!(message, "- {}\n", field);
24 return None;
25 }
26
27 let root = sema.db.parse_or_expand(self.file)?;
28 let field_list_parent = self.field_list_parent.to_node(&root);
29 let old_field_list = field_list_parent.record_expr_field_list()?;
30 let new_field_list = old_field_list.clone_for_update();
31 for f in self.missed_fields.iter() {
32 let field =
33 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
34 .clone_for_update();
35 new_field_list.add_field(field);
36 }
37
38 let edit = {
39 let mut builder = TextEdit::builder();
40 algo::diff(old_field_list.syntax(), new_field_list.syntax())
41 .into_text_edit(&mut builder);
42 builder.finish()
43 };
44 Some(vec![fix(
45 "fill_missing_fields",
46 "Fill struct fields",
47 SourceChange::from_text_edit(self.file.original_file(sema.db), edit),
48 sema.original_range(field_list_parent.syntax()).range,
49 )])
50 } 25 }
26
27 let ptr = InFile::new(
28 d.file,
29 d.field_list_parent_path
30 .clone()
31 .map(SyntaxNodePtr::from)
32 .unwrap_or_else(|| d.field_list_parent.clone().into()),
33 );
34
35 Diagnostic::new(
36 "missing-structure-fields",
37 message,
38 ctx.sema.diagnostics_display_range(ptr).range,
39 )
40 .with_fixes(fixes(ctx, d))
41}
42
43fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Assist>> {
44 // Note that although we could add a diagnostics to
45 // fill the missing tuple field, e.g :
46 // `struct A(usize);`
47 // `let a = A { 0: () }`
48 // but it is uncommon usage and it should not be encouraged.
49 if d.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
50 return None;
51 }
52
53 let root = ctx.sema.db.parse_or_expand(d.file)?;
54 let field_list_parent = d.field_list_parent.to_node(&root);
55 let old_field_list = field_list_parent.record_expr_field_list()?;
56 let new_field_list = old_field_list.clone_for_update();
57 for f in d.missed_fields.iter() {
58 let field =
59 make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit()))
60 .clone_for_update();
61 new_field_list.add_field(field);
62 }
63
64 let edit = {
65 let mut builder = TextEdit::builder();
66 algo::diff(old_field_list.syntax(), new_field_list.syntax()).into_text_edit(&mut builder);
67 builder.finish()
68 };
69 Some(vec![fix(
70 "fill_missing_fields",
71 "Fill struct fields",
72 SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit),
73 ctx.sema.original_range(field_list_parent.syntax()).range,
74 )])
51} 75}
52 76
53#[cfg(test)] 77#[cfg(test)]
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};
8// Diagnostic: unresolved-module 8// Diagnostic: unresolved-module
9// 9//
10// This diagnostic is triggered if rust-analyzer is unable to discover referred module. 10// This diagnostic is triggered if rust-analyzer is unable to discover referred module.
11pub(super) fn render(ctx: &DiagnosticsContext<'_>, d: &hir::UnresolvedModule) -> Diagnostic { 11pub(super) fn unresolved_module(
12 ctx: &DiagnosticsContext<'_>,
13 d: &hir::UnresolvedModule,
14) -> Diagnostic {
12 Diagnostic::new( 15 Diagnostic::new(
13 "unresolved-module", 16 "unresolved-module",
14 "unresolved module", 17 "unresolved module",