aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2021-05-17 09:40:34 +0100
committerAleksey Kladov <[email protected]>2021-05-17 09:40:34 +0100
commitdb8fbb99ce6e60c072250bada004de9645431a43 (patch)
tree73249d5fd442574c7e91a20db33b7da55c7914e9 /crates
parente22b21e1bb5580a816f085879f9acdb3183a15cb (diff)
minor: extract fix to file
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/diagnostics/fixes.rs48
-rw-r--r--crates/ide/src/diagnostics/fixes/fill_missing_fields.rs192
2 files changed, 196 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.
3mod fill_missing_fields;
4
3use hir::{ 5use 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};
17use syntax::{ 19use 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
85impl 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
126impl DiagnosticWithFix for MissingOkOrSomeInTailExpr { 86impl DiagnosticWithFix for MissingOkOrSomeInTailExpr {
127 fn fix( 87 fn fix(
128 &self, 88 &self,
diff --git a/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
new file mode 100644
index 000000000..123c2f0af
--- /dev/null
+++ b/crates/ide/src/diagnostics/fixes/fill_missing_fields.rs
@@ -0,0 +1,192 @@
1use hir::{db::AstDatabase, diagnostics::MissingFields, Semantics};
2use ide_assists::AssistResolveStrategy;
3use ide_db::{source_change::SourceChange, RootDatabase};
4use syntax::{algo, ast::make, AstNode};
5use text_edit::TextEdit;
6
7use crate::{
8 diagnostics::{fix, fixes::DiagnosticWithFix},
9 Assist,
10};
11
12impl DiagnosticWithFix for MissingFields {
13 fn fix(
14 &self,
15 sema: &Semantics<RootDatabase>,
16 _resolve: &AssistResolveStrategy,
17 ) -> Option<Assist> {
18 // Note that although we could add a diagnostics to
19 // fill the missing tuple field, e.g :
20 // `struct A(usize);`
21 // `let a = A { 0: () }`
22 // but it is uncommon usage and it should not be encouraged.
23 if self.missed_fields.iter().any(|it| it.as_tuple_index().is_some()) {
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(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 }
51}
52
53#[cfg(test)]
54mod tests {
55 use crate::diagnostics::tests::{check_fix, check_no_diagnostics};
56
57 #[test]
58 fn test_fill_struct_fields_empty() {
59 check_fix(
60 r#"
61struct TestStruct { one: i32, two: i64 }
62
63fn test_fn() {
64 let s = TestStruct {$0};
65}
66"#,
67 r#"
68struct TestStruct { one: i32, two: i64 }
69
70fn test_fn() {
71 let s = TestStruct { one: (), two: () };
72}
73"#,
74 );
75 }
76
77 #[test]
78 fn test_fill_struct_fields_self() {
79 check_fix(
80 r#"
81struct TestStruct { one: i32 }
82
83impl TestStruct {
84 fn test_fn() { let s = Self {$0}; }
85}
86"#,
87 r#"
88struct TestStruct { one: i32 }
89
90impl TestStruct {
91 fn test_fn() { let s = Self { one: () }; }
92}
93"#,
94 );
95 }
96
97 #[test]
98 fn test_fill_struct_fields_enum() {
99 check_fix(
100 r#"
101enum Expr {
102 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
103}
104
105impl Expr {
106 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
107 Expr::Bin {$0 }
108 }
109}
110"#,
111 r#"
112enum Expr {
113 Bin { lhs: Box<Expr>, rhs: Box<Expr> }
114}
115
116impl Expr {
117 fn new_bin(lhs: Box<Expr>, rhs: Box<Expr>) -> Expr {
118 Expr::Bin { lhs: (), rhs: () }
119 }
120}
121"#,
122 );
123 }
124
125 #[test]
126 fn test_fill_struct_fields_partial() {
127 check_fix(
128 r#"
129struct TestStruct { one: i32, two: i64 }
130
131fn test_fn() {
132 let s = TestStruct{ two: 2$0 };
133}
134"#,
135 r"
136struct TestStruct { one: i32, two: i64 }
137
138fn test_fn() {
139 let s = TestStruct{ two: 2, one: () };
140}
141",
142 );
143 }
144
145 #[test]
146 fn test_fill_struct_fields_raw_ident() {
147 check_fix(
148 r#"
149struct TestStruct { r#type: u8 }
150
151fn test_fn() {
152 TestStruct { $0 };
153}
154"#,
155 r"
156struct TestStruct { r#type: u8 }
157
158fn test_fn() {
159 TestStruct { r#type: () };
160}
161",
162 );
163 }
164
165 #[test]
166 fn test_fill_struct_fields_no_diagnostic() {
167 check_no_diagnostics(
168 r#"
169struct TestStruct { one: i32, two: i64 }
170
171fn test_fn() {
172 let one = 1;
173 let s = TestStruct{ one, two: 2 };
174}
175 "#,
176 );
177 }
178
179 #[test]
180 fn test_fill_struct_fields_no_diagnostic_on_spread() {
181 check_no_diagnostics(
182 r#"
183struct TestStruct { one: i32, two: i64 }
184
185fn test_fn() {
186 let one = 1;
187 let s = TestStruct{ ..a };
188}
189"#,
190 );
191 }
192}