Skip to content

Commit 8ced7d8

Browse files
kasiaMarekWojciechMazur
authored andcommitted
fix: match completions for type aliases
[Cherry-picked f973d0b]
1 parent a12fcfc commit 8ced7d8

File tree

3 files changed

+115
-3
lines changed

3 files changed

+115
-3
lines changed

presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala

+5-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ object CaseKeywordCompletion:
9090
new Parents(NoType, definitions)
9191
case sel => new Parents(sel.tpe, definitions)
9292

93-
val selectorSym = parents.selector.typeSymbol
93+
val selectorSym = parents.selector.widen.metalsDealias.typeSymbol
9494

9595
// Special handle case when selector is a tuple or `FunctionN`.
9696
if definitions.isTupleClass(selectorSym) || definitions.isFunctionClass(
@@ -153,7 +153,9 @@ object CaseKeywordCompletion:
153153
if isValid(ts) then visit(autoImportsGen.inferSymbolImport(ts))
154154
)
155155
// Step 2: walk through known subclasses of sealed types.
156-
val sealedDescs = subclassesForType(parents.selector.widen.bounds.hi)
156+
val sealedDescs = subclassesForType(
157+
parents.selector.widen.metalsDealias.bounds.hi
158+
)
157159
sealedDescs.foreach { sym =>
158160
val symbolImport = autoImportsGen.inferSymbolImport(sym)
159161
visit(symbolImport)
@@ -241,7 +243,7 @@ object CaseKeywordCompletion:
241243
completionPos,
242244
clientSupportsSnippets
243245
)
244-
val tpe = selector.tpe.widen.bounds.hi match
246+
val tpe = selector.tpe.widen.metalsDealias.bounds.hi match
245247
case tr @ TypeRef(_, _) => tr.underlying
246248
case t => t
247249

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala

+41
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,44 @@ class CompletionCaseSuite extends BaseCompletionSuite:
657657
|case Singing(song) => test.Activity
658658
|case Sports(time, intensity) => test.Activity""".stripMargin
659659
)
660+
661+
@Test def `type-alias-case` =
662+
check(
663+
s"""|object O:
664+
| type Id[A] = A
665+
|
666+
| enum Animal:
667+
| case Cat, Dog
668+
|
669+
| val animal: Id[Animal] = ???
670+
|
671+
| animal match
672+
| cas@@
673+
|""".stripMargin,
674+
"""|case Animal.Cat =>
675+
|case Animal.Dog =>
676+
|""".stripMargin,
677+
)
678+
679+
@Test def `type-alias-sealed-trait-case` =
680+
check(
681+
s"""|object O {
682+
| type Id[A] = A
683+
|
684+
|sealed trait Animal
685+
|object Animal {
686+
| case class Cat() extends Animal
687+
| case object Dog extends Animal
688+
|}
689+
|
690+
| val animal: Id[Animal] = ???
691+
|
692+
| animal match {
693+
| cas@@
694+
| }
695+
|}
696+
|""".stripMargin,
697+
"""|case Cat() => test.O.Animal
698+
|case Dog => test.O.Animal
699+
|""".stripMargin,
700+
)

presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionMatchSuite.scala

+69
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,72 @@ class CompletionMatchSuite extends BaseCompletionSuite:
601601
|}""".stripMargin,
602602
filter = _.contains("exhaustive")
603603
)
604+
605+
@Test def `type-alias` =
606+
checkEdit(
607+
s"""|object O {
608+
| type Id[A] = A
609+
|
610+
| enum Animal:
611+
| case Cat, Dog
612+
|
613+
| val animal: Id[Animal] = ???
614+
|
615+
| animal ma@@
616+
|}
617+
|""".stripMargin,
618+
s"""object O {
619+
| type Id[A] = A
620+
|
621+
| enum Animal:
622+
| case Cat, Dog
623+
|
624+
| val animal: Id[Animal] = ???
625+
|
626+
| animal match
627+
|\tcase Animal.Cat => $$0
628+
|\tcase Animal.Dog =>
629+
|
630+
|}
631+
|""".stripMargin,
632+
filter = _.contains("exhaustive"),
633+
)
634+
635+
@Test def `type-alias-sealed-trait` =
636+
checkEdit(
637+
s"""|object O {
638+
| type Id[A] = A
639+
|
640+
|sealed trait Animal
641+
|object Animal {
642+
| case object Cat extends Animal
643+
| case object Dog extends Animal
644+
|}
645+
|
646+
| val animal: Id[Animal] = ???
647+
|
648+
|animal ma@@
649+
|}
650+
|""".stripMargin,
651+
s"""|
652+
|import O.Animal.Cat
653+
|import O.Animal.Dog
654+
|object O {
655+
| type Id[A] = A
656+
|
657+
|sealed trait Animal
658+
|object Animal {
659+
| case object Cat extends Animal
660+
| case object Dog extends Animal
661+
|}
662+
|
663+
| val animal: Id[Animal] = ???
664+
|
665+
|animal match
666+
|\tcase Cat => $$0
667+
|\tcase Dog =>
668+
|
669+
|}
670+
|""".stripMargin,
671+
filter = _.contains("exhaustive"),
672+
)

0 commit comments

Comments
 (0)