From e4d38f92a4a087c8008dd073abb2aead34070e66 Mon Sep 17 00:00:00 2001 From: Patrik Nordwall Date: Sat, 9 Feb 2019 14:21:16 +0100 Subject: [PATCH] Switch to Scalafmt in build * AvoidInfix in scalafmt.conf * align.tokens in scalafmt.conf * scalafmt in MultiJvm * sbt-scalafmt 2.0.0-RC5 * scalafmt in all* command alias * scalafmtAll in CONTRIBUTING.md --- .scalafmt.conf | 48 ++++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 12 +++++++++- build.sbt | 4 ++-- project/AkkaBuild.scala | 2 +- project/Formatting.scala | 29 ------------------------ project/MultiNode.scala | 7 +++--- project/plugins.sbt | 2 +- 7 files changed, 66 insertions(+), 38 deletions(-) create mode 100644 .scalafmt.conf delete mode 100644 project/Formatting.scala diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000000..3bbd512b66 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,48 @@ +version = 2.0.0-RC5 + +style = defaultWithAlign + +danglingParentheses = true +docstrings = JavaDoc +indentOperator = spray +maxColumn = 120 +rewrite.rules = [RedundantParens, SortImports, AvoidInfix] +unindentTopLevelOperators = true +align.tokens = [{code = "=>", owner = "Case"}] +align.openParenDefnSite = true +align.openParenCallSite = true +optIn.breakChainOnFirstMethodDot = false +optIn.configStyleArguments = false +danglingParentheses = false +spaces.inImportCurlyBraces = true +rewrite.neverInfix.excludeFilters = [ + and + min + max + until + to + by + eq + ne + "should.*" + "contain.*" + "must.*" + in + ignore + be + taggedAs + thrownBy + synchronized + have + when + size + only + noneOf + oneElementOf + noElementsOf + atLeastOneElementOf + atMostOneElementOf + allElementsOf + inOrderElementsOf + theSameElementsAs +] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 324e0a5cc2..558c2f59eb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -147,6 +147,13 @@ project akka-cluster multi-jvm:testOnly akka.cluster.SunnyWeather ``` +To format the Scala source code: +``` +sbt +akka-cluster/scalafmtAll +akka-persistence/scalafmtAll +``` + ### Do not use `-optimize` Scala compiler flag Akka has not been compiled or tested with `-optimize` Scala compiler flag. (In sbt, you can specify compiler options in the `scalacOptions` key.) @@ -360,7 +367,10 @@ In such situations we prefer 'internal' over 'impl' as a package name. ### Scala style -Akka uses [Scalariform](https://github.com/daniel-trinh/scalariform) to enforce some of the code style rules. +Akka uses [Scalafmt](https://scalameta.org/scalafmt/docs/installation.html) to enforce some of the code style rules. + +When IntelliJ detects the `.scalafmt.conf` and promts "Scalafmt configuration detected in this project" you should +select "Continue using IntelliJ formatter" and instead install the [Scalafmt IntelliJ plugin](https://scalameta.org/scalafmt/docs/installation.html#intellij). Install the nightly plugin (until version 2.0.0 or later becomes stable) and enable "Format on save". ### Java style diff --git a/build.sbt b/build.sbt index 000467fcc8..79af1c6658 100644 --- a/build.sbt +++ b/build.sbt @@ -524,7 +524,6 @@ def akkaModule(name: String): Project = Project(id = name, base = file(name)) .settings(akka.AkkaBuild.buildSettings) .settings(akka.AkkaBuild.defaultSettings) - .settings(akka.Formatting.formatSettings) .enablePlugins(BootstrapGenjavadoc) /* Command aliases one can run locally against a module @@ -534,7 +533,8 @@ def akkaModule(name: String): Project = def commandValue(p: Project, externalTest: Option[Project] = None) = { val test = externalTest.getOrElse(p) val optionalMima = if (p.id.endsWith("-typed")) "" else s";${p.id}/mimaReportBinaryIssues" - s";${test.id}/test:compile$optionalMima;${docs.id}/paradox" + val optionalExternalTestFormat = externalTest.map(t => s";${t.id}/scalafmtAll").getOrElse("") + s";${p.id}/scalafmtAll$optionalExternalTestFormat;${test.id}/test:compile$optionalMima;${docs.id}/paradox" } addCommandAlias("allActor", commandValue(actor, Some(actorTests))) addCommandAlias("allRemote", commandValue(remote, Some(remoteTests))) diff --git a/project/AkkaBuild.scala b/project/AkkaBuild.scala index 49e394f16d..8ae41f0c69 100644 --- a/project/AkkaBuild.scala +++ b/project/AkkaBuild.scala @@ -9,6 +9,7 @@ import java.util.Properties import sbt.Keys._ import sbt._ +import org.scalafmt.sbt.ScalafmtPlugin.autoImport._ import scala.collection.breakOut @@ -25,7 +26,6 @@ object AkkaBuild { lazy val rootSettings = Release.settings ++ UnidocRoot.akkaSettings ++ - Formatting.formatSettings ++ Protobuf.settings ++ Seq( parallelExecution in GlobalScope := System.getProperty("akka.parallelExecution", parallelExecutionByDefault.toString).toBoolean, version in ThisBuild := "2.5-SNAPSHOT" diff --git a/project/Formatting.scala b/project/Formatting.scala deleted file mode 100644 index b9d51c4344..0000000000 --- a/project/Formatting.scala +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2016-2019 Lightbend Inc. - */ - -package akka - -import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys.MultiJvm -import com.typesafe.sbt.SbtScalariform.ScalariformKeys -import sbt._ - -object Formatting { - import scalariform.formatter.preferences._ - - lazy val formatSettings = Seq( - ScalariformKeys.preferences := setPreferences(ScalariformKeys.preferences.value), - ScalariformKeys.preferences in Compile := setPreferences(ScalariformKeys.preferences.value), - ScalariformKeys.preferences in Test := setPreferences(ScalariformKeys.preferences.value), - ScalariformKeys.preferences in MultiJvm := setPreferences(ScalariformKeys.preferences.value) - ) - - def setPreferences(preferences: IFormattingPreferences) = preferences - .setPreference(RewriteArrowSymbols, true) - .setPreference(AlignParameters, true) - .setPreference(AlignSingleLineCaseStatements, true) - .setPreference(DoubleIndentConstructorArguments, false) - .setPreference(DoubleIndentMethodDeclaration, false) - .setPreference(DanglingCloseParenthesis, Preserve) - .setPreference(NewlineAtEndOfFile, true) -} diff --git a/project/MultiNode.scala b/project/MultiNode.scala index 8ec076fd25..3fb12dbfa6 100644 --- a/project/MultiNode.scala +++ b/project/MultiNode.scala @@ -6,12 +6,12 @@ package akka import akka.TestExtras.Filter.Keys._ import com.typesafe.sbt.MultiJvmPlugin.MultiJvmKeys.multiJvmCreateLogger -import com.typesafe.sbt.{SbtMultiJvm, SbtScalariform} +import com.typesafe.sbt.SbtMultiJvm import com.typesafe.sbt.SbtMultiJvm.MultiJvmKeys._ -import com.typesafe.sbt.SbtScalariform.ScalariformKeys import sbt.{ Def, _ } import sbt.Keys._ import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport._ +import org.scalafmt.sbt.ScalafmtPlugin.scalafmtConfigSettings object MultiNode extends AutoPlugin { @@ -57,10 +57,9 @@ object MultiNode extends AutoPlugin { private val multiJvmSettings = SbtMultiJvm.multiJvmSettings ++ - inConfig(MultiJvm)(SbtScalariform.configScalariformSettings) ++ + inConfig(MultiJvm)(scalafmtConfigSettings) ++ Seq( jvmOptions in MultiJvm := defaultMultiJvmOptions, - compileInputs in (MultiJvm, compile) := ((compileInputs in (MultiJvm, compile)) dependsOn (ScalariformKeys.format in MultiJvm)).value, scalacOptions in MultiJvm := (scalacOptions in Test).value, logLevel in multiJvmCreateLogger := Level.Debug, // to see ssh establishment multiJvmCreateLogger in MultiJvm := { // to use normal sbt logging infra instead of custom sbt-multijvm-one diff --git a/project/plugins.sbt b/project/plugins.sbt index e83c36605f..f5db56edf1 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,7 +4,7 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-multi-jvm" % "0.4.0") //#sbt-multi-jvm addSbtPlugin("com.lightbend.sbt" % "sbt-java-formatter" % "0.4.1") -addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.2") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0-RC5") addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.1") addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.9.4") addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.3.0")