From 7bb279b365e54ee0051e09ead5aa157ff6be917b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 6 Jan 2019 19:51:42 +0100 Subject: Implement autoderef for field accesses --- crates/ra_analysis/src/completion/complete_dot.rs | 57 +++++++++++++++-------- 1 file changed, 38 insertions(+), 19 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion/complete_dot.rs b/crates/ra_analysis/src/completion/complete_dot.rs index 54ce1b638..5d4e60dc5 100644 --- a/crates/ra_analysis/src/completion/complete_dot.rs +++ b/crates/ra_analysis/src/completion/complete_dot.rs @@ -23,31 +23,35 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca } fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> { - // TODO: autoderef etc. - match receiver { - Ty::Adt { def_id, .. } => { - match def_id.resolve(ctx.db)? { - Def::Struct(s) => { - let variant_data = s.variant_data(ctx.db)?; - for field in variant_data.fields() { - CompletionItem::new(CompletionKind::Reference, field.name().to_string()) + for receiver in receiver.autoderef(ctx.db) { + match receiver { + Ty::Adt { def_id, .. } => { + match def_id.resolve(ctx.db)? { + Def::Struct(s) => { + let variant_data = s.variant_data(ctx.db)?; + for field in variant_data.fields() { + CompletionItem::new( + CompletionKind::Reference, + field.name().to_string(), + ) .kind(CompletionItemKind::Field) .add_to(acc); + } } + // TODO unions + _ => {} } - // TODO unions - _ => {} } - } - Ty::Tuple(fields) => { - for (i, _ty) in fields.iter().enumerate() { - CompletionItem::new(CompletionKind::Reference, i.to_string()) - .kind(CompletionItemKind::Field) - .add_to(acc); + Ty::Tuple(fields) => { + for (i, _ty) in fields.iter().enumerate() { + CompletionItem::new(CompletionKind::Reference, i.to_string()) + .kind(CompletionItemKind::Field) + .add_to(acc); + } } - } - _ => {} - }; + _ => {} + }; + } Ok(()) } @@ -87,6 +91,21 @@ mod tests { ); } + #[test] + fn test_struct_field_completion_autoderef() { + check_ref_completion( + r" + struct A { the_field: u32 } + impl A { + fn foo(&self) { + self.<|> + } + } + ", + r#"the_field"#, + ); + } + #[test] fn test_no_struct_field_completion_for_method_call() { check_ref_completion( -- cgit v1.2.3