Skip to content

Commit

Permalink
bugfix: Remove synthetic decorations for script wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk committed Nov 21, 2023
1 parent a137ae9 commit 439a694
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 13 deletions.
24 changes: 18 additions & 6 deletions metals/src/main/scala/scala/meta/internal/metals/Compilers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import scala.meta.pc.HoverSignature
import scala.meta.pc.OffsetParams
import scala.meta.pc.PresentationCompiler
import scala.meta.pc.SymbolSearch
import scala.meta.pc.SyntheticDecoration

import ch.epfl.scala.bsp4j.BuildTargetIdentifier
import ch.epfl.scala.bsp4j.CompileReport
Expand Down Expand Up @@ -573,6 +574,22 @@ class Compilers(
path.toNIO.toUri().toString(),
compiler.scalaVersion(),
)

def adjustDecorations(
decorations: ju.List[SyntheticDecoration]
): ju.List[DecorationOptions] = {
val withCorrectStart = decorations.asScala.dropWhile { d =>
val adjusted = adjust
.adjustPos(d.range().getStart(), adjustToZero = false)
adjusted.getLine() < 0 || adjusted.getCharacter() < 0
}
withCorrectStart.map { decoration =>
DecorationOptions(
decoration.label(),
adjust.adjustRange(decoration.range()),
)
}.asJava
}
val vFile =
CompilerVirtualFileParams(path.toNIO.toUri(), input.text, token)

Expand All @@ -588,12 +605,7 @@ class Compilers(
.syntheticDecorations(pcParams)
.asScala
.map { decorations =>
decorations.map { decoration =>
DecorationOptions(
decoration.label(),
adjust.adjustRange(decoration.range()),
)
}
adjustDecorations(decorations)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class PcSyntheticDecorationsProvider(
def tpdTree = unit.lastBody

def provide(): List[SyntheticDecoration] =
traverse(Synthetics.empty, tpdTree).decorations
traverse(Synthetics.empty, tpdTree).result()

def collectDecorations(
tree: Tree,
Expand Down Expand Up @@ -220,11 +220,22 @@ final class PcSyntheticDecorationsProvider(
def containsDef(offset: Int): Boolean = definitions(offset)
def add(decoration: Decoration, offset: Int): Synthetics =
copy(
decorations = decoration :: decorations,
decorations = addDecoration(decoration),
definitions = definitions + offset
)
def add(decoration: Decoration): Synthetics =
copy(decorations = decoration :: decorations)
copy(
decorations = addDecoration(decoration),
definitions = definitions
)
// If method has both type parameter and implicit parameter, we want the type parameter decoration to be displayed first,
// but it's added second. This method adds the decoration to the right position in the list.
private def addDecoration(decoration: Decoration): List[Decoration] = {
val atSamePos =
decorations.takeWhile(_.range.getStart() == decoration.range.getStart())
(atSamePos :+ decoration) ++ decorations.drop(atSamePos.size)
}
def result(): List[Decoration] = decorations.reverse
}

object Synthetics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ class PcSyntheticDecorationsProvider(
val unit = driver.currentCtx.run.units.head

def tpdTree = unit.tpdTree

def provide(): List[SyntheticDecoration] =
val deepFolder = DeepFolder[Synthetics](collectDecorations)
deepFolder(Synthetics.empty, tpdTree).decorations
deepFolder(Synthetics.empty, tpdTree).result()

def collectDecorations(
decorations: Synthetics,
Expand Down Expand Up @@ -255,11 +254,25 @@ case class Synthetics(
def containsDef(offset: Int) = definitions(offset)
def add(decoration: Decoration, offset: Int) =
copy(
decorations = decoration :: decorations,
decorations = addDecoration(decoration),
definitions = definitions + offset,
)
def add(decoration: Decoration) =
copy(decorations = decoration :: decorations)
copy(
decorations = addDecoration(decoration),
definitions = definitions,
)

// If method has both type parameter and implicit parameter, we want the type parameter decoration to be displayed first,
// but it's added second. This method adds the decoration to the right position in the list.
private def addDecoration(decoration: Decoration): List[Decoration] =
val atSamePos =
decorations.takeWhile(_.range.getStart() == decoration.range.getStart())
(atSamePos :+ decoration) ++ decorations.drop(atSamePos.size)

def result(): List[Decoration] = decorations.reverse

end Synthetics

object Synthetics:
def empty: Synthetics = Synthetics(Nil, Set.empty)
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,15 @@ class SyntheticDecorationsSuite extends BaseSyntheticDecorationsSuite {
|""".stripMargin
)

check(
"ord",
"""|object Main {
| val ordered = "acb".sorted
|}
|""".stripMargin,
"""|object Main {
| val ordered: String = augmentString("acb").sorted[Char](Char)
|}
|""".stripMargin
)
}
44 changes: 44 additions & 0 deletions tests/slow/src/test/scala/tests/scalacli/ScalaCliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests.scalacli
import scala.concurrent.Future

import scala.meta.internal.metals.FileOutOfScalaCliBspScope
import scala.meta.internal.metals.InitializationOptions
import scala.meta.internal.metals.Messages
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.MetalsServerConfig
Expand All @@ -17,6 +18,14 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
override def serverConfig: MetalsServerConfig =
MetalsServerConfig.default.copy(slowTask = SlowTaskConfig.on)

override protected def initializationOptions: Option[InitializationOptions] =
Some(
InitializationOptions.Default.copy(
inlineDecorationProvider = Some(true),
decorationProvider = Some(true),
)
)

private def simpleFileTest(useBsp: Boolean): Future[Unit] =
for {
_ <- scalaCliInitialize(useBsp)(simpleFileLayout)
Expand Down Expand Up @@ -169,6 +178,41 @@ class ScalaCliSuite extends BaseScalaCliSuite(V.scala3) {
|""".stripMargin,
)

_ <- server.didChangeConfiguration(
"""{
| "show-implicit-arguments": true,
| "show-implicit-conversions-and-classes": true,
| "show-inferred-type": true
|}
|""".stripMargin
)

_ <- server.didOpen("MyTests.sc")
_ = assertNoDiff(
client.syntheticDecorations,
s"""#!/usr/bin/env -S scala-cli shebang --java-opt -Xms256m --java-opt -XX:MaxRAMPercentage=80
|//> using scala "$scalaVersion"
|//> using lib "com.lihaoyi::utest::0.7.10"
|//> using lib com.lihaoyi::pprint::0.6.6
|
|import foo.Foo
|import utest._
|
|pprint.log[Int](2)(generate, generate) // top-level statement should be fine in a script
|
|object MyTests extends TestSuite {
| pprint.log[Int](2)(generate, generate)
| val tests: Tests = Tests {
| test("foo") {
| assert(2 + 2 == 4)
| }
| test("nope") {
| assert(2 + 2 == (new Foo).value)
| }
| }
|}
|""".stripMargin,
)
} yield ()

private val simpleFileLayout =
Expand Down

0 comments on commit 439a694

Please sign in to comment.