Skip to content

Commit 8617fe6

Browse files
Merge #3694
3694: Complete only missing fields r=matklad a=SomeoneToIgnore Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
2 parents c23c76e + f1cf1cc commit 8617fe6

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

crates/ra_ide/src/completion/complete_record_literal.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! FIXME: write short doc here
22
33
use crate::completion::{CompletionContext, Completions};
4+
use ra_syntax::SmolStr;
45

56
/// Complete fields in fields literals.
67
pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionContext) {
@@ -11,8 +12,24 @@ pub(super) fn complete_record_literal(acc: &mut Completions, ctx: &CompletionCon
1112
_ => return,
1213
};
1314

15+
let already_present_names: Vec<SmolStr> = ctx
16+
.record_lit_syntax
17+
.as_ref()
18+
.and_then(|record_literal| record_literal.record_field_list())
19+
.map(|field_list| field_list.fields())
20+
.map(|fields| {
21+
fields
22+
.into_iter()
23+
.filter_map(|field| field.name_ref())
24+
.map(|name_ref| name_ref.text().clone())
25+
.collect()
26+
})
27+
.unwrap_or_default();
28+
1429
for (field, field_ty) in ty.variant_fields(ctx.db, variant) {
15-
acc.add_field(ctx, field, &field_ty);
30+
if !already_present_names.contains(&SmolStr::from(field.name(ctx.db).to_string())) {
31+
acc.add_field(ctx, field, &field_ty);
32+
}
1633
}
1734
}
1835

@@ -178,4 +195,47 @@ mod tests {
178195
]
179196
"###);
180197
}
198+
199+
#[test]
200+
fn only_missing_fields_are_completed() {
201+
let completions = complete(
202+
r"
203+
struct S {
204+
foo1: u32,
205+
foo2: u32,
206+
bar: u32,
207+
baz: u32,
208+
}
209+
210+
fn main() {
211+
let foo1 = 1;
212+
let s = S {
213+
foo1,
214+
foo2: 5,
215+
<|>
216+
}
217+
}
218+
",
219+
);
220+
assert_debug_snapshot!(completions, @r###"
221+
[
222+
CompletionItem {
223+
label: "bar",
224+
source_range: [302; 302),
225+
delete: [302; 302),
226+
insert: "bar",
227+
kind: Field,
228+
detail: "u32",
229+
},
230+
CompletionItem {
231+
label: "baz",
232+
source_range: [302; 302),
233+
delete: [302; 302),
234+
insert: "baz",
235+
kind: Field,
236+
detail: "u32",
237+
},
238+
]
239+
"###);
240+
}
181241
}

0 commit comments

Comments
 (0)