Skip to content

Commit 2bfcfb0

Browse files
bors[bot]flodiebold
andcommitted
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]>
2 parents 8865db6 + d37f960 commit 2bfcfb0

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ impl Union {
390390
self.id.module(db)
391391
}
392392

393+
pub fn ty(self, db: &impl HirDatabase) -> Ty {
394+
db.type_for_def(self.into(), Namespace::Types)
395+
}
396+
393397
// FIXME move to a more general type
394398
/// Builds a resolver for type references inside this union.
395399
pub(crate) fn resolver(self, db: &impl HirDatabase) -> Resolver {

crates/ra_ide_api/src/completion/complete_path.rs

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
3737
acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
3838
}
3939
}
40-
hir::ModuleDef::Enum(e) => {
41-
for variant in e.variants(ctx.db) {
42-
acc.add_enum_variant(ctx, variant);
40+
hir::ModuleDef::Enum(_) | hir::ModuleDef::Struct(_) | hir::ModuleDef::Union(_) => {
41+
if let hir::ModuleDef::Enum(e) = def {
42+
for variant in e.variants(ctx.db) {
43+
acc.add_enum_variant(ctx, variant);
44+
}
4345
}
44-
}
45-
hir::ModuleDef::Struct(s) => {
46-
let ty = s.ty(ctx.db);
46+
let ty = match def {
47+
hir::ModuleDef::Enum(e) => e.ty(ctx.db),
48+
hir::ModuleDef::Struct(s) => s.ty(ctx.db),
49+
hir::ModuleDef::Union(u) => u.ty(ctx.db),
50+
_ => unreachable!(),
51+
};
4752
let krate = ctx.module.and_then(|m| m.krate(ctx.db));
4853
if let Some(krate) = krate {
4954
ty.iterate_impl_items(ctx.db, krate, |item| {
@@ -280,6 +285,44 @@ mod tests {
280285
);
281286
}
282287

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+
283326
#[test]
284327
fn completes_use_paths_across_crates() {
285328
check_reference_completion(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
created: "2019-06-29T10:30:34.110468474Z"
3+
creator: insta@0.8.1
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+
]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
created: "2019-06-29T10:37:44.968500164Z"
3+
creator: insta@0.8.1
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+
]

0 commit comments

Comments
 (0)