Skip to content

Commit 240ac68

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 a918d61 commit 240ac68

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
@@ -366,6 +366,22 @@ class Completions(
366366
true,
367367
)
368368

369+
// unapply pattern
370+
case Ident(name) :: (unapp : UnApply) :: _ =>
371+
(
372+
CaseKeywordCompletion.contribute(
373+
EmptyTree, // no selector
374+
completionPos,
375+
indexedContext,
376+
config,
377+
search,
378+
parent = unapp,
379+
autoImports,
380+
patternOnly = Some(name.decoded)
381+
),
382+
false,
383+
)
384+
369385
// class FooImpl extends Foo:
370386
// def x|
371387
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
@@ -28,6 +28,8 @@ import dotty.tools.dotc.core.Types.NoType
2828
import dotty.tools.dotc.core.Types.OrType
2929
import dotty.tools.dotc.core.Types.Type
3030
import dotty.tools.dotc.core.Types.TypeRef
31+
import dotty.tools.dotc.core.Types.AppliedType
32+
import dotty.tools.dotc.typer.Applications.unapplyArgs
3133
import dotty.tools.dotc.util.SourcePosition
3234
import dotty.tools.pc.AutoImports.AutoImportsGenerator
3335
import dotty.tools.pc.AutoImports.SymbolImport
@@ -76,10 +78,23 @@ object CaseKeywordCompletion:
7678
patternOnly,
7779
hasBind
7880
)
81+
7982
val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedContext)
8083
val selTpe = selector match
8184
case EmptyTree =>
8285
parent match
86+
/* Parent is an unapply pattern */
87+
case UnApply(fn, implicits, patterns) if !fn.tpe.isErroneous =>
88+
patternOnly match
89+
case None => None
90+
case Some(value) =>
91+
val argPts = unapplyArgs(fn.tpe.widen.finalResultType, fn, patterns, parent.srcPos)
92+
patterns.zipWithIndex
93+
.find:
94+
case (id@Ident(v), tpe) => v.decoded == value
95+
case _ => false
96+
.map((_, id) => argPts(id).widen.metalsDealias)
97+
/* Parent is a function expecting a case match expression */
8398
case TreeApply(fun, _) if !fun.tpe.isErroneous =>
8499
fun.tpe.paramInfoss match
85100
case (head :: Nil) :: _
@@ -106,7 +121,8 @@ object CaseKeywordCompletion:
106121
if patternOnly.isEmpty then
107122
val selectorTpe = selTpe.show
108123
val tpeLabel =
109-
if !selectorTpe.contains("x$1") then selectorTpe
124+
if !selectorTpe.contains("x$1") /* selector of a function type? */ then
125+
selectorTpe
110126
else selector.symbol.info.show
111127
val label = s"case ${tpeLabel} =>"
112128
List(

0 commit comments

Comments
 (0)