diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_dot.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_dot.rs | 59 |
1 files changed, 56 insertions, 3 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 80d0b1663..37985b398 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs | |||
@@ -17,8 +17,9 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) -> Ca | |||
17 | }; | 17 | }; |
18 | let receiver_ty = infer_result[expr].clone(); | 18 | let receiver_ty = infer_result[expr].clone(); |
19 | if !ctx.is_call { | 19 | if !ctx.is_call { |
20 | complete_fields(acc, ctx, receiver_ty)?; | 20 | complete_fields(acc, ctx, receiver_ty.clone())?; |
21 | } | 21 | } |
22 | complete_methods(acc, ctx, receiver_ty)?; | ||
22 | Ok(()) | 23 | Ok(()) |
23 | } | 24 | } |
24 | 25 | ||
@@ -55,6 +56,24 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) | |||
55 | Ok(()) | 56 | Ok(()) |
56 | } | 57 | } |
57 | 58 | ||
59 | fn complete_methods( | ||
60 | acc: &mut Completions, | ||
61 | ctx: &CompletionContext, | ||
62 | receiver: Ty, | ||
63 | ) -> Cancelable<()> { | ||
64 | receiver.iterate_methods(ctx.db, |func| { | ||
65 | let sig = func.signature(ctx.db); | ||
66 | if sig.has_self_param() { | ||
67 | CompletionItem::new(CompletionKind::Reference, sig.name().to_string()) | ||
68 | .from_function(ctx, func) | ||
69 | .kind(CompletionItemKind::Method) | ||
70 | .add_to(acc); | ||
71 | } | ||
72 | Ok(None::<()>) | ||
73 | })?; | ||
74 | Ok(()) | ||
75 | } | ||
76 | |||
58 | #[cfg(test)] | 77 | #[cfg(test)] |
59 | mod tests { | 78 | mod tests { |
60 | use crate::completion::*; | 79 | use crate::completion::*; |
@@ -87,7 +106,8 @@ mod tests { | |||
87 | } | 106 | } |
88 | } | 107 | } |
89 | ", | 108 | ", |
90 | r#"the_field "(u32,)""#, | 109 | r#"the_field "(u32,)" |
110 | foo "foo($0)""#, | ||
91 | ); | 111 | ); |
92 | } | 112 | } |
93 | 113 | ||
@@ -102,7 +122,8 @@ mod tests { | |||
102 | } | 122 | } |
103 | } | 123 | } |
104 | ", | 124 | ", |
105 | r#"the_field "(u32, i32)""#, | 125 | r#"the_field "(u32, i32)" |
126 | foo "foo($0)""#, | ||
106 | ); | 127 | ); |
107 | } | 128 | } |
108 | 129 | ||
@@ -118,4 +139,36 @@ mod tests { | |||
118 | r#""#, | 139 | r#""#, |
119 | ); | 140 | ); |
120 | } | 141 | } |
142 | |||
143 | #[test] | ||
144 | fn test_method_completion() { | ||
145 | check_ref_completion( | ||
146 | r" | ||
147 | struct A {} | ||
148 | impl A { | ||
149 | fn the_method(&self) {} | ||
150 | } | ||
151 | fn foo(a: A) { | ||
152 | a.<|> | ||
153 | } | ||
154 | ", | ||
155 | r#"the_method "the_method($0)""#, | ||
156 | ); | ||
157 | } | ||
158 | |||
159 | #[test] | ||
160 | fn test_no_non_self_method() { | ||
161 | check_ref_completion( | ||
162 | r" | ||
163 | struct A {} | ||
164 | impl A { | ||
165 | fn the_method() {} | ||
166 | } | ||
167 | fn foo(a: A) { | ||
168 | a.<|> | ||
169 | } | ||
170 | ", | ||
171 | r#""#, | ||
172 | ); | ||
173 | } | ||
121 | } | 174 | } |