Skip to content

Commit

Permalink
Merge pull request #1853 from tgodzik/add-ammonite-jvm-opts
Browse files Browse the repository at this point in the history
Add jvm parameters configuration for Ammonite
  • Loading branch information
tgodzik authored Jun 25, 2020
2 parents cc6038b + 260dcc2 commit ba810f2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ lazy val metals = project
"org.scalameta" %% "scalameta" % V.scalameta,
"org.scalameta" % "semanticdb-scalac-core" % V.scalameta cross CrossVersion.full,
// For starting Ammonite
"io.github.alexarchambault.ammonite" %% "ammonite-runner" % "0.2.5"
"io.github.alexarchambault.ammonite" %% "ammonite-runner" % "0.2.6"
),
buildInfoPackage := "scala.meta.internal.metals",
buildInfoKeys := Seq[BuildInfoKey](
Expand Down
21 changes: 21 additions & 0 deletions metals/src/main/scala/scala/meta/internal/metals/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ object Messages {
}
}

object AmmoniteJvmParametersChange {
def restart: MessageActionItem =
new MessageActionItem("Restart Ammonite")
def notNow: MessageActionItem =
new MessageActionItem("Not now")
def params(): ShowMessageRequestParams = {
val params = new ShowMessageRequestParams()
params.setMessage(
s"Ammonite JVM parameters have been updated, do you want to restart the ammonite BSP server? (the changes will only be picked up after the restart)"
)
params.setType(MessageType.Info)
params.setActions(
List(
restart,
notNow
).asJava
)
params
}
}

object IncompatibleBloopVersion {
def manually: MessageActionItem =
new MessageActionItem("I'll update manually")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import scala.meta.internal.builds.ShellRunner
import scala.meta.internal.implementation.ImplementationProvider
import scala.meta.internal.implementation.Supermethods
import scala.meta.internal.io.FileIO
import scala.meta.internal.metals.Messages.AmmoniteJvmParametersChange
import scala.meta.internal.metals.Messages.IncompatibleBloopVersion
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.ammonite.Ammonite
Expand Down Expand Up @@ -955,6 +956,24 @@ class MetalsLanguageServer(
case _ =>
Future.successful(())
}
} else if (
userConfig.ammoniteJvmProperties != old.ammoniteJvmProperties && buildTargets.allBuildTargetIds
.exists(Ammonite.isAmmBuildTarget)
) {
languageClient
.showMessageRequest(AmmoniteJvmParametersChange.params())
.asScala
.flatMap {
case item if item == AmmoniteJvmParametersChange.restart =>
ammonite
.stop()
.asScala
.flatMap { _ =>
ammonite.start()
}
case _ =>
Future.successful(())
}
} else {
Future.successful(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ case class UserConfiguration(
worksheetCancelTimeout: Int = 4,
bloopSbtAlreadyInstalled: Boolean = false,
bloopVersion: Option[String] = None,
ammoniteJvmProperties: Option[List[String]] = None,
superMethodLensesEnabled: Boolean = false,
remoteLanguageServer: Option[String] = None,
enableStripMarginOnTypeFormatting: Boolean = true
Expand Down Expand Up @@ -107,6 +108,15 @@ object UserConfiguration {
|separators (even on Windows).
|""".stripMargin
),
UserConfigurationOption(
"ammonite-jvm-properties",
"""`[]`.""",
"""["-Xmx1G"]""",
"Ammonite JVM Properties",
"""|Optional list of JVM properties to pass along to the Ammonite server.
|Each property needs to be a separate item.\n\nExample: `-Xmx1G` or `-Xms100M`"
|""".stripMargin
),
UserConfigurationOption(
"bloop-sbt-already-installed",
"false",
Expand Down Expand Up @@ -205,6 +215,29 @@ object UserConfiguration {
Some(value)
}
}

def getStringListKey(key: String): Option[List[String]] =
getKey[List[String]](
key,
{ elem =>
if (elem.isJsonArray()) {
val parsed = elem.getAsJsonArray().asScala.flatMap { value =>
Try(value.getAsJsonPrimitive().getAsString()) match {
case Failure(exception) =>
errors += s"json error: values in '$key' should have value of type string but obtained $value"
None
case Success(value) =>
Some(value)
}
}
Some(parsed.toList)
} else {
errors += s"json error: key '$key' should have value of type array but obtained $elem"
None
}
}
)

def getStringMap(key: String): Option[Map[String, String]] =
getKey(
key,
Expand Down Expand Up @@ -253,6 +286,7 @@ object UserConfiguration {
val worksheetCancelTimeout =
getIntKey("worksheet-cancel-timeout")
.getOrElse(default.worksheetCancelTimeout)
val ammoniteProperties = getStringListKey("ammonite-jvm-properties")
val bloopSbtAlreadyInstalled =
getBooleanKey("bloop-sbt-already-installed").getOrElse(false)
val bloopVersion =
Expand All @@ -277,6 +311,7 @@ object UserConfiguration {
worksheetCancelTimeout,
bloopSbtAlreadyInstalled,
bloopVersion,
ammoniteProperties,
superMethodLensesEnabled,
remoteLanguageServer,
enableStripMarginOnTypeFormatting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,20 @@ final class Ammonite(
case (command, script) =>
val extraScripts = buffers.open.toVector
.filter(path => path.isAmmoniteScript && path != script)
val jvmOpts = userConfig().ammoniteJvmProperties.getOrElse(Nil)
val commandWithJVMOpts =
command.addJvmArgs(jvmOpts: _*)
val futureConn = BuildServerConnection.fromSockets(
workspace(),
buildClient,
languageClient,
() =>
Ammonite
.socketConn(command, script +: extraScripts, workspace()),
.socketConn(
commandWithJVMOpts,
script +: extraScripts,
workspace()
),
tables().dismissedNotifications.ReconnectAmmonite,
config
)
Expand Down

0 comments on commit ba810f2

Please sign in to comment.