Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport "fix: match completions for type aliases" to LTS #20696

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
)
Loading