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

bugfix: match completions for type aliases scala 3 #5714

Merged
merged 2 commits into from
Oct 11, 2023
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 @@ -39,7 +39,6 @@ object CaseKeywordCompletion:
* @param selector `selector` of `selector match { cases }` or `EmptyTree` when
* not in a match expression (for example `List(1).foreach { case@@ }`.
* @param completionPos the position of the completion
* @param typedtree typed tree of the file, used for generating auto imports
* @param indexedContext
* @param config
* @param parent the parent tree node of the pattern match, for example `Apply(_, _)` when in
Expand Down Expand Up @@ -88,7 +87,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 @@ -151,7 +150,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 @@ -239,7 +240,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
47 changes: 47 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionCaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,51 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|case Sports(time, intensity) => exhaustive-enum-tags3.Activity""".stripMargin,
)

check(
"type-alias-case".tag(IgnoreScala2),
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,
)

check(
"type-alias-sealed-trait-case",
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() => `type-alias-sealed-trait-case`.O.Animal
|case Dog => `type-alias-sealed-trait-case`.O.Animal
|""".stripMargin,
compat = Map(
"3" ->
"""|case Cat() => type-alias-sealed-trait-case.O.Animal
|case Dog => type-alias-sealed-trait-case.O.Animal
|""".stripMargin
),
)

}
92 changes: 92 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionMatchSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -757,4 +757,96 @@ class CompletionMatchSuite extends BaseCompletionSuite {
|""".stripMargin,
)

checkEdit(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cases already seem to work for Scala 2 (with Animal being sealed type hierarchy instead of enum). Would it be worth it to add unit tests for it as well (or maybe there are already some)? 🙂

"type-alias".tag(IgnoreScala2),
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"),
)

checkEdit(
"type-alias-sealed-trait",
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,
compat = Map(
"3" ->
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