Skip to content

Commit

Permalink
Merge pull request #76 from godenji/1.8.1
Browse files Browse the repository at this point in the history
1.8.1 release
  • Loading branch information
godenji authored Oct 12, 2017
2 parents 48ecf23 + b069590 commit b37003c
Show file tree
Hide file tree
Showing 19 changed files with 195 additions and 21 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Installing
--------------------------

```
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.0")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.1")
```

Configuration (build.sbt)
Expand Down Expand Up @@ -54,22 +54,24 @@ to override, for example, global filesystem preferences, create an empty `.scala
and define build level preferences accordingly.


Disable Autoformatting
Disable Autoformatting / Enable Formatting of Base Directory Sources
----------------------

There are two ways to disable autoformatting: in the build, or in a `.scalariform.conf` preferences file.
Custom configuration options can be applied in the build, or in a `.scalariform.conf` preferences file.

Build

```
scalariformAutoformat := false
scalariformWithBaseDirectory := true
```

Filesystem

add to the top of target `.scalariform.conf` file:
```
autoformat=false
withBaseDirectory=true
```

License
Expand Down
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ val sbtScalariform = Project(projectName, file("."))
organization := "org.scalariform"
name := projectName
sonatypeProfileName := organization.value
version in ThisBuild := "1.8.0"
version in ThisBuild := "1.8.1"

licenses := Seq(("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0")))
homepage := scmInfo.value map (_.browseUrl)
Expand All @@ -24,7 +24,7 @@ version in ThisBuild := "1.8.0"
)
)

crossSbtVersions := Vector("0.13.16", "1.0.0")
crossSbtVersions := Vector("0.13.16", "1.0.2")

scalacOptions ++= List(
"-unchecked",
Expand All @@ -47,7 +47,9 @@ com.typesafe.sbt.SbtScalariform.ScalariformKeys.preferences := {
.setPreference(SpacesAroundMultiImports, false)
}

ScriptedPlugin.scriptedSettings
// preserve formatting of sbt-scripted test files
excludeFilter in scalariformFormat := "unformatted.scala" || "formatted.scala"

scriptedLaunchOpts := {
val sbtAssemblyVersion = "0.14.5"

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.16
sbt.version=1.0.2
8 changes: 4 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.7.1")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")
addSbtPlugin("org.scalariform" % "sbt-scalariform" % "1.8.0")
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.0")
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.7.0")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.0")
2 changes: 1 addition & 1 deletion project/scripted.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
libraryDependencies += "org.scala-sbt" % "scripted-plugin" % sbtVersion.value
libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value
21 changes: 16 additions & 5 deletions src/main/scala/com/typesafe/sbt/PreferencesFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ object PreferencesFile
}
case class PreferenceData(
autoformat: AutoFormat,
withBaseDirectory: Boolean,
prefs: Option[IFormattingPreferences]
)

Expand All @@ -45,20 +46,30 @@ object PreferencesFile

val maybeAutoFormat = Option(properties.getProperty("autoformat"))
val autoformat = AutoFormat(
maybeAutoFormat.map(_.toBoolean).getOrElse(true)
maybeAutoFormat.map(_.toBoolean).getOrElse(
SbtScalariform.Defaults.autoformat
)
)
val maybeWithBase = Option(properties.getProperty("withBaseDirectory"))
val withBaseDirectory =
maybeWithBase.map(_.toBoolean).getOrElse(
SbtScalariform.Defaults.withBaseDirectory
)
val nonStyles = maybeAutoFormat ++ maybeWithBase
val maybePrefs =
properties.size match {
case 0 => None
case 1 if maybeAutoFormat.isDefined => None // not a style format
case _ => Some(getPreferences(properties))
case x if nonStyles.size == x => None
case _ => Some(
getPreferences(properties)
)
}
(PreferenceData(autoformat, maybePrefs), file)
(PreferenceData(autoformat, withBaseDirectory, maybePrefs), file)
}

private def logChanged(data: ((PreferenceData, File), Streams)): Unit = {
data match {
case ((PreferenceData(AutoFormat(autoformat), maybePrefs), file), streams) =>
case ((PreferenceData(AutoFormat(autoformat), _, maybePrefs), file), streams) =>
if (autoFormatChanged(streams)(autoformat)) {
val action = if (autoformat) "enabled" else "disabled"
streams.log.info(s"Scalariform auto formatting $action")
Expand Down
21 changes: 17 additions & 4 deletions src/main/scala/com/typesafe/sbt/SbtScalariform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ object SbtScalariform
object autoImport {
val scalariformFormat = taskKey[Seq[File]]("Format (Scala) sources using scalariform")
val scalariformPreferences = settingKey[IFormattingPreferences]("Scalariform formatting preferences")
val scalariformAutoformat = settingKey[Boolean]("Whether Scala sources should be auto formatted when compile is run")
val scalariformAutoformat = settingKey[Boolean]("Whether Scala sources should be auto formatted when compile is run (default: true)")
val scalariformDoAutoformat = taskKey[Seq[File]]("Format sources if autoformat is configured")
val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")

@deprecated("Use scalariformAutoformat to turn autoformat on or off", "1.8.1")
def scalariformSettings(autoformat: Boolean): Seq[Setting[_]] = Nil
Expand All @@ -50,7 +51,12 @@ object SbtScalariform
override def globalSettings = Seq(
scalariformPreferences := defaultPreferences,
includeFilter in scalariformFormat := "*.scala",
scalariformAutoformat := PreferencesFile(None).forall(_.autoformat.autoformat)
scalariformAutoformat := (
PreferencesFile(None).map(_.autoformat.autoformat).getOrElse(Defaults.autoformat)
),
scalariformWithBaseDirectory := (
PreferencesFile(None).map(_.withBaseDirectory).getOrElse(Defaults.withBaseDirectory)
)
)

override def projectSettings = compileSettings ++
Expand All @@ -61,6 +67,12 @@ object SbtScalariform
val format = scalariformFormat
val preferences = scalariformPreferences
val autoformat = scalariformAutoformat
val withBaseDirectory = scalariformWithBaseDirectory
}

private[sbt] object Defaults {
val autoformat = true
val withBaseDirectory = false
}

val defaultPreferences = FormattingPreferences()
Expand All @@ -77,8 +89,9 @@ object SbtScalariform
def configScalariformSettings: Seq[Setting[_]] = {
List(
(sourceDirectories in scalariformFormat) :=
unmanagedSourceDirectories.value ++ Seq(
baseDirectory.in(LocalRootProject).value
unmanagedSourceDirectories.value ++ (
if (!scalariformWithBaseDirectory.value) Seq.empty
else Seq(baseDirectory.in(LocalRootProject).value)
),
scalariformPreferences := getPreferences(scalariformPreferences.value)(None),
scalariformFormat := Scalariform(
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scalariform.formatter.preferences._
import com.typesafe.sbt.SbtScalariform.ScalariformKeys

name := "test"
version := "0.1"

scalariformWithBaseDirectory := true

ScalariformKeys.preferences := ScalariformKeys.preferences.value
.setPreference(AlignArguments, true)
.setPreference(AlignSingleLineCaseStatements, true)
.setPreference(AlignSingleLineCaseStatements.MaxArrowIndent, 20)
.setPreference(CompactControlReadability, true)
.setPreference(DanglingCloseParenthesis, Force)
.setPreference(SpacesAroundMultiImports, false)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sys.props.get("sbt-assembly.version") match {
case Some(x) => addSbtPlugin("com.eed3si9n" % "sbt-assembly" % x)
case _ => sys.error(
"'sbt-assembly.version' property not defined! (add it to scriptedLaunchOpts -D)"
)
}

sys.props.get("sbt-scalariform.version") match {
case Some(x) => addSbtPlugin("org.scalariform" % "sbt-scalariform" % x)
case _ => sys.error(
"'sbt-scalariform.version' property not defined! (add it to scriptedLaunchOpts -D)"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object Foo {
def method(
string: String,
int: Int
) = {
string match {
case "wibble" => 42
case "foo" => 123
case _ => 100
}
}
method(
string = "hello",
int = 1
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Foo {
def method(
string: String,
int: Int) = {
string match {
case "wibble" => 42
case "foo" => 123
case _ => 100
}
}
method(
string = "hello",
int = 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$ copy-file sample/unformatted.scala target/A.scala

-$ must-mirror sample/formatted.scala target/A.scala

# autoformat on compile
> compile
$ must-mirror sample/formatted.scala target/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
withBaseDirectory=true
alignArguments=true
#alignParameters=false
alignSingleLineCaseStatements=true
alignSingleLineCaseStatements.maxArrowIndent=20
compactControlReadability=true
#compactStringConcatenation=false
danglingCloseParenthesis=Force
#doubleIndentClassDeclaration=false
#doubleIndentConstructorArguments=false
#doubleIndentMethodDeclaration=false
#firstArgumentOnNewline=Force
#firstParameterOnNewline=Force
#formatXml=true
#indentLocalDefs=false
#indentPackageBlocks=true
#indentSpaces=2
#indentWithTabs=false
#multilineScaladocCommentsStartOnFirstLine=false
#newlineAtEndOfFile=false
#placeScaladocAsterisksBeneathSecondAsterisk=false
#preserveSpaceBeforeArguments=false
#rewriteArrowSymbols=false
#spaceBeforeColon=false
#spaceBeforeContextColon=false
#spaceInsideBrackets=false
#spaceInsideParentheses=false
spacesAroundMultiImports=false
#spacesWithinPatternBinders=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name := "test"
version := "0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sys.props.get("sbt-assembly.version") match {
case Some(x) => addSbtPlugin("com.eed3si9n" % "sbt-assembly" % x)
case _ => sys.error(
"'sbt-assembly.version' property not defined! (add it to scriptedLaunchOpts -D)"
)
}

sys.props.get("sbt-scalariform.version") match {
case Some(x) => addSbtPlugin("org.scalariform" % "sbt-scalariform" % x)
case _ => sys.error(
"'sbt-scalariform.version' property not defined! (add it to scriptedLaunchOpts -D)"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object Foo {
def method(
string: String,
int: Int
) = {
string match {
case "wibble" => 42
case "foo" => 123
case _ => 100
}
}
method(
string = "hello",
int = 1
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Foo {
def method(
string: String,
int: Int) = {
string match {
case "wibble" => 42
case "foo" => 123
case _ => 100
}
}
method(
string = "hello",
int = 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
$ copy-file sample/unformatted.scala target/A.scala

-$ must-mirror sample/formatted.scala target/A.scala

# autoformat on compile
> compile
$ must-mirror sample/formatted.scala target/A.scala

0 comments on commit b37003c

Please sign in to comment.