Skip to content

Commit 485e84c

Browse files
committed
Address review feedback
1 parent 2c40a21 commit 485e84c

File tree

4 files changed

+54
-28
lines changed

4 files changed

+54
-28
lines changed

mtags/src/main/scala/scala/meta/internal/pc/CompletionProvider.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ import org.eclipse.{lsp4j => l}
1616

1717
class CompletionProvider(
1818
val compiler: MetalsGlobal,
19-
params: OffsetParams,
20-
clientSupportsSnippets: Boolean
19+
params: OffsetParams
2120
) {
2221
import compiler._
2322

@@ -45,6 +44,8 @@ class CompletionProvider(
4544
)
4645
val pos = unit.position(params.offset)
4746
val isSnippet = isSnippetEnabled(pos, params.text())
47+
val clientSupportsSnippets =
48+
compiler.metalsConfig.isCompletionSnippetsEnabled()
4849
val (i, completion, editRange, query) = safeCompletionsAt(pos)
4950
val start = inferIdentStart(pos, params.text())
5051
val end = inferIdentEnd(pos, params.text())

mtags/src/main/scala/scala/meta/internal/pc/Completions.scala

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import scala.meta.internal.tokenizers.Chars
1919
*/
2020
trait Completions { this: MetalsGlobal =>
2121

22+
val clientSupportsSnippets =
23+
metalsConfig.isCompletionSnippetsEnabled()
24+
2225
/**
2326
* A member for symbols on the classpath that are not in scope, produced via workspace/symbol.
2427
*/
@@ -765,11 +768,11 @@ trait Completions { this: MetalsGlobal =>
765768
if (text.charAt(lit.pos.start - 1) != 's')
766769
List(new l.TextEdit(lit.pos.withEnd(lit.pos.start).toLSP, "s"))
767770
else Nil
768-
val dolarEdits = for {
771+
val dollarEdits = for {
769772
i <- lit.pos.start to (lit.pos.end - CURSOR.length())
770773
if text.charAt(i) == '$' && i != interpolator.dollar
771774
} yield new l.TextEdit(pos.source.position(i).withEnd(i).toLSP, "$")
772-
interpolatorEdit ++ dolarEdits
775+
interpolatorEdit ++ dollarEdits
773776
}
774777

775778
def newText(sym: Symbol): String = {
@@ -793,18 +796,23 @@ trait Completions { this: MetalsGlobal =>
793796

794797
val filter: String =
795798
text.substring(lit.pos.start, pos.point - interpolator.name.length)
799+
796800
override def contribute: List[Member] = {
797-
metalsScopeMembers(pos).collect {
798-
case s: ScopeMember
799-
if CompletionFuzzy.matches(interpolator.name, s.sym.name) =>
800-
val edit = new l.TextEdit(nameRange, newText(s.sym))
801-
val filterText = filter + s.sym.name.decoded
802-
new TextEditMember(
803-
filterText,
804-
edit,
805-
s.sym,
806-
additionalTextEdits = additionalEdits()
807-
)
801+
if (clientSupportsSnippets) {
802+
metalsScopeMembers(pos).collect {
803+
case s: ScopeMember
804+
if CompletionFuzzy.matches(interpolator.name, s.sym.name) =>
805+
val edit = new l.TextEdit(nameRange, newText(s.sym))
806+
val filterText = filter + s.sym.name.decoded
807+
new TextEditMember(
808+
filterText,
809+
edit,
810+
s.sym,
811+
additionalTextEdits = additionalEdits()
812+
)
813+
}
814+
} else {
815+
List.empty
808816
}
809817
}
810818
}
@@ -985,7 +993,7 @@ trait Completions { this: MetalsGlobal =>
985993
allParams.exists(param => param.name.startsWith(prefix))
986994
val isExplicitlyCalled = suffix.startsWith(prefix)
987995
val hasParamsToFill = allParams.count(!_.hasDefault) > 1
988-
if ((shouldShow || isExplicitlyCalled) && hasParamsToFill) {
996+
if ((shouldShow || isExplicitlyCalled) && hasParamsToFill && clientSupportsSnippets) {
989997
val editText = allParams.zipWithIndex
990998
.collect {
991999
case (param, index) if !param.hasDefault =>
@@ -1202,7 +1210,11 @@ trait Completions { this: MetalsGlobal =>
12021210
private def signature = printer.defaultMethodSignature()
12031211
private def edit = new l.TextEdit(
12041212
range,
1205-
s"$filterText$signature = $${0:???}"
1213+
if (clientSupportsSnippets) {
1214+
s"$filterText$signature = $${0:???}"
1215+
} else {
1216+
s"$filterText$signature = ???"
1217+
}
12061218
)
12071219
}
12081220

@@ -1259,7 +1271,11 @@ trait Completions { this: MetalsGlobal =>
12591271
"match",
12601272
new l.TextEdit(
12611273
editRange,
1262-
"match {\n\tcase$0\n}"
1274+
if (clientSupportsSnippets) {
1275+
"match {\n\tcase$0\n}"
1276+
} else {
1277+
"match"
1278+
}
12631279
),
12641280
completionsSymbol("match"),
12651281
label = Some("match"),
@@ -1273,7 +1289,11 @@ trait Completions { this: MetalsGlobal =>
12731289
tail
12741290
.map(_.edit.getNewText())
12751291
.mkString(
1276-
s"match {\n\t${head.edit.getNewText} $$0\n\t",
1292+
if (clientSupportsSnippets) {
1293+
"match {\n\t${head.edit.getNewText} $$0\n\t"
1294+
} else {
1295+
"match {\n\t${head.edit.getNewText}\n\t"
1296+
},
12771297
"\n\t",
12781298
"\n}"
12791299
)
@@ -1408,7 +1428,10 @@ trait Completions { this: MetalsGlobal =>
14081428
if (definitions.isTupleType(parents.selector)) {
14091429
result += new TextEditMember(
14101430
"case () =>",
1411-
new l.TextEdit(editRange, "case ($0) =>"),
1431+
new l.TextEdit(
1432+
editRange,
1433+
if (clientSupportsSnippets) "case ($0) =>" else "case () =>"
1434+
),
14121435
parents.selector.typeSymbol,
14131436
label = Some(s"case ${parents.selector} =>"),
14141437
command = metalsConfig.parameterHintsCommand().asScala
@@ -1466,8 +1489,10 @@ trait Completions { this: MetalsGlobal =>
14661489
val label = s"case $pattern =>"
14671490
new TextEditMember(
14681491
filterText = label,
1469-
edit =
1470-
new l.TextEdit(editRange, label + (if (isSnippet) " $0" else "")),
1492+
edit = new l.TextEdit(
1493+
editRange,
1494+
label + (if (isSnippet && clientSupportsSnippets) " $0" else "")
1495+
),
14711496
sym = sym,
14721497
label = Some(label),
14731498
additionalTextEdits = autoImports
@@ -1482,7 +1507,8 @@ trait Completions { this: MetalsGlobal =>
14821507
s"case _: $name",
14831508
new l.TextEdit(
14841509
editRange,
1485-
if (isSnippet) s"case $${0:_}: $name$suffix => "
1510+
if (isSnippet && clientSupportsSnippets)
1511+
s"case $${0:_}: $name$suffix => "
14861512
else s"case _: $name$suffix =>"
14871513
),
14881514
sym,

mtags/src/main/scala/scala/meta/internal/pc/MetalsGlobal.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ class MetalsGlobal(
573573

574574
def snippetCursor: String = sym.paramss match {
575575
case Nil =>
576-
"$0"
576+
if (clientSupportsSnippets) "$0" else ""
577577
case Nil :: Nil =>
578-
"()$0"
578+
if (clientSupportsSnippets) "()$0" else "()"
579579
case _ =>
580-
"($0)"
580+
if (clientSupportsSnippets) "($0)" else ""
581581
}
582582

583583
def isDefined: Boolean =

mtags/src/main/scala/scala/meta/internal/pc/ScalaPresentationCompiler.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ case class ScalaPresentationCompiler(
8585
params: OffsetParams
8686
): CompletableFuture[CompletionList] =
8787
access.withInterruptableCompiler(emptyCompletion, params.token) { global =>
88-
new CompletionProvider(global, params, config.isCompletionSnippetsEnabled)
89-
.completions()
88+
new CompletionProvider(global, params).completions()
9089
}
9190

9291
// NOTE(olafur): hover and signature help use a "shared" compiler instance because

0 commit comments

Comments
 (0)