@@ -38,7 +38,15 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext) {
38
38
if let Some ( ty) = ctx. expected_type_of ( & ctx. token . parent ( ) ) {
39
39
if let Some ( Adt :: Enum ( enum_data) ) = ty. as_adt ( ) {
40
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
50
for variant in variants {
43
51
if let Some ( path) = module. find_use_path ( ctx. db , ModuleDef :: from ( variant) ) {
44
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
}
0 commit comments