diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-06-29 12:01:36 +0100 |
---|---|---|
committer | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-06-29 12:01:36 +0100 |
commit | 2bfcfb0b0e5e518e8fc2779c6564ed1009d3f920 (patch) | |
tree | 97b1732bc0bcfb1aee6ffa6496d5f70be264ef95 | |
parent | 8865db6768fd913b971df56fc0bcf7439fcb1e71 (diff) | |
parent | d37f960dfaac2272de3065ad586b25dc1cdb7dbd (diff) |
Merge #1457
1457: Complete associated methods on enums (and unions) as well r=matklad a=flodiebold
This has been seriously annoying me for a while ;)
Co-authored-by: Florian Diebold <[email protected]>
4 files changed, 91 insertions, 6 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index ebbc37c0e..32f98e394 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -390,6 +390,10 @@ impl Union { | |||
390 | self.id.module(db) | 390 | self.id.module(db) |
391 | } | 391 | } |
392 | 392 | ||
393 | pub fn ty(self, db: &impl HirDatabase) -> Ty { | ||
394 | db.type_for_def(self.into(), Namespace::Types) | ||
395 | } | ||
396 | |||
393 | // FIXME move to a more general type | 397 | // FIXME move to a more general type |
394 | /// Builds a resolver for type references inside this union. | 398 | /// Builds a resolver for type references inside this union. |
395 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { | 399 | pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver { |
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index c14af593b..b42b7c458 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs | |||
@@ -37,13 +37,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | |||
37 | acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); | 37 | acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def)); |
38 | } | 38 | } |
39 | } | 39 | } |
40 | hir::ModuleDef::Enum(e) => { | 40 | hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => { |
41 | for variant in e.variants(ctx.db) { | 41 | if let hir::ModuleDef::Enum(e) = def { |
42 | acc.add_enum_variant(ctx, variant); | 42 | for variant in e.variants(ctx.db) { |
43 | acc.add_enum_variant(ctx, variant); | ||
44 | } | ||
43 | } | 45 | } |
44 | } | 46 | let ty = match def { |
45 | hir::ModuleDef::Struct(s) => { | 47 | hir::ModuleDef::Enum(e) => e.ty(ctx.db), |
46 | let ty = s.ty(ctx.db); | 48 | hir::ModuleDef::Struct(s) => s.ty(ctx.db), |
49 | hir::ModuleDef::Union(u) => u.ty(ctx.db), | ||
50 | _ => unreachable!(), | ||
51 | }; | ||
47 | let krate = ctx.module.and_then(|m| m.krate(ctx.db)); | 52 | let krate = ctx.module.and_then(|m| m.krate(ctx.db)); |
48 | if let Some(krate) = krate { | 53 | if let Some(krate) = krate { |
49 | ty.iterate_impl_items(ctx.db, krate, |item| { | 54 | ty.iterate_impl_items(ctx.db, krate, |item| { |
@@ -281,6 +286,44 @@ mod tests { | |||
281 | } | 286 | } |
282 | 287 | ||
283 | #[test] | 288 | #[test] |
289 | fn completes_enum_associated_method() { | ||
290 | check_reference_completion( | ||
291 | "enum_associated_method", | ||
292 | " | ||
293 | //- /lib.rs | ||
294 | /// An enum | ||
295 | enum S {}; | ||
296 | |||
297 | impl S { | ||
298 | /// An associated method | ||
299 | fn m() { } | ||
300 | } | ||
301 | |||
302 | fn foo() { let _ = S::<|> } | ||
303 | ", | ||
304 | ); | ||
305 | } | ||
306 | |||
307 | #[test] | ||
308 | fn completes_union_associated_method() { | ||
309 | check_reference_completion( | ||
310 | "union_associated_method", | ||
311 | " | ||
312 | //- /lib.rs | ||
313 | /// A union | ||
314 | union U {}; | ||
315 | |||
316 | impl U { | ||
317 | /// An associated method | ||
318 | fn m() { } | ||
319 | } | ||
320 | |||
321 | fn foo() { let _ = U::<|> } | ||
322 | ", | ||
323 | ); | ||
324 | } | ||
325 | |||
326 | #[test] | ||
284 | fn completes_use_paths_across_crates() { | 327 | fn completes_use_paths_across_crates() { |
285 | check_reference_completion( | 328 | check_reference_completion( |
286 | "completes_use_paths_across_crates", | 329 | "completes_use_paths_across_crates", |
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap new file mode 100644 index 000000000..ee6518fc8 --- /dev/null +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap | |||
@@ -0,0 +1,19 @@ | |||
1 | --- | ||
2 | created: "2019-06-29T10:30:34.110468474Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "m", | ||
10 | source_range: [100; 100), | ||
11 | delete: [100; 100), | ||
12 | insert: "m()$0", | ||
13 | kind: Function, | ||
14 | detail: "fn m()", | ||
15 | documentation: Documentation( | ||
16 | "An associated method", | ||
17 | ), | ||
18 | }, | ||
19 | ] | ||
diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap new file mode 100644 index 000000000..1c1a250f4 --- /dev/null +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap | |||
@@ -0,0 +1,19 @@ | |||
1 | --- | ||
2 | created: "2019-06-29T10:37:44.968500164Z" | ||
3 | creator: [email protected] | ||
4 | source: crates/ra_ide_api/src/completion/completion_item.rs | ||
5 | expression: kind_completions | ||
6 | --- | ||
7 | [ | ||
8 | CompletionItem { | ||
9 | label: "m", | ||
10 | source_range: [101; 101), | ||
11 | delete: [101; 101), | ||
12 | insert: "m()$0", | ||
13 | kind: Function, | ||
14 | detail: "fn m()", | ||
15 | documentation: Documentation( | ||
16 | "An associated method", | ||
17 | ), | ||
18 | }, | ||
19 | ] | ||