微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

解决方法

把它放在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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐