Skip to content

Commit

Permalink
improvement: Add option to create config in .metals
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Oct 26, 2023
1 parent 2cae1fe commit 7aaea84
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import scala.meta.io.RelativePath
object Directories {
def database: RelativePath =
RelativePath(".metals").resolve("metals.h2.db")
def hiddenScalafmt: RelativePath =
RelativePath(".metals").resolve(".scalafmt.conf")
def readonly: RelativePath =
RelativePath(".metals").resolve("readonly")
def tmp: RelativePath =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ final class FormattingProvider(
scalafmtConf(projectRoot) match {
case None =>
handleMissingFile(projectRoot).map {
case true =>
runFormat(path, projectRoot, input).asJava
case false =>
case Some(conf) =>
runFormat(path, conf, input).asJava
case None =>
Collections.emptyList[l.TextEdit]()
}
case Some(config) =>
Expand All @@ -132,7 +132,7 @@ final class FormattingProvider(
// before returning future.
promise.future.map {
case true if !token.isCancelled =>
runFormat(path, projectRoot, input).asJava
runFormat(path, config, input).asJava
case _ => result.asJava
}
case None =>
Expand Down Expand Up @@ -215,29 +215,37 @@ final class FormattingProvider(
} else Future.successful(None)
}

private def handleMissingFile(projectRoot: AbsolutePath): Future[Boolean] = {
private def handleMissingFile(
projectRoot: AbsolutePath
): Future[Option[AbsolutePath]] = {
if (!tables.dismissedNotifications.CreateScalafmtFile.isDismissed) {
val params = MissingScalafmtConf.params()
client.showMessageRequest(params).asScala.map { item =>
if (item == MissingScalafmtConf.createFile) {
if (
item == MissingScalafmtConf.createFile || item == MissingScalafmtConf.runDefaults
) {
val toWrite =
if (item == MissingScalafmtConf.createFile)
projectRoot.resolve(defaultScalafmtLocation)
else projectRoot.resolve(Directories.hiddenScalafmt)
Files
.write(
projectRoot.resolve(defaultScalafmtLocation).toNIO,
toWrite.toNIO,
initialConfig().getBytes(StandardCharsets.UTF_8),
)
client.showMessage(MissingScalafmtConf.fixedParams(isCancelled))
true
Some(toWrite)
} else if (item == Messages.notNow) {
tables.dismissedNotifications.CreateScalafmtFile
.dismiss(24, TimeUnit.HOURS)
false
None
} else if (item == Messages.dontShowAgain) {
tables.dismissedNotifications.CreateScalafmtFile
.dismissForever()
false
} else false
None
} else None
}
} else Future.successful(false)
} else Future.successful(None)
}

private def initialConfig(): String = {
Expand Down Expand Up @@ -424,8 +432,10 @@ final class FormattingProvider(
val defaultLocation = projectRoot.resolve(defaultScalafmtLocation)
lazy val scalacliDefault =
projectRoot.resolve(".scala-build/.scalafmt.conf")
lazy val hiddenDefault = projectRoot.resolve(Directories.hiddenScalafmt)
if (defaultLocation.exists) Some(defaultLocation)
else if (scalacliDefault.exists) Some(scalacliDefault)
else if (hiddenDefault.exists) Some(hiddenDefault)
else None
}
configpath.orElse(default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,7 @@ object Messages {
else ""

def createFile = new MessageActionItem("Create .scalafmt.conf")
def runDefaults = new MessageActionItem("Run with defaults")

def fixedParams(isAgain: Boolean): MessageParams =
new MessageParams(
Expand All @@ -636,6 +637,7 @@ object Messages {
params.setActions(
List(
createFile,
runDefaults,
notNow,
dontShowAgain,
).asJava
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/src/test/scala/tests/FormattingLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@ class FormattingLspSuite extends BaseLspSuite("formatting") {
} yield ()
}

test("initial-config-hidden") {
cleanWorkspace()
client.showMessageRequestHandler = { params =>
val expected = MissingScalafmtConf.createScalafmtConfMessage
if (params.getMessage() == expected) {
params.getActions().asScala.find(_ == MissingScalafmtConf.runDefaults)
} else None
}
for {
_ <- initialize(
s"""|/metals.json
|{"a":{"scalaVersion" : ${V.scala213}}}
|/a/src/main/scala/a/Main.scala
|object FormatMe {
| val x = 1 }
|""".stripMargin,
expectError = true,
)
_ <- server.didOpen("a/src/main/scala/a/Main.scala")
_ <- server.formatting("a/src/main/scala/a/Main.scala")
_ = assertNoDiff(
server.textContents(".metals/.scalafmt.conf"),
s"""|version = "${V.scalafmtVersion}"
|runner.dialect = scala213
|""".stripMargin,
)
} yield ()
}

test("custom-config-path") {
for {
_ <- initialize(
Expand Down

0 comments on commit 7aaea84

Please sign in to comment.