如何自动格式化sbt构建文件?

时间:2013-10-05 18:56:45

标签: scala sbt scalariform

我想用scalariform格式化我的sbt构建文件。在启动sbt时,是否可以在构建定义本身上运行标量?

1 个答案:

答案 0 :(得分:12)

将其放入project/scalariform.sbt

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

这是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)

执行命令

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

魔术!

val foo: Int => Int = {
  case 1 => 1
  case 2 => 2
}