Skip to content

Commit e131763

Browse files
Include correct item path for variant completions
1 parent 5d97667 commit e131763

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

crates/ra_ide/src/completion/complete_unqualified_path.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext) {
3838
if let Some(ty) = ctx.expected_type_of(&ctx.token.parent()) {
3939
if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
4040
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+
4250
for variant in variants {
4351
if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
4452
// Variants with trivial paths are already added by the existing completion logic,
@@ -1308,4 +1316,47 @@ mod tests {
13081316
"###
13091317
)
13101318
}
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+
}
13111362
}

0 commit comments

Comments
 (0)