Skip to content

Commit

Permalink
Configure scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jvican committed May 3, 2017
1 parent 10d0051 commit b55f23c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
maxColumn = 100
project.git = true
project.excludeFilters = [ /sbt-test/, /input_sources/, /contraband-scala/ ]

# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style.
# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala
docstrings = JavaDoc

# This also seems more idiomatic to include whitespace in import x.{ yyy }
spaces.inImportCurlyBraces = true

# This works around sequence wildcard (`_*`) turning into `_ *`
spaces.beforeSeqWildcard = true

# Vertical alignment only => for pattern matching
align.tokens.add = [
{ code = "=>", owner = "Case" }
]
25 changes: 24 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ def internalPath = file("internal")

lazy val compilerBridgeScalaVersions = Seq(scala212, scala211, scala210)

val scalafmtCheck = Command.command("scalafmtCheck") { state =>
sys.process.Process("git diff --name-only --exit-code").! match {
case 0 => // ok
case x => sys.error("git diff detected! Did you compile before committing?")
}
state
}

def commonSettings: Seq[Setting[_]] = Seq(
scalaVersion := scala212,
// publishArtifact in packageDoc := false,
Expand All @@ -24,7 +32,7 @@ def commonSettings: Seq[Setting[_]] = Seq(
crossScalaVersions := Seq(scala211, scala212),
mimaPreviousArtifacts := Set(), // Some(organization.value %% moduleName.value % "1.0.0"),
publishArtifact in Test := false,
commands ++= Seq(publishBridgesAndTest, publishBridges, crossTestBridges)
commands ++= Seq(publishBridgesAndTest, publishBridges, crossTestBridges, scalafmtCheck)
)

def relaxNon212: Seq[Setting[_]] = Seq(
Expand Down Expand Up @@ -75,6 +83,7 @@ def altPublishSettings: Seq[Setting[_]] = Seq(
})

lazy val zincRoot: Project = (project in file(".")).
disablePlugins(com.typesafe.sbt.SbtScalariform).
// configs(Sxr.sxrConf).
aggregate(
zinc,
Expand Down Expand Up @@ -137,6 +146,7 @@ lazy val zincRoot: Project = (project in file(".")).
)

lazy val zinc = (project in file("zinc")).
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zincCore, zincPersist, zincCompileCore,
zincClassfile, zincIvyIntegration % "compile->compile;test->test",
zincTesting % Test).
Expand All @@ -146,6 +156,7 @@ lazy val zinc = (project in file("zinc")).
)

lazy val zincTesting = (project in internalPath / "zinc-testing").
disablePlugins(com.typesafe.sbt.SbtScalariform).
settings(
minimalSettings,
name := "zinc Testing",
Expand All @@ -159,6 +170,7 @@ lazy val zincTesting = (project in internalPath / "zinc-testing").
configure(addSbtLm, addSbtUtilTesting)

lazy val zincCompile = (project in file("zinc-compile")).
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zincCompileCore, zincCompileCore % "test->test").
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -168,6 +180,7 @@ lazy val zincCompile = (project in file("zinc-compile")).

// Persists the incremental data structures using SBinary
lazy val zincPersist = (project in internalPath / "zinc-persist").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zincCore, zincCore % "test->test").
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -178,6 +191,7 @@ lazy val zincPersist = (project in internalPath / "zinc-persist").
// Implements the core functionality of detecting and propagating changes incrementally.
// Defines the data structures for representing file fingerprints and relationships and the overall source analysis
lazy val zincCore = (project in internalPath / "zinc-core").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zincApiInfo, zincClasspath, compilerInterface, compilerBridge % Test).
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -193,6 +207,7 @@ lazy val zincCore = (project in internalPath / "zinc-core").
configure(addSbtIO, addSbtUtilLogging, addSbtUtilRelation)

lazy val zincBenchmarks = (project in internalPath / "zinc-benchmarks").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface % "compile->compile;compile->test").
dependsOn(compilerBridge, zincCore, zincTesting % Test).
enablePlugins(JmhPlugin).
Expand All @@ -209,6 +224,7 @@ lazy val zincBenchmarks = (project in internalPath / "zinc-benchmarks").
)

lazy val zincIvyIntegration = (project in internalPath / "zinc-ivy-integration").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zincCompileCore, zincTesting % Test).
settings(
baseSettings,
Expand All @@ -218,6 +234,7 @@ lazy val zincIvyIntegration = (project in internalPath / "zinc-ivy-integration")

// sbt-side interface to compiler. Calls compiler-side interface reflectively
lazy val zincCompileCore = (project in internalPath / "zinc-compile-core").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface % "compile;test->test", zincClasspath, zincApiInfo, zincClassfile, zincTesting % Test).
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -232,6 +249,7 @@ lazy val zincCompileCore = (project in internalPath / "zinc-compile-core").
// format from which Java sources are generated by the sbt-contraband plugin.
lazy val compilerInterface = (project in internalPath / "compiler-interface").
enablePlugins(ContrabandPlugin).
disablePlugins(com.typesafe.sbt.SbtScalariform).
settings(
minimalSettings,
// javaOnlySettings,
Expand All @@ -252,6 +270,7 @@ lazy val compilerInterface = (project in internalPath / "compiler-interface").
// Compiler-side interface to compiler that is compiled against the compiler being used either in advance or on the fly.
// Includes API and Analyzer phases that extract source API and relationships.
lazy val compilerBridge: Project = (project in internalPath / "compiler-bridge").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface % "compile;test->test", /*launchProj % "test->test",*/ zincApiInfo % "test->test").
settings(
baseSettings,
Expand Down Expand Up @@ -284,6 +303,7 @@ def inBoth(ss: Setting[_]*): Seq[Setting[_]] = Seq(Compile, Test) flatMap (inCon
// defines operations on the API of a source, including determining whether it has changed and converting it to a string
// and discovery of Projclasses and annotations
lazy val zincApiInfo = (project in internalPath / "zinc-apiinfo").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface, zincClassfile % "compile;test->test").
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -294,6 +314,7 @@ lazy val zincApiInfo = (project in internalPath / "zinc-apiinfo").

// Utilities related to reflection, managing Scala versions, and custom class loaders
lazy val zincClasspath = (project in internalPath / "zinc-classpath").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface).
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -307,6 +328,7 @@ lazy val zincClasspath = (project in internalPath / "zinc-classpath").

// class file reader and analyzer
lazy val zincClassfile = (project in internalPath / "zinc-classfile").
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(compilerInterface % "compile;test->test").
configure(addBaseSettingsAndTestDeps).
settings(
Expand All @@ -319,6 +341,7 @@ lazy val zincClassfile = (project in internalPath / "zinc-classfile").
// re-implementation of scripted engine
lazy val zincScripted = (project in internalPath / "zinc-scripted").
enablePlugins(ContrabandPlugin, JsonCodecPlugin).
disablePlugins(com.typesafe.sbt.SbtScalariform).
dependsOn(zinc, zincIvyIntegration % "test->test").
settings(
minimalSettings,
Expand Down
66 changes: 66 additions & 0 deletions project/AutomateScalafmtPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import org.scalafmt.bootstrap.ScalafmtBootstrap
import org.scalafmt.sbt.ScalafmtPlugin
import sbt._
import sbt.Keys._
import sbt.inc.Analysis

// Taken from https://github.com/akka/alpakka/blob/master/project/AutomateScalafmtPlugin.scala
object AutomateScalafmtPlugin extends AutoPlugin {
object autoImport {
def automateScalafmtFor(configurations: Configuration*): Seq[Setting[_]] =
configurations.flatMap { c =>
inConfig(c)(
Seq(
compileInputs.in(compile) := {
scalafmtInc.value
compileInputs.in(compile).value
},
sourceDirectories.in(scalafmtInc) := Seq(scalaSource.value),
scalafmtInc := {
val cache = streams.value.cacheDirectory / "scalafmt"
val include = includeFilter.in(scalafmtInc).value
val exclude = excludeFilter.in(scalafmtInc).value
val sources =
sourceDirectories
.in(scalafmtInc)
.value
.descendantsExcept(include, exclude)
.get
.toSet
def format(handler: Set[File] => Unit, msg: String) = {
def update(handler: Set[File] => Unit, msg: String)(in: ChangeReport[File],
out: ChangeReport[File]) = {
val label = Reference.display(thisProjectRef.value)
val files = in.modified -- in.removed
Analysis
.counted("Scala source", "", "s", files.size)
.foreach(count => streams.value.log.info(s"$msg $count in $label ..."))
handler(files)
files
}
FileFunction.cached(cache)(FilesInfo.hash, FilesInfo.exists)(update(handler, msg))(
sources
)
}
def formattingHandler(files: Set[File]) =
if (files.nonEmpty) {
val filesArg = files.map(_.getAbsolutePath).mkString(",")
ScalafmtBootstrap.main(List("--quiet", "-i", "-f", filesArg))
}
format(formattingHandler, "Formatting")
format(_ => (), "Reformatted") // Recalculate the cache
}
)
)
}
}

private val scalafmtInc = taskKey[Unit]("Incrementally format modified sources")

override def requires = ScalafmtPlugin

override def trigger = allRequirements

override def projectSettings =
(includeFilter.in(scalafmtInc) := "*.scala") +: autoImport.automateScalafmtFor(Compile, Test)
}
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.2.21")
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.6.8")

0 comments on commit b55f23c

Please sign in to comment.