From a73bae5c52c348c5ba4046314e280293cee3dfe6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Cie=C5=9Bluk?=
<72950514+jkciesluk@users.noreply.github.com>
Date: Fri, 2 Feb 2024 09:28:04 +0100
Subject: [PATCH] chore: Backport changes from Metals (#19592)
backports https://github.com/scalameta/metals/pull/5346
[Cherry-picked f15bbdad89e1a32b66f2c4c7bfae1048f13313e1]
---
.../pc/completions/MatchCaseCompletions.scala | 32 +++++++++++++++----
.../completion/CompletionCaseSuite.scala | 18 ++++++++++-
2 files changed, 42 insertions(+), 8 deletions(-)
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 063ed2b44225..45f303316818 100644
--- a/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala
+++ b/presentation-compiler/src/main/dotty/tools/pc/completions/MatchCaseCompletions.scala
@@ -164,13 +164,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.
@@ -447,6 +451,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(
denot: Denotation,
label: String,
diff --git a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala
index 96a7cff9e73c..7a00c0397644 100644
--- a/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala
+++ b/presentation-compiler/test/dotty/tools/pc/tests/completion/CompletionCaseSuite.scala
@@ -542,7 +542,9 @@ class CompletionCaseSuite extends BaseCompletionSuite:
| ca@@
| }
|}""".stripMargin,
- ""
+ """
+ |case
+ |""".stripMargin
)
@Test def `private-member-2` =
@@ -722,3 +724,17 @@ class CompletionCaseSuite extends BaseCompletionSuite:
|""".stripMargin,
"case (Int, Int) => scala",
)
+
+ @Test def `keyword-only` =
+ check(
+ """
+ |sealed trait Alpha
+ |object A {
+ | List.empty[Alpha].groupBy{
+ | ca@@
+ | }
+ |}
+ |""".stripMargin,
+ "case",
+ )
+