Skip to content

Commit

Permalink
fix: match completions for type aliases
Browse files Browse the repository at this point in the history
[Cherry-picked f973d0b]
  • Loading branch information
kasiaMarek authored and WojciechMazur committed Jun 19, 2024
1 parent a12fcfc commit 8ced7d8
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ object CaseKeywordCompletion:
new Parents(NoType, definitions)
case sel => new Parents(sel.tpe, definitions)

val selectorSym = parents.selector.typeSymbol
val selectorSym = parents.selector.widen.metalsDealias.typeSymbol

// Special handle case when selector is a tuple or `FunctionN`.
if definitions.isTupleClass(selectorSym) || definitions.isFunctionClass(
Expand Down Expand Up @@ -153,7 +153,9 @@ object CaseKeywordCompletion:
if isValid(ts) then visit(autoImportsGen.inferSymbolImport(ts))
)
// Step 2: walk through known subclasses of sealed types.
val sealedDescs = subclassesForType(parents.selector.widen.bounds.hi)
val sealedDescs = subclassesForType(
parents.selector.widen.metalsDealias.bounds.hi
)
sealedDescs.foreach { sym =>
val symbolImport = autoImportsGen.inferSymbolImport(sym)
visit(symbolImport)
Expand Down Expand Up @@ -241,7 +243,7 @@ object CaseKeywordCompletion:
completionPos,
clientSupportsSnippets
)
val tpe = selector.tpe.widen.bounds.hi match
val tpe = selector.tpe.widen.metalsDealias.bounds.hi match
case tr @ TypeRef(_, _) => tr.underlying
case t => t

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,44 @@ class CompletionCaseSuite extends BaseCompletionSuite:
|case Singing(song) => test.Activity
|case Sports(time, intensity) => test.Activity""".stripMargin
)

@Test def `type-alias-case` =
check(
s"""|object O:
| type Id[A] = A
|
| enum Animal:
| case Cat, Dog
|
| val animal: Id[Animal] = ???
|
| animal match
| cas@@
|""".stripMargin,
"""|case Animal.Cat =>
|case Animal.Dog =>
|""".stripMargin,
)

@Test def `type-alias-sealed-trait-case` =
check(
s"""|object O {
| type Id[A] = A
|
|sealed trait Animal
|object Animal {
| case class Cat() extends Animal
| case object Dog extends Animal
|}
|
| val animal: Id[Animal] = ???
|
| animal match {
| cas@@
| }
|}
|""".stripMargin,
"""|case Cat() => test.O.Animal
|case Dog => test.O.Animal
|""".stripMargin,
)
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,72 @@ class CompletionMatchSuite extends BaseCompletionSuite:
|}""".stripMargin,
filter = _.contains("exhaustive")
)

@Test def `type-alias` =
checkEdit(
s"""|object O {
| type Id[A] = A
|
| enum Animal:
| case Cat, Dog
|
| val animal: Id[Animal] = ???
|
| animal ma@@
|}
|""".stripMargin,
s"""object O {
| type Id[A] = A
|
| enum Animal:
| case Cat, Dog
|
| val animal: Id[Animal] = ???
|
| animal match
|\tcase Animal.Cat => $$0
|\tcase Animal.Dog =>
|
|}
|""".stripMargin,
filter = _.contains("exhaustive"),
)

@Test def `type-alias-sealed-trait` =
checkEdit(
s"""|object O {
| type Id[A] = A
|
|sealed trait Animal
|object Animal {
| case object Cat extends Animal
| case object Dog extends Animal
|}
|
| val animal: Id[Animal] = ???
|
|animal ma@@
|}
|""".stripMargin,
s"""|
|import O.Animal.Cat
|import O.Animal.Dog
|object O {
| type Id[A] = A
|
|sealed trait Animal
|object Animal {
| case object Cat extends Animal
| case object Dog extends Animal
|}
|
| val animal: Id[Animal] = ???
|
|animal match
|\tcase Cat => $$0
|\tcase Dog =>
|
|}
|""".stripMargin,
filter = _.contains("exhaustive"),
)

0 comments on commit 8ced7d8

Please sign in to comment.