aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2019-06-29 11:40:01 +0100
committerFlorian Diebold <[email protected]>2019-06-29 11:40:01 +0100
commitd37f960dfaac2272de3065ad586b25dc1cdb7dbd (patch)
tree64c33aff77b965c568819a54b9c888dd82621412 /crates
parent64f71dd3fff7b902656a2c2465a1b5071b2b1903 (diff)
Complete associated methods on enums (and unions) as well
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs4
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs55
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__enum_associated_method.snap19
-rw-r--r--crates/ra_ide_api/src/completion/snapshots/completion_item__union_associated_method.snap19
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---
2created: "2019-06-29T10:30:34.110468474Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/completion/completion_item.rs
5expression: 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---
2created: "2019-06-29T10:37:44.968500164Z"
3creator: [email protected]
4source: crates/ra_ide_api/src/completion/completion_item.rs
5expression: 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]