Skip to content

Commit

Permalink
bugfix: add multiline comment completion
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Oct 19, 2023
1 parent 430012e commit 9ed092b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class MetalsGlobal(
with completions.ScalaCliCompletions
with completions.MillIvyCompletions
with completions.SbtLibCompletions
with completions.MultilineCommentCompletions
with Signatures
with Compat
with GlobalProxy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ trait Completions { this: MetalsGlobal =>
SbtLibCompletion(coursierComplete, pos, dep)
case ScalaCliExtractor(dep) =>
ScalaCliCompletion(coursierComplete, pos, text, dep)
case _ if isMultilineCommentStart(pos, text) =>
MultilineCommentCompletion(editRange, pos, text)
case _ if isScaladocCompletion(pos, text) =>
val associatedDef = onUnitOf(pos.source) { unit =>
new AssociatedMemberDefFinder(pos).findAssociatedDef(unit.body)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package scala.meta.internal.pc.completions

import scala.meta.internal.pc.MetalsGlobal

import org.eclipse.{lsp4j => l}

trait MultilineCommentCompletions { this: MetalsGlobal =>
case class MultilineCommentCompletion(
editRange: l.Range,
pos: Position,
text: String
) extends CompletionPosition {

override def contribute: List[TextEditMember] = {
val newText =
if (clientSupportsSnippets) s" $$0 */"
else s" */"
List(
new TextEditMember(
"Multiline Comment",
new l.TextEdit(
editRange,
newText
),
completionsSymbol("Multiline"),
label = Some("/* */"),
detail = Some("Multiline Comment")
)
)
}
}

protected def isMultilineCommentStart(
pos: Position,
text: String
): Boolean = {
pos.isDefined &&
pos.point >= 2 &&
text.charAt(pos.point - 2) == '/' &&
text.charAt(pos.point - 1) == '*'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,15 @@ class Completions(
path match
case ScalaCliCompletions(dependency) =>
(ScalaCliCompletions.contribute(dependency), true)

case _
if MultilineCommentCompletion.isMultilineCommentCompletion(
pos,
text,
) =>
val values = MultilineCommentCompletion.contribute(config)
(values, true)

case _ if ScaladocCompletions.isScaladocCompletion(pos, text) =>
val values = ScaladocCompletions.contribute(pos, text, config)
(values, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package scala.meta.internal.pc.completions

import scala.meta.pc.PresentationCompilerConfig

import dotty.tools.dotc.util.SourcePosition

object MultilineCommentCompletion:

def contribute(config: PresentationCompilerConfig): List[CompletionValue] =
val newText = if config.isCompletionSnippetsEnabled then " $0 */" else " */"
List(CompletionValue.document("/* */", newText, "Multiline Comment"))

def isMultilineCommentCompletion(pos: SourcePosition, text: String): Boolean =
pos.point >= 2 &&
text.charAt(pos.point - 2) == '/' &&
text.charAt(pos.point - 1) == '*'
16 changes: 16 additions & 0 deletions tests/cross/src/test/scala/tests/pc/CompletionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1921,4 +1921,20 @@ class CompletionSuite extends BaseCompletionSuite {
|""".stripMargin,
)

checkEdit(
"multiline-comment",
"""|package a
|object O {
| /*@@
| def f = 1
|}
|""".stripMargin,
"""|package a
|object O {
| /* $0 */
| def f = 1
|}
|""".stripMargin,
)

}

0 comments on commit 9ed092b

Please sign in to comment.