本文介绍了如何使用 scalariform 自动格式化 sbt 构建文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用 scalariform 格式化我的 sbt 构建文件.启动 sbt 时是否可以在构建定义本身上运行 scalariform?
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?
推荐答案
把这个放到 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
}
这篇关于如何使用 scalariform 自动格式化 sbt 构建文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!