From 9ed092bfad0f44a5c0f93b07f675bd5946633948 Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Thu, 12 Oct 2023 13:45:59 +0200 Subject: [PATCH] bugfix: add multiline comment completion --- .../scala/meta/internal/pc/MetalsGlobal.scala | 1 + .../internal/pc/completions/Completions.scala | 2 + .../MultilineCommentCompletion.scala | 43 +++++++++++++++++++ .../internal/pc/completions/Completions.scala | 9 ++++ .../MultilineCommentCompletion.scala | 16 +++++++ .../test/scala/tests/pc/CompletionSuite.scala | 16 +++++++ 6 files changed, 87 insertions(+) create mode 100644 mtags/src/main/scala-2/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala create mode 100644 mtags/src/main/scala-3/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala index e783d92b351..be19ca4d20b 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/MetalsGlobal.scala @@ -54,6 +54,7 @@ class MetalsGlobal( with completions.ScalaCliCompletions with completions.MillIvyCompletions with completions.SbtLibCompletions + with completions.MultilineCommentCompletions with Signatures with Compat with GlobalProxy diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/completions/Completions.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/completions/Completions.scala index ee50e48f0c0..d0dc6f13745 100644 --- a/mtags/src/main/scala-2/scala/meta/internal/pc/completions/Completions.scala +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/completions/Completions.scala @@ -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) diff --git a/mtags/src/main/scala-2/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala b/mtags/src/main/scala-2/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala new file mode 100644 index 00000000000..3ef940f6cf6 --- /dev/null +++ b/mtags/src/main/scala-2/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala @@ -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) == '*' + } + +} diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala index 224be28a429..7d0b823b5c9 100644 --- a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/Completions.scala @@ -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) diff --git a/mtags/src/main/scala-3/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala new file mode 100644 index 00000000000..4ad48474c5a --- /dev/null +++ b/mtags/src/main/scala-3/scala/meta/internal/pc/completions/MultilineCommentCompletion.scala @@ -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) == '*' diff --git a/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala b/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala index 85836720ae0..b6548f7e06a 100644 --- a/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala +++ b/tests/cross/src/test/scala/tests/pc/CompletionSuite.scala @@ -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, + ) + }