aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-07 14:04:30 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-07 14:04:30 +0000
commite2592cf09087ae0a6cad5b588cbf1ab1161440e9 (patch)
tree7a495d68453ea1e3c7a726e97bbbe19f9bc90532 /crates/ra_analysis/src/completion
parenta6071c9f4c8441b4b8f2e970bc055d66cc9be5f0 (diff)
parent7bb279b365e54ee0051e09ead5aa157ff6be917b (diff)
Merge #450
450: Implement autoderef for field accesses r=matklad a=flodiebold Which means we now get completion for fields e.g. in `&self` methods :) Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/completion')
-rw-r--r--crates/ra_analysis/src/completion/complete_dot.rs57
1 files changed, 38 insertions, 19 deletions
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
23} 23}
24 24
25fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> { 25fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) -> Cancelable<()> {
26 // TODO: autoderef etc. 26 for receiver in receiver.autoderef(ctx.db) {
27 match receiver { 27 match receiver {
28 Ty::Adt { def_id, .. } => { 28 Ty::Adt { def_id, .. } => {
29 match def_id.resolve(ctx.db)? { 29 match def_id.resolve(ctx.db)? {
30 Def::Struct(s) => { 30 Def::Struct(s) => {
31 let variant_data = s.variant_data(ctx.db)?; 31 let variant_data = s.variant_data(ctx.db)?;
32 for field in variant_data.fields() { 32 for field in variant_data.fields() {
33 CompletionItem::new(CompletionKind::Reference, field.name().to_string()) 33 CompletionItem::new(
34 CompletionKind::Reference,
35 field.name().to_string(),
36 )
34 .kind(CompletionItemKind::Field) 37 .kind(CompletionItemKind::Field)
35 .add_to(acc); 38 .add_to(acc);
39 }
36 } 40 }
41 // TODO unions
42 _ => {}
37 } 43 }
38 // TODO unions
39 _ => {}
40 } 44 }
41 } 45 Ty::Tuple(fields) => {
42 Ty::Tuple(fields) => { 46 for (i, _ty) in fields.iter().enumerate() {
43 for (i, _ty) in fields.iter().enumerate() { 47 CompletionItem::new(CompletionKind::Reference, i.to_string())
44 CompletionItem::new(CompletionKind::Reference, i.to_string()) 48 .kind(CompletionItemKind::Field)
45 .kind(CompletionItemKind::Field) 49 .add_to(acc);
46 .add_to(acc); 50 }
47 } 51 }
48 } 52 _ => {}
49 _ => {} 53 };
50 }; 54 }
51 Ok(()) 55 Ok(())
52} 56}
53 57
@@ -88,6 +92,21 @@ mod tests {
88 } 92 }
89 93
90 #[test] 94 #[test]
95 fn test_struct_field_completion_autoderef() {
96 check_ref_completion(
97 r"
98 struct A { the_field: u32 }
99 impl A {
100 fn foo(&self) {
101 self.<|>
102 }
103 }
104 ",
105 r#"the_field"#,
106 );
107 }
108
109 #[test]
91 fn test_no_struct_field_completion_for_method_call() { 110 fn test_no_struct_field_completion_for_method_call() {
92 check_ref_completion( 111 check_ref_completion(
93 r" 112 r"