0

I have the following piece of code:

package my.package

import sbt._
import Keys._

object OpenElectronsScalaFmtPlugin extends AutoPlugin {
  override def trigger = allRequirements
  override def requires = plugins.JvmPlugin
  override def buildSettings: Seq[Def.Setting[_]] = {
    SettingKey[Unit]("scalafmtGenerateConfig") :=
      IO.write(
        // writes to file once when build is loaded
        file(".scalafmt-common.conf"),
        ("version = 3.6.1\n" +
          "runner.dialect = scala213source3\n\n" +
          "project.git = true\n" +
          "preset = default\n\n" +
          "align.preset = none\n" +
          "align.tokens = [\n" +
          "  {code = \"<-\"},\n" +
          "]\n\n" +
          "docstrings.style = keep\n" +
          "maxColumn = 120\n\n"
          ).stripMargin.getBytes("UTF-8")
      )
  }
}

As you can see, I'm appending the entries one by one which I find tedious. My IDE is not helping me to look into the IO.write function where I want to know what the parameters are so that I can pass in a .scalafmt-common.conf as a file instead of individual elements. Any ideas?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
joesan
  • 13,963
  • 27
  • 95
  • 232
  • 1
    For the reference, beginning was here: https://stackoverflow.com/questions/74964734/reuse-scalafmt-config-file-across-projects https://stackoverflow.com/questions/74965503/sbt-plugin-published-locally-could-not-resolve-proper-scala-version https://stackoverflow.com/questions/74965732/sbt-plugin-published-locally-fails-with-an-error-when-included-in-a-project – Dmytro Mitin Dec 30 '22 at 22:51
  • I never tried that for an sbt plugin, but: I'd try to place the original file in the resources folder of the plugin project (so that it gets bundled into the jar) and then make the plugin implementation copy it from there. – MartinHH Dec 31 '22 at 07:57
  • For reference, the SBT documentation is available at https://www.scala-sbt.org/1.8.0/api/sbt/io/IO$.html#read(file:java.io.File,charset:java.nio.charset.Charset):String but if you use `IO.read` it will happen in the project using your plugin, not what you want. – Gaël J Dec 31 '22 at 08:27
  • Well, actually as @MartinHH suggested, if you package the file with the plugin, you can use `IO.read` or similar method targeting the classpath to get the file. – Gaël J Dec 31 '22 at 08:29

1 Answers1

0

Here is what I came up with:

val commonScalaFormatConfig: String =
    """
      |version = 3.6.1
      |runner.dialect = scala213source3
      |
      |project.git = true
      |preset = default
      |
      |align.preset = none
      |align.tokens = [
      |  {code = "<-"},
      |]
      |
      |docstrings.style = keep
      |maxColumn = 120
      |
      |rewrite.rules = [
      |  SortImports,
      |  AvoidInfix,
      |]
      |
      |spaces.inImportCurlyBraces = true
      |includeNoParensInSelectChains = false
      |trailingCommas = preserve
      |
      |continuationIndent {
      |  callSite = 2
      |  defnSite = 2
      |  extendSite = 2
      |}
      |
      |optIn {
      |  forceBlankLineBeforeDocstring = false
      |}
      |
      |newlines {
      |  source = keep
      |  afterCurlyLambdaParams = preserve
      |  beforeCurlyLambdaParams = multilineWithCaseOnly
      |  topLevelBodyIfMinStatements = []
      |}
      |""".stripMargin

  override def trigger = allRequirements
  override def requires = plugins.JvmPlugin
  override def buildSettings: Seq[Def.Setting[_]] = {
    SettingKey[Unit]("scalafmtGenerateConfig") :=
      IO.write(
        // writes to file once when build is loaded
        file(".scalafmt-common.conf"), commonScalaFormatConfig.stripMargin.getBytes("UTF-8")
      )
  }

Not quite what I expected, but atleast one step better than my original version.

The other approach would be to place the conf file in a certain location and use IO.copyFromFile(..., ...) to read it from there.

joesan
  • 13,963
  • 27
  • 95
  • 232