Skip to content

Commit 433a897

Browse files
committed
Add pattern completion based on the Unapply argument type
The idea was to collect patterns given the Unapply tree as the parent. We need to deconstruct the UnApply type tree and get the type of the placeholder ident.
1 parent f6bf36d commit 433a897

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,22 @@ class Completions(
405405
true,
406406
)
407407

408+
// unapply pattern
409+
case Ident(name) :: (unapp : UnApply) :: _ =>
410+
(
411+
CaseKeywordCompletion.contribute(
412+
EmptyTree, // no selector
413+
completionPos,
414+
indexedContext,
415+
config,
416+
search,
417+
parent = unapp,
418+
autoImports,
419+
patternOnly = Some(name.decoded)
420+
),
421+
false,
422+
)
423+
408424
// class FooImpl extends Foo:
409425
// def x|
410426
case OverrideExtractor(td, completing, start, exhaustive, fallbackName) =>

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import dotty.tools.dotc.core.Types.NoType
2727
import dotty.tools.dotc.core.Types.OrType
2828
import dotty.tools.dotc.core.Types.Type
2929
import dotty.tools.dotc.core.Types.TypeRef
30+
import dotty.tools.dotc.core.Types.AppliedType
31+
import dotty.tools.dotc.typer.Applications.UnapplyArgs
3032
import dotty.tools.dotc.util.SourcePosition
3133
import dotty.tools.pc.AutoImports.AutoImportsGenerator
3234
import dotty.tools.pc.AutoImports.SymbolImport
@@ -75,10 +77,23 @@ object CaseKeywordCompletion:
7577
patternOnly,
7678
hasBind
7779
)
80+
7881
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedContext)
7982
val selTpe = selector match
8083
case EmptyTree =>
8184
parent match
85+
/* Parent is an unapply pattern */
86+
case UnApply(fn, implicits, patterns) if !fn.tpe.isErroneous =>
87+
patternOnly match
88+
case None => None
89+
case Some(value) =>
90+
val argPts = UnapplyArgs(fn.tpe.widen.finalResultType, fn, patterns, parent.srcPos).argTypes
91+
patterns.zipWithIndex
92+
.find:
93+
case (id@Ident(v), tpe) => v.decoded == value
94+
case _ => false
95+
.map((_, id) => argPts(id).widen.deepDealias)
96+
/* Parent is a function expecting a case match expression */
8297
case TreeApply(fun, _) if !fun.tpe.isErroneous =>
8398
fun.tpe.paramInfoss match
8499
case (head :: Nil) :: _
@@ -105,7 +120,8 @@ object CaseKeywordCompletion:
105120
if patternOnly.isEmpty then
106121
val selectorTpe = selTpe.show
107122
val tpeLabel =
108-
if !selectorTpe.contains("x$1") then selectorTpe
123+
if !selectorTpe.contains("x$1") /* selector of a function type? */ then
124+
selectorTpe
109125
else selector.symbol.info.show
110126
val label = s"case ${tpeLabel} =>"
111127
List(

0 commit comments

Comments
 (0)