From 8ced7d8680b188ed191869bd0df0d84d12f443c1 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Mon, 9 Oct 2023 11:28:41 +0200 Subject: [PATCH] fix: match completions for type aliases [Cherry-picked f973d0ba64dd774646a17ae3156f59a3e4e268dc] --- .../pc/completions/MatchCaseCompletions.scala | 8 ++- .../completion/CompletionCaseSuite.scala | 41 +++++++++++ .../completion/CompletionMatchSuite.scala | 69 +++++++++++++++++++ 3 files changed, 115 insertions(+), 3 deletions(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala index 0949d050a59c..8e7fa821d95d 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala @@ -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( @@ -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) @@ -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 diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala index 80a9e843e746..37df23972813 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala @@ -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, + ) diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionMatchSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionMatchSuite.scala index 264e6f56c586..142667171c75 100644 --- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionMatchSuite.scala +++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionMatchSuite.scala @@ -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"), + )