diff options
author | Kirill Bulatov <[email protected]> | 2020-03-23 22:18:05 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-03-23 22:36:06 +0000 |
commit | 00cbe81a5b4e47ae28003f3314e8f56912493459 (patch) | |
tree | 148d2f21c774946b7e0c75d45347abc00f2bd623 /crates/ra_ide | |
parent | eff1b3fe4d17dcecf0ec9a30c35d6c88715cb8ea (diff) |
Complete only missing fields
Diffstat (limited to 'crates/ra_ide')
-rw-r--r-- | crates/ra_ide/src/completion/complete_record_literal.rs | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/complete_record_literal.rs b/crates/ra_ide/src/completion/complete_record_literal.rs index 83ed1d52c..a0bc3ee0a 100644 --- a/crates/ra_ide/src/completion/complete_record_literal.rs +++ b/crates/ra_ide/src/completion/complete_record_literal.rs | |||
@@ -11,8 +11,24 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon | |||
11 | _ => return, | 11 | _ => return, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | let already_present_names: Vec<String> = ctx | ||
15 | .record_lit_syntax | ||
16 | .as_ref() | ||
17 | .and_then(|record_literal| record_literal.record_field_list()) | ||
18 | .map(|field_list| field_list.fields()) | ||
19 | .map(|fields| { | ||
20 | fields | ||
21 | .into_iter() | ||
22 | .filter_map(|field| field.name_ref()) | ||
23 | .map(|name_ref| name_ref.to_string()) | ||
24 | .collect() | ||
25 | }) | ||
26 | .unwrap_or_default(); | ||
27 | |||
14 | for (field, field_ty) in ty.variant_fields(ctx.db, variant) { | 28 | for (field, field_ty) in ty.variant_fields(ctx.db, variant) { |
15 | acc.add_field(ctx, field, &field_ty); | 29 | if !already_present_names.contains(&field.name(ctx.db).to_string()) { |
30 | acc.add_field(ctx, field, &field_ty); | ||
31 | } | ||
16 | } | 32 | } |
17 | } | 33 | } |
18 | 34 | ||
@@ -178,4 +194,47 @@ mod tests { | |||
178 | ] | 194 | ] |
179 | "###); | 195 | "###); |
180 | } | 196 | } |
197 | |||
198 | #[test] | ||
199 | fn only_missing_fields_are_completed() { | ||
200 | let completions = complete( | ||
201 | r" | ||
202 | struct S { | ||
203 | foo1: u32, | ||
204 | foo2: u32, | ||
205 | bar: u32, | ||
206 | baz: u32, | ||
207 | } | ||
208 | |||
209 | fn main() { | ||
210 | let foo1 = 1; | ||
211 | let s = S { | ||
212 | foo1, | ||
213 | foo2: 5, | ||
214 | <|> | ||
215 | } | ||
216 | } | ||
217 | ", | ||
218 | ); | ||
219 | assert_debug_snapshot!(completions, @r###" | ||
220 | [ | ||
221 | CompletionItem { | ||
222 | label: "bar", | ||
223 | source_range: [302; 302), | ||
224 | delete: [302; 302), | ||
225 | insert: "bar", | ||
226 | kind: Field, | ||
227 | detail: "u32", | ||
228 | }, | ||
229 | CompletionItem { | ||
230 | label: "baz", | ||
231 | source_range: [302; 302), | ||
232 | delete: [302; 302), | ||
233 | insert: "baz", | ||
234 | kind: Field, | ||
235 | detail: "u32", | ||
236 | }, | ||
237 | ] | ||
238 | "###); | ||
239 | } | ||
181 | } | 240 | } |