aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_ide/src/completion/complete_unqualified_path.rs53
1 files changed, 52 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs
index 638f86eda..8e3dcf96e 100644
--- a/crates/ra_ide/src/completion/complete_unqualified_path.rs
+++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs
@@ -38,7 +38,15 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext) {
38 if let Some(ty) = ctx.expected_type_of(&ctx.token.parent()) { 38 if let Some(ty) = ctx.expected_type_of(&ctx.token.parent()) {
39 if let Some(Adt::Enum(enum_data)) = ty.as_adt() { 39 if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
40 let variants = enum_data.variants(ctx.db); 40 let variants = enum_data.variants(ctx.db);
41 let module = enum_data.module(ctx.db); 41
42 let module = if let Some(module) = ctx.scope().module() {
43 // Compute path from the completion site if available.
44 module
45 } else {
46 // Otherwise fall back to the enum's definition site.
47 enum_data.module(ctx.db)
48 };
49
42 for variant in variants { 50 for variant in variants {
43 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) { 51 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
44 // Variants with trivial paths are already added by the existing completion logic, 52 // Variants with trivial paths are already added by the existing completion logic,
@@ -1308,4 +1316,47 @@ mod tests {
1308 "### 1316 "###
1309 ) 1317 )
1310 } 1318 }
1319
1320 #[test]
1321 fn completes_enum_variant_from_module() {
1322 assert_debug_snapshot!(
1323 do_reference_completion(
1324 r"
1325 mod m { pub enum E { V } }
1326
1327 fn f() -> m::E {
1328 V<|>
1329 }
1330 "
1331 ),
1332 @r###"
1333 [
1334 CompletionItem {
1335 label: "f()",
1336 source_range: [98; 99),
1337 delete: [98; 99),
1338 insert: "f()$0",
1339 kind: Function,
1340 lookup: "f",
1341 detail: "fn f() -> m::E",
1342 },
1343 CompletionItem {
1344 label: "m",
1345 source_range: [98; 99),
1346 delete: [98; 99),
1347 insert: "m",
1348 kind: Module,
1349 },
1350 CompletionItem {
1351 label: "m::E::V",
1352 source_range: [98; 99),
1353 delete: [98; 99),
1354 insert: "m::E::V",
1355 kind: EnumVariant,
1356 detail: "()",
1357 },
1358 ]
1359 "###
1360 )
1361 }
1311} 1362}