diff options
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 27 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 38 |
2 files changed, 53 insertions, 12 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index fb788736d..3fb419571 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -1157,18 +1157,21 @@ impl Type { | |||
1157 | 1157 | ||
1158 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { | 1158 | pub fn fields(&self, db: &dyn HirDatabase) -> Vec<(Field, Type)> { |
1159 | if let Ty::Apply(a_ty) = &self.ty.value { | 1159 | if let Ty::Apply(a_ty) = &self.ty.value { |
1160 | if let TypeCtor::Adt(AdtId::StructId(s)) = a_ty.ctor { | 1160 | let variant_id = match a_ty.ctor { |
1161 | let var_def = s.into(); | 1161 | TypeCtor::Adt(AdtId::StructId(s)) => s.into(), |
1162 | return db | 1162 | TypeCtor::Adt(AdtId::UnionId(u)) => u.into(), |
1163 | .field_types(var_def) | 1163 | _ => return Vec::new(), |
1164 | .iter() | 1164 | }; |
1165 | .map(|(local_id, ty)| { | 1165 | |
1166 | let def = Field { parent: var_def.into(), id: local_id }; | 1166 | return db |
1167 | let ty = ty.clone().subst(&a_ty.parameters); | 1167 | .field_types(variant_id) |
1168 | (def, self.derived(ty)) | 1168 | .iter() |
1169 | }) | 1169 | .map(|(local_id, ty)| { |
1170 | .collect(); | 1170 | let def = Field { parent: variant_id.into(), id: local_id }; |
1171 | } | 1171 | let ty = ty.clone().subst(&a_ty.parameters); |
1172 | (def, self.derived(ty)) | ||
1173 | }) | ||
1174 | .collect(); | ||
1172 | }; | 1175 | }; |
1173 | Vec::new() | 1176 | Vec::new() |
1174 | } | 1177 | } |
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index 814354ffa..05f825c6f 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs | |||
@@ -250,6 +250,44 @@ mod tests { | |||
250 | } | 250 | } |
251 | 251 | ||
252 | #[test] | 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 | |||
290 | #[test] | ||
253 | fn test_method_completion() { | 291 | fn test_method_completion() { |
254 | assert_debug_snapshot!( | 292 | assert_debug_snapshot!( |
255 | do_ref_completion( | 293 | do_ref_completion( |