Skip to content

Commit e7e6f1e

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 54eb9d9 commit e7e6f1e

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
@@ -406,6 +406,22 @@ class Completions(
406406
true,
407407
)
408408

409+
// unapply pattern
410+
case Ident(name) :: (unapp : UnApply) :: _ =>
411+
(
412+
CaseKeywordCompletion.contribute(
413+
EmptyTree, // no selector
414+
completionPos,
415+
indexedContext,
416+
config,
417+
search,
418+
parent = unapp,
419+
autoImports,
420+
patternOnly = Some(name.decoded)
421+
),
422+
false,
423+
)
424+
409425
// class FooImpl extends Foo:
410426
// def x|
411427
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
@@ -26,6 +26,8 @@ import dotty.tools.dotc.core.Types.ClassInfo
2626
import dotty.tools.dotc.core.Types.OrType
2727
import dotty.tools.dotc.core.Types.Type
2828
import dotty.tools.dotc.core.Types.TypeRef
29+
import dotty.tools.dotc.core.Types.AppliedType
30+
import dotty.tools.dotc.typer.Applications.unapplyArgs
2931
import dotty.tools.dotc.util.SourcePosition
3032
import dotty.tools.pc.AutoImports.AutoImportsGenerator
3133
import dotty.tools.pc.AutoImports.SymbolImport
@@ -74,10 +76,23 @@ object CaseKeywordCompletion:
7476
patternOnly,
7577
hasBind
7678
)
79+
7780
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedContext)
7881
val selTpe = selector match
7982
case EmptyTree =>
8083
parent match
84+
/* Parent is an unapply pattern */
85+
case UnApply(fn, implicits, patterns) if !fn.tpe.isErroneous =>
86+
patternOnly match
87+
case None => None
88+
case Some(value) =>
89+
val argPts = unapplyArgs(fn.tpe.widen.finalResultType, fn, patterns, parent.srcPos)
90+
patterns.zipWithIndex
91+
.find:
92+
case (id@Ident(v), tpe) => v.decoded == value
93+
case _ => false
94+
.map((_, id) => argPts(id).widen.metalsDealias)
95+
/* Parent is a function expecting a case match expression */
8196
case TreeApply(fun, _) if !fun.tpe.isErroneous =>
8297
fun.tpe.paramInfoss match
8398
case (head :: Nil) :: _
@@ -104,7 +119,8 @@ object CaseKeywordCompletion:
104119
if patternOnly.isEmpty then
105120
val selectorTpe = selTpe.show
106121
val tpeLabel =
107-
if !selectorTpe.contains("x$1") then selectorTpe
122+
if !selectorTpe.contains("x$1") /* selector of a function type? */ then
123+
selectorTpe
108124
else selector.symbol.info.show
109125
val label = s"case ${tpeLabel} =>"
110126
List(

0 commit comments

Comments
 (0)