How to format the sbt build files with scalariform automatically?

后端 未结 1 1453
失恋的感觉
失恋的感觉 2021-01-02 00:52

I\'d like to format my sbt build files with scalariform. Is it possible to run scalariform on the build definition itself when starting sbt?

相关标签:
1条回答
  • 2021-01-02 01:09

    Put this in project/scalariform.sbt:

    addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.2.0")
    

    And this in scalariform.sbt:

    import scalariform.formatter.preferences._
    import ScalariformKeys._
    
    lazy val BuildConfig = config("build") extend Compile
    lazy val BuildSbtConfig = config("buildsbt") extend Compile
    
    val foo: Int => Int = {
      case 1 => 1
      case     2 => 2
    }
    
    noConfigScalariformSettings
    
    inConfig(BuildConfig)(configScalariformSettings)
    
    inConfig(BuildSbtConfig)(configScalariformSettings)
    
    scalaSource in BuildConfig := baseDirectory.value / "project"
    
    scalaSource in BuildSbtConfig := baseDirectory.value
    
    includeFilter in (BuildConfig, format) := ("*.scala": FileFilter)
    
    includeFilter in (BuildSbtConfig, format) := ("*.sbt": FileFilter)
    
    format in BuildConfig := {
      val x = (format in BuildSbtConfig).value
      (format in BuildConfig).value
    }
    
    preferences := preferences.value.
      setPreference(AlignSingleLineCaseStatements, true).
      setPreference(AlignParameters, true)
    

    Run:

    helloworld> build:scalariformFormat
    [info] Formatting 1 Scala source {file:/Users/eed3si9n/work/helloworld/}helloworld(buildsbt) ...
    [info] Reformatted 1 Scala source {file:/Users/eed3si9n/work/helloworld/}helloworld(buildsbt).
    [success] Total time: 1 s, completed Oct 5, 2013 2:58:45 PM
    

    Magic!

    val foo: Int => Int = {
      case 1 => 1
      case 2 => 2
    }
    
    0 讨论(0)
提交回复
热议问题