diff options
Diffstat (limited to 'crates/ide/src/diagnostics/fixes.rs')
-rw-r--r-- | crates/ide/src/diagnostics/fixes.rs | 48 |
1 files changed, 4 insertions, 44 deletions
diff --git a/crates/ide/src/diagnostics/fixes.rs b/crates/ide/src/diagnostics/fixes.rs index 695b59e27..5330449f9 100644 --- a/crates/ide/src/diagnostics/fixes.rs +++ b/crates/ide/src/diagnostics/fixes.rs | |||
@@ -1,10 +1,12 @@ | |||
1 | //! Provides a way to attach fixes to the diagnostics. | 1 | //! Provides a way to attach fixes to the diagnostics. |
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. |
3 | mod fill_missing_fields; | ||
4 | |||
3 | use hir::{ | 5 | use hir::{ |
4 | db::AstDatabase, | 6 | db::AstDatabase, |
5 | diagnostics::{ | 7 | diagnostics::{ |
6 | Diagnostic, IncorrectCase, MissingFields, MissingOkOrSomeInTailExpr, NoSuchField, | 8 | Diagnostic, IncorrectCase, MissingOkOrSomeInTailExpr, NoSuchField, RemoveThisSemicolon, |
7 | RemoveThisSemicolon, ReplaceFilterMapNextWithFindMap, UnresolvedModule, | 9 | ReplaceFilterMapNextWithFindMap, UnresolvedModule, |
8 | }, | 10 | }, |
9 | HasSource, HirDisplay, InFile, Semantics, VariantDef, | 11 | HasSource, HirDisplay, InFile, Semantics, VariantDef, |
10 | }; | 12 | }; |
@@ -15,7 +17,6 @@ use ide_db::{ | |||
15 | RootDatabase, | 17 | RootDatabase, |
16 | }; | 18 | }; |
17 | use syntax::{ | 19 | use syntax::{ |
18 | algo, | ||
19 | ast::{self, edit::IndentLevel, make, ArgListOwner}, | 20 | ast::{self, edit::IndentLevel, make, ArgListOwner}, |
20 | AstNode, TextRange, | 21 | AstNode, TextRange, |
21 | }; | 22 | }; |
@@ -82,47 +83,6 @@ impl DiagnosticWithFix for NoSuchField { | |||
82 | } | 83 | } |
83 | } | 84 | } |
84 | 85 | ||
85 | impl DiagnosticWithFix for MissingFields { | ||
86 | fn fix( | ||
87 | &self, | ||
88 | sema: &Semantics<RootDatabase>, | ||
89 | _resolve: &AssistResolveStrategy, | ||
90 | ) -> Option<Assist> { | ||
91 | // Note that although we could add a diagnostics to | ||
92 | // fill the missing tuple field, e.g : | ||
93 | // `struct A(usize);` | ||
94 | // `let a = A { 0: () }` | ||
95 | // but it is uncommon usage and it should not be encouraged. | ||
96 | if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) { | ||
97 | return None; | ||
98 | } | ||
99 | |||
100 | let root = sema.db.parse_or_expand(self.file)?; | ||
101 | let field_list_parent = self.field_list_parent.to_node(&root); | ||
102 | let old_field_list = field_list_parent.record_expr_field_list()?; | ||
103 | let new_field_list = old_field_list.clone_for_update(); | ||
104 | for f in self.missed_fields.iter() { | ||
105 | let field = | ||
106 | make::record_expr_field(make::name_ref(&f.to_string()), Some(make::expr_unit())) | ||
107 | .clone_for_update(); | ||
108 | new_field_list.add_field(field); | ||
109 | } | ||
110 | |||
111 | let edit = { | ||
112 | let mut builder = TextEdit::builder(); | ||
113 | algo::diff(&old_field_list.syntax(), &new_field_list.syntax()) | ||
114 | .into_text_edit(&mut builder); | ||
115 | builder.finish() | ||
116 | }; | ||
117 | Some(fix( | ||
118 | "fill_missing_fields", | ||
119 | "Fill struct fields", | ||
120 | SourceChange::from_text_edit(self.file.original_file(sema.db), edit), | ||
121 | sema.original_range(&field_list_parent.syntax()).range, | ||
122 | )) | ||
123 | } | ||
124 | } | ||
125 | |||
126 | impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { | 86 | impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { |
127 | fn fix( | 87 | fn fix( |
128 | &self, | 88 | &self, |