Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
gabro committed Nov 11, 2019
1 parent 2c40a21 commit 5f8a7e4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
Expand Up @@ -16,8 +16,7 @@ import org.eclipse.{lsp4j => l}

class CompletionProvider(
val compiler: MetalsGlobal,
params: OffsetParams,
clientSupportsSnippets: Boolean
params: OffsetParams
) {
import compiler._

Expand Down Expand Up @@ -45,6 +44,8 @@ class CompletionProvider(
)
val pos = unit.position(params.offset)
val isSnippet = isSnippetEnabled(pos, params.text())
val clientSupportsSnippets =
compiler.metalsConfig.isCompletionSnippetsEnabled()
val (i, completion, editRange, query) = safeCompletionsAt(pos)
val start = inferIdentStart(pos, params.text())
val end = inferIdentEnd(pos, params.text())
Expand Down
42 changes: 32 additions & 10 deletions mtags/src/main/scala/scala/meta/internal/pc/Completions.scala
Expand Up @@ -19,6 +19,9 @@ import scala.meta.internal.tokenizers.Chars
*/
trait Completions { this: MetalsGlobal =>

val clientSupportsSnippets =
metalsConfig.isCompletionSnippetsEnabled()

/**
* A member for symbols on the classpath that are not in scope, produced via workspace/symbol.
*/
Expand Down Expand Up @@ -765,11 +768,11 @@ trait Completions { this: MetalsGlobal =>
if (text.charAt(lit.pos.start - 1) != 's')
List(new l.TextEdit(lit.pos.withEnd(lit.pos.start).toLSP, "s"))
else Nil
val dolarEdits = for {
val dollarEdits = for {
i <- lit.pos.start to (lit.pos.end - CURSOR.length())
if text.charAt(i) == '$' && i != interpolator.dollar
} yield new l.TextEdit(pos.source.position(i).withEnd(i).toLSP, "$")
interpolatorEdit ++ dolarEdits
interpolatorEdit ++ dollarEdits
}

def newText(sym: Symbol): String = {
Expand All @@ -793,6 +796,7 @@ trait Completions { this: MetalsGlobal =>

val filter: String =
text.substring(lit.pos.start, pos.point - interpolator.name.length)

override def contribute: List[Member] = {
metalsScopeMembers(pos).collect {
case s: ScopeMember
Expand Down Expand Up @@ -985,7 +989,7 @@ trait Completions { this: MetalsGlobal =>
allParams.exists(param => param.name.startsWith(prefix))
val isExplicitlyCalled = suffix.startsWith(prefix)
val hasParamsToFill = allParams.count(!_.hasDefault) > 1
if ((shouldShow || isExplicitlyCalled) && hasParamsToFill) {
if ((shouldShow || isExplicitlyCalled) && hasParamsToFill && clientSupportsSnippets) {
val editText = allParams.zipWithIndex
.collect {
case (param, index) if !param.hasDefault =>
Expand Down Expand Up @@ -1202,7 +1206,11 @@ trait Completions { this: MetalsGlobal =>
private def signature = printer.defaultMethodSignature()
private def edit = new l.TextEdit(
range,
s"$filterText$signature = $${0:???}"
if (clientSupportsSnippets) {
s"$filterText$signature = $${0:???}"
} else {
s"$filterText$signature = ???"
}
)
}

Expand Down Expand Up @@ -1259,7 +1267,11 @@ trait Completions { this: MetalsGlobal =>
"match",
new l.TextEdit(
editRange,
"match {\n\tcase$0\n}"
if (clientSupportsSnippets) {
"match {\n\tcase$0\n}"
} else {
"match"
}
),
completionsSymbol("match"),
label = Some("match"),
Expand All @@ -1273,7 +1285,11 @@ trait Completions { this: MetalsGlobal =>
tail
.map(_.edit.getNewText())
.mkString(
s"match {\n\t${head.edit.getNewText} $$0\n\t",
if (clientSupportsSnippets) {
"match {\n\t${head.edit.getNewText} $$0\n\t"
} else {
"match {\n\t${head.edit.getNewText}\n\t"
},
"\n\t",
"\n}"
)
Expand Down Expand Up @@ -1408,7 +1424,10 @@ trait Completions { this: MetalsGlobal =>
if (definitions.isTupleType(parents.selector)) {
result += new TextEditMember(
"case () =>",
new l.TextEdit(editRange, "case ($0) =>"),
new l.TextEdit(
editRange,
if (clientSupportsSnippets) "case ($0) =>" else "case () =>"
),
parents.selector.typeSymbol,
label = Some(s"case ${parents.selector} =>"),
command = metalsConfig.parameterHintsCommand().asScala
Expand Down Expand Up @@ -1466,8 +1485,10 @@ trait Completions { this: MetalsGlobal =>
val label = s"case $pattern =>"
new TextEditMember(
filterText = label,
edit =
new l.TextEdit(editRange, label + (if (isSnippet) " $0" else "")),
edit = new l.TextEdit(
editRange,
label + (if (isSnippet && clientSupportsSnippets) " $0" else "")
),
sym = sym,
label = Some(label),
additionalTextEdits = autoImports
Expand All @@ -1482,7 +1503,8 @@ trait Completions { this: MetalsGlobal =>
s"case _: $name",
new l.TextEdit(
editRange,
if (isSnippet) s"case $${0:_}: $name$suffix => "
if (isSnippet && clientSupportsSnippets)
s"case $${0:_}: $name$suffix => "
else s"case _: $name$suffix =>"
),
sym,
Expand Down
Expand Up @@ -573,11 +573,11 @@ class MetalsGlobal(

def snippetCursor: String = sym.paramss match {
case Nil =>
"$0"
if (clientSupportsSnippets) "$0" else ""
case Nil :: Nil =>
"()$0"
if (clientSupportsSnippets) "()$0" else "()"
case _ =>
"($0)"
if (clientSupportsSnippets) "($0)" else ""
}

def isDefined: Boolean =
Expand Down
Expand Up @@ -85,8 +85,7 @@ case class ScalaPresentationCompiler(
params: OffsetParams
): CompletableFuture[CompletionList] =
access.withInterruptableCompiler(emptyCompletion, params.token) { global =>
new CompletionProvider(global, params, config.isCompletionSnippetsEnabled)
.completions()
new CompletionProvider(global, params).completions()
}

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

0 comments on commit 5f8a7e4

Please sign in to comment.