Skip to content

Commit

Permalink
scalafmt: enable trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 1, 2024
1 parent cacff03 commit d0fed30
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
1 change: 1 addition & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rewrite {
stringInterpolation = true
}
sortModifiers.preset = styleGuide
trailingCommas.style = "always"
}
align {
preset = none
Expand Down
16 changes: 8 additions & 8 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ inThisBuild(List(
"poslegm",
"Mikhail Chugunkov",
"poslegm@gmail.com",
url("https://chugunkov.website/")
url("https://chugunkov.website/"),
),
Developer(
"olafurpg",
"Ólafur Páll Geirsson",
"olafurpg@gmail.com",
url("https://geirsson.com")
url("https://geirsson.com"),
),
Developer(
"tanishiking",
"Rikito Taniguchi",
"rikiriki1238@gmail.com",
url("https://github.com/tanishiking/")
)
url("https://github.com/tanishiking/"),
),
),
resolvers ++= Resolver.sonatypeOssRepos("public"),
scalaVersion := "2.12.19",
packageDoc / publishArtifact := sys.env.contains("CI"),
packageSrc / publishArtifact := sys.env.contains("CI")
packageSrc / publishArtifact := sys.env.contains("CI"),
))
publish / skip := true

Expand All @@ -48,16 +48,16 @@ lazy val plugin = project.enablePlugins(SbtPlugin).settings(
libraryDependencies ++= List(
"com.googlecode.java-diff-utils" % "diffutils" % "1.3.0",
"org.scalameta" %% "scalafmt-sysops" % scalafmtVersion,
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion
"org.scalameta" %% "scalafmt-dynamic" % scalafmtVersion,
),
scriptedBufferLog := false,
scriptedLaunchOpts += s"-Dplugin.version=${version.value}",
// For compat reasons we have this in here to ensure we are testing against 1.2.8
// We honestly probably don't need to, so if this ever causes issues, rip it out.
pluginCrossBuild / sbtVersion := "1.2.8"
pluginCrossBuild / sbtVersion := "1.2.8",
)

// For some reason, it doesn't work if this is defined in globalSettings in PublishPlugin.
inScope(Global)(Seq(
PgpKeys.pgpPassphrase := sys.env.get("PGP_PASSPHRASE").map(_.toCharArray())
PgpKeys.pgpPassphrase := sys.env.get("PGP_PASSPHRASE").map(_.toCharArray()),
))
2 changes: 1 addition & 1 deletion plugin/src/main/scala/org/scalafmt/sbt/ErrorHandling.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ private[sbt] class ErrorHandling(
val printDiff: Boolean,
val logOnEachError: Boolean,
val failOnErrors: Boolean,
val detailedErrorEnabled: Boolean
val detailedErrorEnabled: Boolean,
)
70 changes: 35 additions & 35 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,39 @@ object ScalafmtPlugin extends AutoPlugin {
val scalafmtIncremental =
taskKey[Unit]("Format Scala sources to be compiled incrementally with scalafmt (alias to scalafmt).")
val scalafmtCheck = taskKey[Unit](
"Fails if a Scala source is mis-formatted. Does not write to files."
"Fails if a Scala source is mis-formatted. Does not write to files.",
)
val scalafmtOnCompile = settingKey[Boolean](
"Format Scala source files on compile, off by default."
"Format Scala source files on compile, off by default.",
)
val scalafmtConfig = taskKey[File](
"Location of .scalafmt.conf file. " +
"If the file does not exist, exception is thrown."
"If the file does not exist, exception is thrown.",
)
val scalafmtSbt =
taskKey[Unit]("Format *.sbt and project/*.scala files for this sbt build.")
val scalafmtSbtCheck = taskKey[Unit](
"Fails if a *.sbt or project/*.scala source is mis-formatted. " +
"Does not write to files."
"Does not write to files.",
)
val scalafmtOnly = inputKey[Unit]("Format a single given file.")
val scalafmtAll = taskKey[Unit](
"Execute the scalafmt task for all configurations in which it is enabled. " +
"(By default this means the Compile and Test configurations.)"
"(By default this means the Compile and Test configurations.)",
)
val scalafmtCheckAll = taskKey[Unit](
"Execute the scalafmtCheck task for all configurations in which it is enabled. " +
"(By default this means the Compile and Test configurations.)"
"(By default this means the Compile and Test configurations.)",
)
val scalafmtDetailedError = settingKey[Boolean](
"Enables logging of detailed errors with stacktraces, disabled by default"
"Enables logging of detailed errors with stacktraces, disabled by default",
)
val scalafmtFilter =
settingKey[String]("File filtering mode when running scalafmt.")
val scalafmtLogOnEachError =
settingKey[Boolean]("Enables logging on an error.")
val scalafmtFailOnErrors = settingKey[Boolean](
"Controls whether to fail in case of formatting errors."
"Controls whether to fail in case of formatting errors.",
)
val scalafmtPrintDiff =
settingKey[Boolean]("Enables full diff output when running check.")
Expand All @@ -85,7 +85,7 @@ object ScalafmtPlugin extends AutoPlugin {
{ a: ScalafmtAnalysis =>
("failedScalafmtCheck", a.failedScalafmtCheck) :*: LNil
},
{ in: Set[File] :*: LNil => ScalafmtAnalysis(in.head) }
{ in: Set[File] :*: LNil => ScalafmtAnalysis(in.head) },
)
}

Expand Down Expand Up @@ -131,13 +131,13 @@ object ScalafmtPlugin extends AutoPlugin {
credentials: Seq[Credentials],
currentProject: ResolvedProject,
filterMode: String,
errorHandling: ErrorHandling
errorHandling: ErrorHandling,
) {
private val log = new ScalafmtLogger(taskStreams.log)
private val reporter = new ScalafmtSbtReporter(
log,
new OutputStreamWriter(taskStreams.binary()),
errorHandling
errorHandling,
)

private val scalafmtSession = {
Expand All @@ -159,7 +159,7 @@ object ScalafmtPlugin extends AutoPlugin {
.withRepositoryCredentials(repoCredentials: _*)
.createSession(config.toAbsolutePath)
if (scalafmtSession == null) throw messageException(
"failed to create formatting session. Please report bug to https://github.com/scalameta/sbt-scalafmt"
"failed to create formatting session. Please report bug to https://github.com/scalameta/sbt-scalafmt",
)
scalafmtSession
}
Expand Down Expand Up @@ -208,7 +208,7 @@ object ScalafmtPlugin extends AutoPlugin {
}

private def withFormattedSources[T](initial: T, sources: Seq[File])(
onFormat: (T, File, Input, Output) => T
onFormat: (T, File, Input, Output) => T,
): T = {
var res = initial
var good = 0
Expand Down Expand Up @@ -320,21 +320,21 @@ object ScalafmtPlugin extends AutoPlugin {
// The tracking is shared between scalafmt and scalafmtCheck
private def trackSourcesAndConfig(
cacheStoreFactory: CacheStoreFactory,
sources: Seq[File]
sources: Seq[File],
)(
f: (ChangeReport[File], Boolean, ScalafmtAnalysis) => ScalafmtAnalysis
f: (ChangeReport[File], Boolean, ScalafmtAnalysis) => ScalafmtAnalysis,
): ScalafmtAnalysis = {
// use prevTracker to share previous values between tasks
val prevTracker = Tracked
.lastOutput[Unit, ScalafmtAnalysis](cacheStoreFactory.make("last")) {
(_, prev0) =>
val prev = prev0.getOrElse(ScalafmtAnalysis(Set.empty))
val tracker = Tracked.inputChanged[HashFileInfo, ScalafmtAnalysis](
cacheStoreFactory.make("config")
cacheStoreFactory.make("config"),
) { case (configChanged, configHash) =>
Tracked.diffOutputs(
cacheStoreFactory.make("output-diff"),
FileInfo.lastModified
FileInfo.lastModified,
)(sources.toSet) { (outDiff: ChangeReport[File]) =>
log.debug(outDiff.toString())
f(outDiff, configChanged, prev)
Expand All @@ -348,7 +348,7 @@ object ScalafmtPlugin extends AutoPlugin {
private def throwOnFailure(analysis: ScalafmtAnalysis): Unit = {
val failed = analysis.failedScalafmtCheck
if (failed.nonEmpty) throw messageException(
s"${failed.size} files must be formatted ($baseDir)"
s"${failed.size} files must be formatted ($baseDir)",
)
}
}
Expand All @@ -374,27 +374,27 @@ object ScalafmtPlugin extends AutoPlugin {
.getAbsolutePath
projectDirectory.descendantsExcept(
"*.scala",
(pathname: File) => pathname.getAbsolutePath.startsWith(targetDirectory)
(pathname: File) => pathname.getAbsolutePath.startsWith(targetDirectory),
).get
} else Nil
}

private def scalafmtTask(
sources: Seq[File],
dirs: Seq[File],
session: FormatSession
session: FormatSession,
) = Def.task(session.formatTrackedSources(sources, dirs))
.tag(ScalafmtTagPack: _*)

private def scalafmtCheckTask(
sources: Seq[File],
dirs: Seq[File],
session: FormatSession
session: FormatSession,
) = Def.task(session.checkTrackedSources(sources, dirs))
.tag(ScalafmtTagPack: _*)

private def getScalafmtSourcesTask(
f: (Seq[File], Seq[File], FormatSession) => InitTask
f: (Seq[File], Seq[File], FormatSession) => InitTask,
) = Def.taskDyn[Unit] {
val sources = unmanagedSources.in(scalafmt).?.value.getOrElse(Seq.empty)
val dirs = unmanagedSourceDirectories.in(scalafmt).?.value.getOrElse(Nil)
Expand All @@ -404,26 +404,26 @@ object ScalafmtPlugin extends AutoPlugin {
private def scalafmtSbtTask(
sources: Seq[File],
dirs: Seq[File],
session: FormatSession
session: FormatSession,
) = Def.task(session.formatSources(sources, dirs)).tag(ScalafmtTagPack: _*)

private def scalafmtSbtCheckTask(
sources: Seq[File],
dirs: Seq[File],
session: FormatSession
session: FormatSession,
) = Def.task(session.checkSources(sources, dirs)).tag(ScalafmtTagPack: _*)

private def getScalafmtSbtTasks(
func: (Seq[File], Seq[File], FormatSession) => InitTask
func: (Seq[File], Seq[File], FormatSession) => InitTask,
) = Def.taskDyn {
joinScalafmtTasks(func)(
(sbtSources.value, Nil, sbtConfig.value),
(metabuildSources.value, Nil, scalaConfig.value)
(metabuildSources.value, Nil, scalaConfig.value),
)
}

private def joinScalafmtTasks(
func: (Seq[File], Seq[File], FormatSession) => InitTask
func: (Seq[File], Seq[File], FormatSession) => InitTask,
)(tuples: (Seq[File], Seq[File], Path)*) = {
val tasks = tuples.map { case (files, dirs, config) =>
getScalafmtTask(func)(files, dirs, config)
Expand All @@ -432,7 +432,7 @@ object ScalafmtPlugin extends AutoPlugin {
}

private def getScalafmtTask(
func: (Seq[File], Seq[File], FormatSession) => InitTask
func: (Seq[File], Seq[File], FormatSession) => InitTask,
)(files: Seq[File], dirs: Seq[File], config: Path) = Def.taskDyn[Unit] {
if (files.isEmpty) Def.task(Unit)
else {
Expand All @@ -448,8 +448,8 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtPrintDiff.value,
scalafmtLogOnEachError.value,
scalafmtFailOnErrors.value,
scalafmtDetailedError.value
)
scalafmtDetailedError.value,
),
)
func(files, dirs, session)
}
Expand Down Expand Up @@ -491,10 +491,10 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtPrintDiff.value,
scalafmtLogOnEachError.value,
scalafmtFailOnErrors.value,
scalafmtDetailedError.value
)
scalafmtDetailedError.value,
),
).formatSources(absFiles, Nil)
}
},
)

private val anyConfigsInThisProject =
Expand All @@ -505,7 +505,7 @@ object ScalafmtPlugin extends AutoPlugin {
inConfig(_)(scalafmtConfigSettings)
} ++ Seq(
scalafmtAll := scalafmt.?.all(anyConfigsInThisProject).value,
scalafmtCheckAll := scalafmtCheck.?.all(anyConfigsInThisProject).value
scalafmtCheckAll := scalafmtCheck.?.all(anyConfigsInThisProject).value,
)

override def buildSettings: Seq[Def.Setting[_]] =
Expand All @@ -517,7 +517,7 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtLogOnEachError := false,
scalafmtFailOnErrors := true,
scalafmtPrintDiff := false,
scalafmtDetailedError := false
scalafmtDetailedError := false,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.scalafmt.interfaces.ScalafmtReporter
class ScalafmtSbtReporter(
log: Logger,
out: OutputStreamWriter,
errorHandling: ErrorHandling
errorHandling: ErrorHandling,
) extends ScalafmtReporter {
import ScalafmtSbtReporter._

Expand Down

0 comments on commit d0fed30

Please sign in to comment.