diff options
author | Florian Diebold <[email protected]> | 2019-01-07 18:12:19 +0000 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-01-12 14:01:27 +0000 |
commit | d0bdaa6c00faa503db58f2a7987d1252086b4441 (patch) | |
tree | c304f66a307b6b5e9227f048ac5d5a4adf4f47a1 /crates/ra_ide_api/src | |
parent | 082ef52bcb15d779c6aff78d9860d328bf7df9b2 (diff) |
Complete inherent methods
Diffstat (limited to 'crates/ra_ide_api/src')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_dot.rs | 59 | ||||
-rw-r--r-- | crates/ra_ide_api/src/completion/completion_item.rs | 7 |
2 files changed, 62 insertions, 4 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..9b01eb0ab 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_arg() { | ||
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 | } |
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index e7fa967a0..9ce778487 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs | |||
@@ -37,6 +37,7 @@ pub enum CompletionItemKind { | |||
37 | Const, | 37 | Const, |
38 | Trait, | 38 | Trait, |
39 | TypeAlias, | 39 | TypeAlias, |
40 | Method, | ||
40 | } | 41 | } |
41 | 42 | ||
42 | #[derive(Debug, PartialEq, Eq)] | 43 | #[derive(Debug, PartialEq, Eq)] |
@@ -183,7 +184,11 @@ impl Builder { | |||
183 | self | 184 | self |
184 | } | 185 | } |
185 | 186 | ||
186 | fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder { | 187 | pub(super) fn from_function( |
188 | mut self, | ||
189 | ctx: &CompletionContext, | ||
190 | function: hir::Function, | ||
191 | ) -> Builder { | ||
187 | // If not an import, add parenthesis automatically. | 192 | // If not an import, add parenthesis automatically. |
188 | if ctx.use_item_syntax.is_none() && !ctx.is_call { | 193 | if ctx.use_item_syntax.is_none() && !ctx.is_call { |
189 | if function.signature(ctx.db).args().is_empty() { | 194 | if function.signature(ctx.db).args().is_empty() { |