Skip to content

Commit

Permalink
fix: Remove pattern only completions for tuple type
Browse files Browse the repository at this point in the history
It was hard to detect we are in `yield @@` or `case (@@)` and this completion was not really usefull,
so it was best to remove it
  • Loading branch information
jkciesluk authored and tgodzik committed Nov 2, 2023
1 parent 6eb5ee4 commit 0dd2cd8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,20 @@ trait MatchCaseCompletions { this: MetalsGlobal =>

// Special handle case when selector is a tuple or `FunctionN`.
if (definitions.isTupleType(parents.selector)) {
List(
new TextEditMember(
"case () =>",
new l.TextEdit(
editRange,
if (clientSupportsSnippets) "case ($0) =>" else "case () =>"
),
selectorSym,
label = Some(s"case ${parents.selector} =>"),
command = metalsConfig.parameterHintsCommand().asScala
if (patternOnly.isEmpty)
List(
new TextEditMember(
"case () =>",
new l.TextEdit(
editRange,
if (clientSupportsSnippets) "case ($0) =>" else "case () =>"
),
selectorSym,
label = Some(s"case ${parents.selector} =>"),
command = metalsConfig.parameterHintsCommand().asScala
)
)
)
else Nil
} else {
val completionGenerator = new CompletionValueGenerator(
editRange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ object CaseKeywordCompletion:
case _ =>
new Parents(NoType, definitions)
case sel =>
val selectorTpe = sel.tpe
new Parents(selectorTpe, definitions)
new Parents(sel.tpe, definitions)

val selectorSym = parents.selector.widen.metalsDealias.typeSymbol

Expand All @@ -96,32 +95,26 @@ object CaseKeywordCompletion:
selectorSym
)
then
val selectorTpe = parents.selector.show
val tpeLabel =
if !selectorTpe.contains("x$1") then selectorTpe
else selector.symbol.info.show
val label =
if patternOnly.isEmpty then s"case ${tpeLabel} =>"
else tpeLabel
if completionGenerator.fuzzyMatches(label) then
if patternOnly.isEmpty then
val selectorTpe = parents.selector.show
val tpeLabel =
if !selectorTpe.contains("x$1") then selectorTpe
else selector.symbol.info.show
val label = s"case ${tpeLabel} =>"
List(
CompletionValue.CaseKeyword(
selectorSym,
label,
Some(
if patternOnly.isEmpty then
if config.isCompletionSnippetsEnabled() then "case ($0) =>"
else "case () =>"
else if config.isCompletionSnippetsEnabled() then "($0)"
else "()"
if config.isCompletionSnippetsEnabled() then "case ($0) =>"
else "case () =>"
),
Nil,
range = Some(completionPos.toEditRange),
command = config.parameterHintsCommand().asScala,
)
)
else Nil
end if
else
val result = ListBuffer.empty[SymbolImport]
val isVisited = mutable.Set.empty[Symbol]
Expand Down
12 changes: 12 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionCaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -753,4 +753,16 @@ class CompletionCaseSuite extends BaseCompletionSuite {
"",
)

check(
"lambda-case-tuple",
"""|object A {
| val a = List((1,2)).foreach {
| case (a,b) => println(a)
| case@@
| }
|}
|""".stripMargin,
"case (Int, Int) => scala",
)

}

0 comments on commit 0dd2cd8

Please sign in to comment.