Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format the sbt build files with scalariform automatically?

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?

like image 598
Leif Wickland Avatar asked Oct 05 '13 18:10

Leif Wickland


1 Answers

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
}
like image 164
Eugene Yokota Avatar answered Sep 21 '22 06:09

Eugene Yokota