Skip to content

Commit

Permalink
make formatting of base directory sources opt-in (fixes sbt#71, sbt#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
godenji committed Oct 12, 2017
1 parent 524e89f commit 5664751
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/main/scala/com/typesafe/sbt/PreferencesFile.scala
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
16 changes: 12 additions & 4 deletions src/main/scala/com/typesafe/sbt/SbtScalariform.scala
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,8 @@ object SbtScalariform
override def globalSettings = Seq(
scalariformPreferences := defaultPreferences,
includeFilter in scalariformFormat := "*.scala",
scalariformAutoformat := PreferencesFile(None).forall(_.autoformat.autoformat)
scalariformAutoformat := PreferencesFile(None).forall(_.autoformat.autoformat),
scalariformWithBaseDirectory := PreferencesFile(None).forall(_.withBaseDirectory)
)

override def projectSettings = compileSettings ++
Expand All @@ -63,6 +65,11 @@ object SbtScalariform
val autoformat = scalariformAutoformat
}

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

val defaultPreferences = FormattingPreferences()

private val compileSettings = inputs(Compile) ++ inputs(Test)
Expand All @@ -77,8 +84,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

0 comments on commit 5664751

Please sign in to comment.