diff options
author | Kevin DeLorey <[email protected]> | 2020-02-13 02:21:43 +0000 |
---|---|---|
committer | Kevin DeLorey <[email protected]> | 2020-02-13 02:21:43 +0000 |
commit | 877cfbacf98d9d803aa5e0f54be560c7da7acbca (patch) | |
tree | f742916f028079c0a2a60134448d597cbabd65ff | |
parent | 6f130e7ef8fea374bbcfb2c9c60009f558ce3ecf (diff) |
Started to refactor the trigger of the trait_impl completion.
-rw-r--r-- | crates/ra_ide/src/completion/complete_trait_impl.rs | 110 |
1 files changed, 77 insertions, 33 deletions
diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index c8f26c954..b1017e57a 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs | |||
@@ -16,19 +16,63 @@ use ra_syntax::{ | |||
16 | use ra_assists::utils::get_missing_impl_items; | 16 | use ra_assists::utils::get_missing_impl_items; |
17 | 17 | ||
18 | pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { | 18 | pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { |
19 | // it is possible to have a parent `fn` and `impl` block. Ignore completion | 19 | let trigger = ctx.token |
20 | // attempts from within a `fn` block. | 20 | .ancestors() |
21 | if ctx.function_syntax.is_some() { | 21 | .find(|p| match p.kind() { |
22 | return; | 22 | SyntaxKind::FN_DEF | |
23 | } | 23 | SyntaxKind::TYPE_ALIAS_DEF | |
24 | SyntaxKind::CONST_DEF | | ||
25 | SyntaxKind::ITEM_LIST => true, | ||
26 | _ => false | ||
27 | }); | ||
28 | |||
29 | let impl_block = trigger | ||
30 | .as_ref() | ||
31 | .and_then(|node| node.parent()) | ||
32 | .and_then(|node| node.parent()) | ||
33 | .and_then(|node| ast::ImplBlock::cast(node)); | ||
34 | |||
35 | if let (Some(trigger), Some(impl_block)) = (trigger, impl_block) { | ||
36 | match trigger.kind() { | ||
37 | SyntaxKind::FN_DEF => { | ||
38 | for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block) | ||
39 | .iter() | ||
40 | .filter_map(|item| { | ||
41 | match item { | ||
42 | hir::AssocItem::Function(fn_item) => Some(fn_item), | ||
43 | _ => None | ||
44 | } | ||
45 | }) | ||
46 | { | ||
47 | add_function_impl(acc, ctx, &missing_fn); | ||
48 | } | ||
49 | }, | ||
24 | 50 | ||
25 | if let Some(ref impl_block) = ctx.impl_block { | 51 | SyntaxKind::TYPE_ALIAS_DEF => { |
26 | for item in get_missing_impl_items(ctx.db, &ctx.analyzer, impl_block) { | 52 | for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block) |
27 | match item { | 53 | .iter() |
28 | hir::AssocItem::Function(f) => add_function_impl(acc, ctx, &f), | 54 | .filter_map(|item| match item { |
29 | hir::AssocItem::TypeAlias(t) => add_type_alias_impl(acc, ctx, &t), | 55 | hir::AssocItem::TypeAlias(type_item) => Some(type_item), |
30 | hir::AssocItem::Const(c) => add_const_impl(acc, ctx, &c), | 56 | _ => None |
31 | } | 57 | }) |
58 | { | ||
59 | add_type_alias_impl(acc, ctx, &missing_fn); | ||
60 | } | ||
61 | }, | ||
62 | |||
63 | SyntaxKind::CONST_DEF => { | ||
64 | for missing_fn in get_missing_impl_items(ctx.db, &ctx.analyzer, &impl_block) | ||
65 | .iter() | ||
66 | .filter_map(|item| match item { | ||
67 | hir::AssocItem::Const(const_item) => Some(const_item), | ||
68 | _ => None | ||
69 | }) | ||
70 | { | ||
71 | add_const_impl(acc, ctx, &missing_fn); | ||
72 | } | ||
73 | }, | ||
74 | |||
75 | _ => {} | ||
32 | } | 76 | } |
33 | } | 77 | } |
34 | } | 78 | } |
@@ -126,7 +170,7 @@ mod tests { | |||
126 | struct T1; | 170 | struct T1; |
127 | 171 | ||
128 | impl Test for T1 { | 172 | impl Test for T1 { |
129 | <|> | 173 | fn<|> |
130 | } | 174 | } |
131 | ", | 175 | ", |
132 | ); | 176 | ); |
@@ -134,8 +178,8 @@ mod tests { | |||
134 | [ | 178 | [ |
135 | CompletionItem { | 179 | CompletionItem { |
136 | label: "fn foo()", | 180 | label: "fn foo()", |
137 | source_range: [138; 138), | 181 | source_range: [140; 140), |
138 | delete: [138; 138), | 182 | delete: [140; 140), |
139 | insert: "fn foo() {}", | 183 | insert: "fn foo() {}", |
140 | kind: Function, | 184 | kind: Function, |
141 | }, | 185 | }, |
@@ -157,7 +201,7 @@ mod tests { | |||
157 | impl Test for T1 { | 201 | impl Test for T1 { |
158 | fn foo() {} | 202 | fn foo() {} |
159 | 203 | ||
160 | <|> | 204 | fn<|> |
161 | } | 205 | } |
162 | ", | 206 | ", |
163 | ); | 207 | ); |
@@ -165,8 +209,8 @@ mod tests { | |||
165 | [ | 209 | [ |
166 | CompletionItem { | 210 | CompletionItem { |
167 | label: "fn bar()", | 211 | label: "fn bar()", |
168 | source_range: [193; 193), | 212 | source_range: [195; 195), |
169 | delete: [193; 193), | 213 | delete: [195; 195), |
170 | insert: "fn bar() {}", | 214 | insert: "fn bar() {}", |
171 | kind: Function, | 215 | kind: Function, |
172 | }, | 216 | }, |
@@ -185,7 +229,7 @@ mod tests { | |||
185 | struct T1; | 229 | struct T1; |
186 | 230 | ||
187 | impl Test for T1 { | 231 | impl Test for T1 { |
188 | <|> | 232 | fn<|> |
189 | } | 233 | } |
190 | ", | 234 | ", |
191 | ); | 235 | ); |
@@ -193,8 +237,8 @@ mod tests { | |||
193 | [ | 237 | [ |
194 | CompletionItem { | 238 | CompletionItem { |
195 | label: "fn foo()", | 239 | label: "fn foo()", |
196 | source_range: [141; 141), | 240 | source_range: [143; 143), |
197 | delete: [141; 141), | 241 | delete: [143; 143), |
198 | insert: "fn foo<T>() {}", | 242 | insert: "fn foo<T>() {}", |
199 | kind: Function, | 243 | kind: Function, |
200 | }, | 244 | }, |
@@ -213,7 +257,7 @@ mod tests { | |||
213 | struct T1; | 257 | struct T1; |
214 | 258 | ||
215 | impl Test for T1 { | 259 | impl Test for T1 { |
216 | <|> | 260 | fn<|> |
217 | } | 261 | } |
218 | ", | 262 | ", |
219 | ); | 263 | ); |
@@ -221,8 +265,8 @@ mod tests { | |||
221 | [ | 265 | [ |
222 | CompletionItem { | 266 | CompletionItem { |
223 | label: "fn foo()", | 267 | label: "fn foo()", |
224 | source_range: [163; 163), | 268 | source_range: [165; 165), |
225 | delete: [163; 163), | 269 | delete: [165; 165), |
226 | insert: "fn foo<T>()\nwhere T: Into<String> {}", | 270 | insert: "fn foo<T>()\nwhere T: Into<String> {}", |
227 | kind: Function, | 271 | kind: Function, |
228 | }, | 272 | }, |
@@ -239,7 +283,7 @@ mod tests { | |||
239 | } | 283 | } |
240 | 284 | ||
241 | impl Test for () { | 285 | impl Test for () { |
242 | <|> | 286 | type<|> |
243 | } | 287 | } |
244 | ", | 288 | ", |
245 | ); | 289 | ); |
@@ -247,8 +291,8 @@ mod tests { | |||
247 | [ | 291 | [ |
248 | CompletionItem { | 292 | CompletionItem { |
249 | label: "type SomeType = ", | 293 | label: "type SomeType = ", |
250 | source_range: [119; 119), | 294 | source_range: [123; 123), |
251 | delete: [119; 119), | 295 | delete: [123; 123), |
252 | insert: "type SomeType = ", | 296 | insert: "type SomeType = ", |
253 | kind: TypeAlias, | 297 | kind: TypeAlias, |
254 | }, | 298 | }, |
@@ -265,7 +309,7 @@ mod tests { | |||
265 | } | 309 | } |
266 | 310 | ||
267 | impl Test for () { | 311 | impl Test for () { |
268 | <|> | 312 | const<|> |
269 | } | 313 | } |
270 | ", | 314 | ", |
271 | ); | 315 | ); |
@@ -273,8 +317,8 @@ mod tests { | |||
273 | [ | 317 | [ |
274 | CompletionItem { | 318 | CompletionItem { |
275 | label: "const SOME_CONST: u16 = ", | 319 | label: "const SOME_CONST: u16 = ", |
276 | source_range: [127; 127), | 320 | source_range: [132; 132), |
277 | delete: [127; 127), | 321 | delete: [132; 132), |
278 | insert: "const SOME_CONST: u16 = ", | 322 | insert: "const SOME_CONST: u16 = ", |
279 | kind: Const, | 323 | kind: Const, |
280 | }, | 324 | }, |
@@ -291,7 +335,7 @@ mod tests { | |||
291 | } | 335 | } |
292 | 336 | ||
293 | impl Test for () { | 337 | impl Test for () { |
294 | <|> | 338 | const<|> |
295 | } | 339 | } |
296 | ", | 340 | ", |
297 | ); | 341 | ); |
@@ -299,8 +343,8 @@ mod tests { | |||
299 | [ | 343 | [ |
300 | CompletionItem { | 344 | CompletionItem { |
301 | label: "const SOME_CONST: u16 = ", | 345 | label: "const SOME_CONST: u16 = ", |
302 | source_range: [132; 132), | 346 | source_range: [137; 137), |
303 | delete: [132; 132), | 347 | delete: [137; 137), |
304 | insert: "const SOME_CONST: u16 = ", | 348 | insert: "const SOME_CONST: u16 = ", |
305 | kind: Const, | 349 | kind: Const, |
306 | }, | 350 | }, |