Skip to content

Commit 549ce9a

Browse files
Merge #4198
4198: Complete union fields after dot r=matklad a=jonas-schievink Co-authored-by: Jonas Schievink <[email protected]>
2 parents 0d7c997 + 8cb1390 commit 549ce9a

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,18 +1157,21 @@ impl Type {
11571157

11581158
pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> {
11591159
if let Ty::Apply(a_ty) = &self.ty.value {
1160-
if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor {
1161-
let var_def = s.into();
1162-
return db
1163-
.field_types(var_def)
1164-
.iter()
1165-
.map(|(local_id, ty)| {
1166-
let def = Field { parent: var_def.into(), id: local_id };
1167-
let ty = ty.clone().subst(&a_ty.parameters);
1168-
(def, self.derived(ty))
1169-
})
1170-
.collect();
1171-
}
1160+
let variant_id = match a_ty.ctor {
1161+
TypeCtor::Adt(AdtId::StructId(s)) => s.into(),
1162+
TypeCtor::Adt(AdtId::UnionId(u)) => u.into(),
1163+
_ => return Vec::new(),
1164+
};
1165+
1166+
return db
1167+
.field_types(variant_id)
1168+
.iter()
1169+
.map(|(local_id, ty)| {
1170+
let def = Field { parent: variant_id.into(), id: local_id };
1171+
let ty = ty.clone().subst(&a_ty.parameters);
1172+
(def, self.derived(ty))
1173+
})
1174+
.collect();
11721175
};
11731176
Vec::new()
11741177
}

crates/ra_ide/src/completion/complete_dot.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,44 @@ mod tests {
249249
);
250250
}
251251

252+
#[test]
253+
fn test_union_field_completion() {
254+
assert_debug_snapshot!(
255+
do_ref_completion(
256+
r"
257+
union Un {
258+
field: u8,
259+
other: u16,
260+
}
261+
262+
fn foo(u: Un) {
263+
u.<|>
264+
}
265+
",
266+
),
267+
@r###"
268+
[
269+
CompletionItem {
270+
label: "field",
271+
source_range: 140..140,
272+
delete: 140..140,
273+
insert: "field",
274+
kind: Field,
275+
detail: "u8",
276+
},
277+
CompletionItem {
278+
label: "other",
279+
source_range: 140..140,
280+
delete: 140..140,
281+
insert: "other",
282+
kind: Field,
283+
detail: "u16",
284+
},
285+
]
286+
"###
287+
);
288+
}
289+
252290
#[test]
253291
fn test_method_completion() {
254292
assert_debug_snapshot!(

0 commit comments

Comments
 (0)