From 240ac683299f05fdb4aa143146b0005e32747eda Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Fri, 5 Apr 2024 23:28:18 +0200 Subject: [PATCH] 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. --- .../tools/pc/completions/Completions.scala | 16 ++++++++++++++++ .../pc/completions/MatchCaseCompletions.scala | 18 +++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala index 81a543701817..58f1b7033e03 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/Completions.scala @@ -366,6 +366,22 @@ class Completions( true, ) + // unapply pattern + case Ident(name) :: (unapp : UnApply) :: _ => + ( + CaseKeywordCompletion.contribute( + EmptyTree, // no selector + completionPos, + indexedContext, + config, + search, + parent = unapp, + autoImports, + patternOnly = Some(name.decoded) + ), + false, + ) + // class FooImpl extends Foo: // def x| case OverrideExtractor(td, completing, start, exhaustive, fallbackName) => 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 7f1d92305309..3f0b9e562932 100644 --- a/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala +++ b/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala @@ -28,6 +28,8 @@ import dotty.tools.dotc.core.Types.NoType import dotty.tools.dotc.core.Types.OrType import dotty.tools.dotc.core.Types.Type import dotty.tools.dotc.core.Types.TypeRef +import dotty.tools.dotc.core.Types.AppliedType +import dotty.tools.dotc.typer.Applications.unapplyArgs import dotty.tools.dotc.util.SourcePosition import dotty.tools.pc.AutoImports.AutoImportsGenerator import dotty.tools.pc.AutoImports.SymbolImport @@ -76,10 +78,23 @@ object CaseKeywordCompletion: patternOnly, hasBind ) + val printer = ShortenedTypePrinter(search, IncludeDefaultParam.Never)(using indexedContext) val selTpe = selector match case EmptyTree => parent match + /* Parent is an unapply pattern */ + case UnApply(fn, implicits, patterns) if !fn.tpe.isErroneous => + patternOnly match + case None => None + case Some(value) => + val argPts = unapplyArgs(fn.tpe.widen.finalResultType, fn, patterns, parent.srcPos) + patterns.zipWithIndex + .find: + case (id@Ident(v), tpe) => v.decoded == value + case _ => false + .map((_, id) => argPts(id).widen.metalsDealias) + /* Parent is a function expecting a case match expression */ case TreeApply(fun, _) if !fun.tpe.isErroneous => fun.tpe.paramInfoss match case (head :: Nil) :: _ @@ -106,7 +121,8 @@ object CaseKeywordCompletion: if patternOnly.isEmpty then val selectorTpe = selTpe.show val tpeLabel = - if !selectorTpe.contains("x$1") then selectorTpe + if !selectorTpe.contains("x$1") /* selector of a function type? */ then + selectorTpe else selector.symbol.info.show val label = s"case ${tpeLabel} =>" List(