diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/fill_struct_fields.rs | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/crates/ra_assists/src/fill_struct_fields.rs b/crates/ra_assists/src/fill_struct_fields.rs index 398dbcd21..6e61a58fc 100644 --- a/crates/ra_assists/src/fill_struct_fields.rs +++ b/crates/ra_assists/src/fill_struct_fields.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use std::fmt::Write; | 1 | use std::fmt::Write; |
2 | 2 | ||
3 | use hir::{AdtDef, Ty, db::HirDatabase, source_binder::function_from_child_node, StructField}; | 3 | use hir::{AdtDef, Ty, db::HirDatabase, source_binder::function_from_child_node}; |
4 | 4 | ||
5 | use ra_syntax::ast::{self, AstNode}; | 5 | use ra_syntax::ast::{self, AstNode}; |
6 | 6 | ||
@@ -26,7 +26,7 @@ pub(crate) fn fill_struct_fields(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
26 | struct FillStructFields<'a, 'b: 'a, DB> { | 26 | struct FillStructFields<'a, 'b: 'a, DB> { |
27 | ctx: &'a mut AssistCtx<'b, DB>, | 27 | ctx: &'a mut AssistCtx<'b, DB>, |
28 | named_field_list: &'a ast::NamedFieldList, | 28 | named_field_list: &'a ast::NamedFieldList, |
29 | struct_fields: Vec<StructField>, | 29 | struct_fields: Vec<(String, String)>, |
30 | struct_lit: &'a ast::StructLit, | 30 | struct_lit: &'a ast::StructLit, |
31 | } | 31 | } |
32 | 32 | ||
@@ -64,35 +64,29 @@ where | |||
64 | Ty::Adt { def_id: AdtDef::Struct(s), .. } => s, | 64 | Ty::Adt { def_id: AdtDef::Struct(s), .. } => s, |
65 | _ => return None, | 65 | _ => return None, |
66 | }; | 66 | }; |
67 | self.struct_fields = struct_def.fields(self.ctx.db); | 67 | self.struct_fields = struct_def |
68 | .fields(self.ctx.db) | ||
69 | .into_iter() | ||
70 | .map(|f| (f.name(self.ctx.db).to_string(), "()".into())) | ||
71 | .collect(); | ||
68 | Some(()) | 72 | Some(()) |
69 | } | 73 | } |
70 | 74 | ||
71 | fn remove_already_included_fields(&mut self) -> Option<()> { | 75 | fn remove_already_included_fields(&mut self) -> Option<()> { |
72 | for ast_field in self.named_field_list.fields() { | 76 | for ast_field in self.named_field_list.fields() { |
77 | let expr = ast_field.expr()?.syntax().text().to_string(); | ||
73 | let name_from_ast = ast_field.name_ref()?.text().to_string(); | 78 | let name_from_ast = ast_field.name_ref()?.text().to_string(); |
74 | if let Some(idx) = self | 79 | if let Some(idx) = self.struct_fields.iter().position(|(n, _)| n == &name_from_ast) { |
75 | .struct_fields | 80 | self.struct_fields[idx] = (name_from_ast, expr); |
76 | .iter() | ||
77 | .map(|f| f.name(self.ctx.db).to_string()) | ||
78 | .position(|n| n == name_from_ast) | ||
79 | { | ||
80 | self.struct_fields.remove(idx); | ||
81 | } | 81 | } |
82 | } | 82 | } |
83 | Some(()) | 83 | Some(()) |
84 | } | 84 | } |
85 | 85 | ||
86 | fn struct_fields_string(&self) -> Option<String> { | 86 | fn struct_fields_string(&mut self) -> Option<String> { |
87 | let mut buf = String::from("{\n"); | 87 | let mut buf = String::from("{\n"); |
88 | for field in self.named_field_list.fields() { | 88 | for (name, expr) in &self.struct_fields { |
89 | let expr = field.expr()?.syntax().text().to_string(); | 89 | write!(&mut buf, " {}: {},\n", name, expr).unwrap(); |
90 | let field_name = field.name_ref()?.syntax().text().to_string(); | ||
91 | write!(&mut buf, " {}: {},\n", field_name, expr).unwrap(); | ||
92 | } | ||
93 | for field in &self.struct_fields { | ||
94 | let field_name = field.name(self.ctx.db).to_string(); | ||
95 | write!(&mut buf, " {}: (),\n", field_name).unwrap(); | ||
96 | } | 90 | } |
97 | buf.push_str("}"); | 91 | buf.push_str("}"); |
98 | Some(buf) | 92 | Some(buf) |
@@ -233,11 +227,11 @@ mod tests { | |||
233 | 227 | ||
234 | fn main() { | 228 | fn main() { |
235 | let s = <|>S { | 229 | let s = <|>S { |
236 | c: (1, 2), | ||
237 | e: "foo", | ||
238 | a: (), | 230 | a: (), |
239 | b: (), | 231 | b: (), |
232 | c: (1, 2), | ||
240 | d: (), | 233 | d: (), |
234 | e: "foo", | ||
241 | } | 235 | } |
242 | } | 236 | } |
243 | "#, | 237 | "#, |