1
1
//! FIXME: write short doc here
2
2
3
3
use crate :: completion:: { CompletionContext , Completions } ;
4
+ use ra_syntax:: SmolStr ;
4
5
5
6
/// Complete fields in fields literals.
6
7
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
11
12
_ => return ,
12
13
} ;
13
14
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
+
14
29
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
+ }
16
33
}
17
34
}
18
35
@@ -178,4 +195,47 @@ mod tests {
178
195
]
179
196
"### ) ;
180
197
}
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
+ }
181
241
}
0 commit comments