Skip to content

Commit

Permalink
improvement: Add case keyword completion
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Jan 16, 2024
1 parent 1f3e9f2 commit 4b38ed0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ trait MatchCaseCompletions { this: MetalsGlobal =>
}
}
val members = result.result()
val edits = members.map(_._2)
val edits = {
if (members.isEmpty) completionGenerator.caseKeywordOnly
else members.map(_._2)
}
// In `List(foo).map { cas@@} we want to provide also `case (exhaustive)` completion
// which works like exhaustive match, so we need to collect only members from this step
includeExhaustive match {
Expand Down Expand Up @@ -462,6 +465,22 @@ trait MatchCaseCompletions { this: MetalsGlobal =>
)
}

def caseKeywordOnly: List[TextEditMember] =
if (patternOnly.isEmpty) {
val label = "case"
val suffix =
if (clientSupportsSnippets) " $0 =>"
else " "
List(
new TextEditMember(
label,
new l.TextEdit(editRange, label + suffix),
NoSymbol.newErrorSymbol(TermName("case")).setInfo(NoType),
label = Some(label)
)
)
} else Nil

private def tryInfixPattern(sym: Symbol): Option[String] = {
sym.primaryConstructor.paramss match {
case (a :: b :: Nil) :: Nil =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,17 @@ object CaseKeywordCompletion:
(si, label)
}
}
val caseItems = res.map((si, label) =>
completionGenerator.toCompletionValue(
si.sym,
label,
autoImportsGen.renderImports(si.importSel.toList),
)
)
val caseItems =
if res.isEmpty then completionGenerator.caseKeywordOnly
else
res.map((si, label) =>
completionGenerator.toCompletionValue(
si.sym,
label,
autoImportsGen.renderImports(si.importSel.toList),
)
)

includeExhaustive match
// In `List(foo).map { cas@@} we want to provide also `case (exhaustive)` completion
// which works like exhaustive match.
Expand Down Expand Up @@ -446,6 +450,20 @@ class CompletionValueGenerator(
end if
end labelForCaseMember

def caseKeywordOnly: List[CompletionValue.Keyword] =
if patternOnly.isEmpty then
val label = "case"
val suffix =
if clientSupportsSnippets then " $0 =>"
else " "
List(
CompletionValue.Keyword(
label,
Some(label + suffix),
)
)
else Nil

def toCompletionValue(
sym: Symbol,
label: String,
Expand Down
18 changes: 16 additions & 2 deletions tests/cross/src/test/scala/tests/pc/CompletionCaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
)

check(
"private-member".tag(IgnoreScala2),
"private-member".tag(IgnoreScala2.and(IgnoreForScala3CompilerPC)),
"""
|package example
|import scala.collection.immutable.Vector
Expand All @@ -591,7 +591,8 @@ class CompletionCaseSuite extends BaseCompletionSuite {
| ca@@
| }
|}""".stripMargin,
""
"""|case
|""".stripMargin
)

check(
Expand Down Expand Up @@ -779,4 +780,17 @@ class CompletionCaseSuite extends BaseCompletionSuite {
"case (Int, Int) => scala"
)

check(
"keyword-only".tag(IgnoreForScala3CompilerPC),
"""
|sealed trait Alpha
|object A {
| List.empty[Alpha].groupBy{
| ca@@
| }
|}""".stripMargin,
"""|case
|""".stripMargin
)

}

0 comments on commit 4b38ed0

Please sign in to comment.