Skip to content

Commit

Permalink
bugfix: Case completions for tuple type
Browse files Browse the repository at this point in the history
We shouldn't show case completions for tuple type if query doesn't match label.
Also, sometimes label contained `x$1` symbol (eg. `(x$1: (Int, Int)) @unchecked scala`,
which we don't want to show to the user.
I think the simplest way to detect it is just by looking if label contains `x$1`
  • Loading branch information
jkciesluk committed May 31, 2023
1 parent 8ed12f6 commit 23bdd50
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,34 +86,42 @@ object CaseKeywordCompletion:
new Parents(argTypes, definitions)
case _ =>
new Parents(NoType, definitions)
case sel => new Parents(sel.tpe, definitions)
case sel =>
val selectorTpe = sel.tpe
new Parents(selectorTpe, definitions)

val selectorSym = parents.selector.typeSymbol

// Special handle case when selector is a tuple or `FunctionN`.
if definitions.isTupleClass(selectorSym) || definitions.isFunctionClass(
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 ${parents.selector.show} =>"
else parents.selector.show
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 "()"
),
Nil,
range = Some(completionPos.toEditRange),
command = config.parameterHintsCommand().asScala,
if patternOnly.isEmpty then s"case ${tpeLabel} =>"
else tpeLabel
if completionGenerator.fuzzyMatches(label) then
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 "()"
),
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
13 changes: 13 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionCaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -693,4 +693,17 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|case Sports(time, intensity) => exhaustive-enum-tags3.Activity""".stripMargin,
)

check(
"for-comp",
"""|object A {
| val a = for {
| foo <- List("a", "b", "c")
| abc = println("Print!")
| } yield bar@@
|
|}
|""".stripMargin,
"",
)

}

0 comments on commit 23bdd50

Please sign in to comment.