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 Jun 26, 2023
1 parent 14b56d9 commit f4479ad
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ trait MatchCaseCompletions { this: MetalsGlobal =>
sortSubclasses(sealedMembers, selectorSym.tpe, source).map(_._2)

sortedMembers match {
case Nil => edits
case Nil => edits ++ completionGenerator.caseKeywordOnly
case head :: tail =>
val (newLine, addIndent) =
if (moveToNewLine) ("\n\t", "\t") else ("", "")
Expand Down Expand Up @@ -225,9 +225,9 @@ trait MatchCaseCompletions { this: MetalsGlobal =>
additionalTextEdits =
sortedMembers.toList.flatMap(_.additionalTextEdits)
)
exhaustive :: edits
exhaustive :: edits ++ completionGenerator.caseKeywordOnly
}
case None => edits
case None => edits ++ completionGenerator.caseKeywordOnly
}
}
}
Expand Down Expand Up @@ -460,6 +460,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 @@ -183,7 +183,7 @@ object CaseKeywordCompletion:
search,
)
sealedMembers match
case Nil => caseItems
case Nil => caseItems ++ completionGenerator.caseKeywordOnly
case (_, label) :: tail =>
val (newLine, addIndent) =
if moveToNewLine then ("\n\t", "\t") else ("", "")
Expand All @@ -207,9 +207,9 @@ object CaseKeywordCompletion:
importEdit.toList,
s" ${selectorSym.decodedName} (${res.length} cases)",
)
exhaustive :: caseItems
exhaustive :: caseItems ++ completionGenerator.caseKeywordOnly
end match
case None => caseItems
case None => caseItems ++ completionGenerator.caseKeywordOnly
end match
end if

Expand Down Expand Up @@ -443,6 +443,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
50 changes: 44 additions & 6 deletions tests/cross/src/test/scala/tests/pc/CompletionCaseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand Down Expand Up @@ -54,6 +55,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|case _: HasMouth[_] => pkg
|case HasWings(e) => pkg
|case Seal => pkg
|case
|""".stripMargin,
compat = Map(
"3" -> """|case _: Animal => pkg
Expand All @@ -65,6 +67,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|case _: HasMouth[?] => pkg
|case HasWings(e) => pkg
|case Seal => pkg
|case
|""".stripMargin
),
)
Expand All @@ -79,6 +82,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -93,6 +97,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -107,6 +112,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -120,6 +126,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case Left(value) => scala.util
|case Right(value) => scala.util
|case
|""".stripMargin,
)

Expand All @@ -134,6 +141,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case Failure(exception) => scala.util
|case Success(value) => scala.util
|case
|""".stripMargin,
)

Expand All @@ -153,9 +161,12 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
// Assert we don't include AdtTwo in the results.
"""|case Cls(a, b) => `sealed-two`.Outer
|case
|""".stripMargin,
compat = Map(
"3" -> "case Cls(a, b) => sealed-two.Outer"
"3" -> """|case Cls(a, b) => sealed-two.Outer
|case
|""".stripMargin
),
)

Expand All @@ -173,6 +184,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case scala.util.Left(value) =>
|case Right(value) => scala.util
|case
|""".stripMargin,
)

Expand Down Expand Up @@ -234,6 +246,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -248,6 +261,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -274,11 +288,13 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|case (exhaustive) Option[A] (2 cases)
|""".stripMargin,
compat = Map("3" -> """|case None => scala
|case Some(value) => scala
|case (exhaustive) Option (2 cases)
|case
|""".stripMargin),
)

Expand All @@ -293,6 +309,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -307,6 +324,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -320,11 +338,13 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|case (exhaustive) Option[A] (2 cases)
|""".stripMargin,
compat = Map("3" -> """|case None => scala
|case Some(value) => scala
|case (exhaustive) Option (2 cases)
|case
|""".stripMargin),
)

Expand All @@ -338,11 +358,13 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|case (exhaustive) Option[A] (2 cases)
|""".stripMargin,
compat = Map("3" -> """|case None => scala
|case Some(value) => scala
|case (exhaustive) Option (2 cases)
|case
|""".stripMargin),
)

Expand All @@ -357,6 +379,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -371,6 +394,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case None => scala
|case Some(value) => scala
|case
|""".stripMargin,
)

Expand All @@ -384,11 +408,13 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|}""".stripMargin,
"""|case head :: next => scala.collection.immutable
|case Nil => scala.collection.immutable
|case
|""".stripMargin,
compat = Map(
"2.12" ->
"""|case head :: tl => scala.collection.immutable
|case Nil => scala.collection.immutable
|case
|""".stripMargin
),
)
Expand Down Expand Up @@ -463,6 +489,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|""".stripMargin,
"cas@@",
"case a :+: b => $0",
filter = _.contains(":+:"),
)

checkEditLine(
Expand All @@ -482,6 +509,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
"cas@@",
// Assert we don't use infix syntax because `::` resolves to conflicting symbol in scope.
"case Outer.::(a, b) => $0",
filter = _.contains("::"),
)

check(
Expand All @@ -499,6 +527,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
"""|case Color.Blue =>
|case Color.Green =>
|case Color.Red =>
|case
|""".stripMargin,
)

Expand Down Expand Up @@ -563,6 +592,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
| List(Foo(1,2)).map{ cas@@ }
|}""".stripMargin,
"""|case Foo(a, b) => example
|case
|""".stripMargin,
)

Expand All @@ -577,7 +607,7 @@ class CompletionCaseSuite extends BaseCompletionSuite {
| ca@@
| }
|}""".stripMargin,
"",
"case",
)

check(
Expand All @@ -592,7 +622,9 @@ class CompletionCaseSuite extends BaseCompletionSuite {
| cas@@
|}""".stripMargin,
"""|case A.B =>
|case A.C =>""".stripMargin,
|case A.C =>
|case
|""".stripMargin,
)

check(
Expand Down Expand Up @@ -632,7 +664,9 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|""".stripMargin,
"""|case Cleaning =>Activity & Physical & Chore
|case DishWashing(amount) => exhaustive-enum-tags.Activity
|case Sports(time, intensity) => exhaustive-enum-tags.Activity""".stripMargin,
|case Sports(time, intensity) => exhaustive-enum-tags.Activity
|case
|""".stripMargin,
)

check(
Expand Down Expand Up @@ -660,7 +694,9 @@ class CompletionCaseSuite extends BaseCompletionSuite {
|
|""".stripMargin,
"""|case Cleaning =>Activity & Physical & Chore
|case Sports(time, intensity) => exhaustive-enum-tags2.Activity""".stripMargin,
|case Sports(time, intensity) => exhaustive-enum-tags2.Activity
|case
|""".stripMargin,
)

check(
Expand Down Expand Up @@ -690,7 +726,9 @@ class CompletionCaseSuite extends BaseCompletionSuite {
"""|case Cleaning =>Activity & Physical & Chore
|case Reading(book, author) => exhaustive-enum-tags3.Activity
|case Singing(song) => exhaustive-enum-tags3.Activity
|case Sports(time, intensity) => exhaustive-enum-tags3.Activity""".stripMargin,
|case Sports(time, intensity) => exhaustive-enum-tags3.Activity
|case
|""".stripMargin,
)

}

0 comments on commit f4479ad

Please sign in to comment.