Skip to content

Commit 00cbe81

Browse files
Complete only missing fields
1 parent eff1b3f commit 00cbe81

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

crates/ra_ide/src/completion/complete_record_literal.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,24 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon
1111
_ => return,
1212
};
1313

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+
1428
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+
}
1632
}
1733
}
1834

@@ -178,4 +194,47 @@ mod tests {
178194
]
179195
"###);
180196
}
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+
}
181240
}

0 commit comments

Comments
 (0)