Skip to content

Commit

Permalink
ximprovement: Add toString UserConfiguration
Browse files Browse the repository at this point in the history
Will be useful in the inlay hints worksheet PR for sending configuration, since we always need user configuration to be sent.
  • Loading branch information
tgodzik committed Nov 26, 2024
1 parent b55bf8f commit d42536c
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class ScalaVersions(
private val _isSupportedScalaVersion: Set[String] =
supportedScalaVersions.toSet

def isSupportedAtReleaseMomentScalaVersion(version: String): Boolean =
_isSupportedScalaVersion(dropVendorSuffix(version))
def isSupportedAtReleaseMomentScalaVersion(version: String): Boolean = {
val versionWithoutVendorSuffix = dropVendorSuffix(version)
SemVer.isLaterVersion("3.3.3", versionWithoutVendorSuffix) ||
_isSupportedScalaVersion(versionWithoutVendorSuffix)
}

def isDeprecatedScalaVersion(version: String): Boolean =
_isDeprecatedScalaVersion(dropVendorSuffix(version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import scala.util.Failure
import scala.util.Success
import scala.util.Try

import scala.meta.internal.jdk.CollectionConverters._
import scala.meta.internal.metals.JsonParser.XtensionSerializedAsOption
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.mtags.Symbol
import scala.meta.io.AbsolutePath
import scala.meta.pc.PresentationCompilerConfig

import com.google.gson.GsonBuilder
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
Expand Down Expand Up @@ -58,6 +59,95 @@ case class UserConfiguration(
defaultBspToBuildTool: Boolean = false,
) {

override def toString(): String = {
def mapField[K, T](
key: String,
opts: Map[K, T],
): Option[(String, java.util.Map[String, String])] = {
val serializable = opts.map { case (k, v) =>
(k.toString(), v.toString())
}
Some((key, serializable.asJava))
}

def listField[T](key: String, list: Option[List[String]]) = {
list match {
case None => None
case Some(value) => Some(key -> value.asJava)
}
}

def optStringField(
key: String,
value: Option[Any],
): Option[(String, Any)] =
value match {
case None => None
case Some(value) => Some(key -> value.toString)
}

val fields = List(
optStringField("javaHome", javaHome),
optStringField("sbtScript", sbtScript),
optStringField("gradleScript", gradleScript),
optStringField("mavenScript", mavenScript),
optStringField("millScript", millScript),
optStringField("scalafmtConfigPath", scalafmtConfigPath),
optStringField("scalafixConfigPath", scalafixConfigPath),
optStringField("scalafixConfigPath", scalafixConfigPath),
mapField("symbolPrefixes", symbolPrefixes),
Some(("worksheetScreenWidth", worksheetScreenWidth)),
Some(("worksheetCancelTimeout", worksheetCancelTimeout)),
Some(("bloopSbtAlreadyInstalled", bloopSbtAlreadyInstalled)),
optStringField("bloopVersion", bloopVersion),
listField("bloopJvmProperties", bloopJvmProperties),
listField("ammoniteJvmProperties", ammoniteJvmProperties),
Some(("superMethodLensesEnabled", superMethodLensesEnabled)),
mapField("inlayHintsOptions", inlayHintsOptions.options),
Some(
(
"enableStripMarginOnTypeFormatting",
enableStripMarginOnTypeFormatting,
)
),
Some(("enableIndentOnPaste", enableIndentOnPaste)),
Some(
(
"enableSemanticHighlighting",
enableSemanticHighlighting,
)
),
listField("excludedPackages", excludedPackages),
optStringField("fallbackScalaVersion", fallbackScalaVersion),
Some("testUserInterface" -> testUserInterface.toString()),
javaFormatConfig.map(value =>
"javaFormat" -> List(
Some("eclipseConfigPath" -> value.eclipseFormatConfigPath.toString()),
value.eclipseFormatProfile.map("eclipseProfile" -> _),
).flatten.toMap.asJava
),
listField(
"scalafixRulesDependencies",
Some(scalafixRulesDependencies),
),
optStringField("customProjectRoot", customProjectRoot),
Some(("verboseCompilation", verboseCompilation)),
Some(
"autoImportBuilds" ->
automaticImportBuild.toString().toLowerCase()
),
optStringField("scalaCliLauncher", scalaCliLauncher),
Some(
(
"defaultBspToBuildTool",
defaultBspToBuildTool,
)
),
).flatten.toMap.asJava
val gson = new GsonBuilder().setPrettyPrinting().create()
gson.toJson(fields).toString()
}

def shouldAutoImportNewProject: Boolean =
automaticImportBuild != AutoImportBuildKind.Off

Expand Down Expand Up @@ -697,13 +787,17 @@ object UserConfiguration {

sealed trait TestUserInterfaceKind
object TestUserInterfaceKind {
object CodeLenses extends TestUserInterfaceKind
object TestExplorer extends TestUserInterfaceKind
object CodeLenses extends TestUserInterfaceKind {
override def toString: String = "code lenses"
}
object TestExplorer extends TestUserInterfaceKind {
override def toString: String = "test explorer"
}
}

sealed trait AutoImportBuildKind
object AutoImportBuildKind {
object Off extends AutoImportBuildKind
object Initial extends AutoImportBuildKind
object All extends AutoImportBuildKind
case object Off extends AutoImportBuildKind
case object Initial extends AutoImportBuildKind
case object All extends AutoImportBuildKind
}
2 changes: 1 addition & 1 deletion tests/slow/src/test/scala/tests/sbt/SbtServerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class SbtServerSuite
code <- startSbtServer()
_ = assert(code == 0)
_ = assert(workspace.resolve(".bsp/sbt.json").exists)
_ <- initializer.initialize(workspace, server, client, false)
_ <- initializer.initialize(workspace, server, client, false, userConfig)
_ <- server.initialized()
} yield {
// should not contain the 'Navigation will not work for this build due to mis-configuration.' message
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/src/main/scala/tests/BaseLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ abstract class BaseLspSuite(
): Future[InitializeResult] = {
Debug.printEnclosing()
writeLayout(layout)
initializer.initialize(workspace, server, client, expectError)
initializer.initialize(workspace, server, client, expectError, userConfig)
}

def initialize(
Expand All @@ -137,6 +137,7 @@ abstract class BaseLspSuite(
server,
client,
expectError,
userConfig,
Some(layout.keys.toList),
)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/src/main/scala/tests/BuildServerInitializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import scala.meta.internal.metals.Messages.GenerateBspAndConnect
import scala.meta.internal.metals.Messages.ImportBuild
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ServerCommands
import scala.meta.internal.metals.UserConfiguration
import scala.meta.internal.metals.{BuildInfo => V}
import scala.meta.io.AbsolutePath

Expand All @@ -25,6 +26,7 @@ sealed trait BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult]
}
Expand All @@ -40,6 +42,7 @@ object QuickBuildInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
val foldersToInit =
Expand All @@ -51,6 +54,7 @@ object QuickBuildInitializer extends BuildServerInitializer {
for {
initializeResult <- server.initialize(workspaceFolders)
_ <- server.initialized()
_ <- server.didChangeConfiguration(userConfig.toString)
} yield {
if (!expectError) {
server.assertBuildServerConnection()
Expand All @@ -71,13 +75,15 @@ object BloopImportInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
for {
initializeResult <- server.initialize(workspaceFolders)
// Import build using Bloop
_ = client.importBuild = ImportBuild.yes
_ <- server.initialized()
_ <- server.didChangeConfiguration(userConfig.toString())
} yield {
if (!expectError) {
server.assertBuildServerConnection()
Expand All @@ -99,6 +105,7 @@ object SbtServerInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
val paths = workspaceFolders match {
Expand All @@ -123,6 +130,7 @@ object SbtServerInitializer extends BuildServerInitializer {
)
}
}
_ <- server.didChangeConfiguration(userConfig.toString)
_ <- paths.zipWithIndex.foldLeft(Future.successful(())) {
case (future, (_, i)) =>
future.flatMap { _ =>
Expand Down Expand Up @@ -178,11 +186,13 @@ object MillServerInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
for {
initializeResult <- server.initialize()
_ <- server.initialized()
_ <- server.didChangeConfiguration(userConfig.toString)
// choose mill-bsp as the Bsp Server
_ = client.selectBspServer = { _ => new MessageActionItem("mill-bsp") }
_ <- server.executeCommand(ServerCommands.BspSwitch)
Expand All @@ -202,13 +212,15 @@ object BazelServerInitializer extends BuildServerInitializer {
server: TestingServer,
client: TestingClient,
expectError: Boolean,
userConfig: UserConfiguration,
workspaceFolders: Option[List[String]] = None,
)(implicit ec: ExecutionContext): Future[InitializeResult] = {
for {
initializeResult <- server.initialize()
// Import build using Bazel
_ = client.generateBspAndConnect = GenerateBspAndConnect.yes
_ <- server.initialized()
_ <- server.didChangeConfiguration(userConfig.toString)
} yield {
if (!expectError) {
server.assertBuildServerConnection()
Expand Down
Loading

0 comments on commit d42536c

Please sign in to comment.