From f0f72169e7cc7cb3e1365faf1e45d79d946a9705 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 31 Mar 2024 13:38:04 -0700 Subject: [PATCH] scalafmt: expand RedundantBraces --- .scalafmt.conf | 4 ++++ build.sbt | 2 +- .../org/scalafmt/sbt/ScalafmtPlugin.scala | 23 ++++++++----------- .../scalafmt/sbt/ScalafmtSbtReporter.scala | 3 +-- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 922f8ff..062981b 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -26,6 +26,10 @@ rewrite { ["org\\..*"] ] } + redundantBraces { + ifElseExpressions = true + stringInterpolation = true + } sortModifiers.preset = styleGuide } align { diff --git a/build.sbt b/build.sbt index 2654dbf..51e7dd6 100644 --- a/build.sbt +++ b/build.sbt @@ -44,7 +44,7 @@ inThisBuild( publish / skip := true val scalafmtVersion = "3.8.1" -onLoadMessage := s"Welcome to sbt-scalafmt ${version.value} (scalafmt ${scalafmtVersion})" +onLoadMessage := s"Welcome to sbt-scalafmt ${version.value} (scalafmt $scalafmtVersion)" lazy val plugin = project .enablePlugins(SbtPlugin) diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala index 2e4f8fd..72b5b2a 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala @@ -103,11 +103,10 @@ object ScalafmtPlugin extends AutoPlugin { private val scalaConfig = scalafmtConfig.map { f => - if (f.exists()) { + if (f.exists()) f.toPath - } else { + else throw messageException(s"File does not exist: ${f.toPath}") - } } private val sbtConfig = scalaConfig @@ -320,14 +319,14 @@ object ScalafmtPlugin extends AutoPlugin { throwOnFailure(checkFilteredSources(filterFiles(sources, dirs))) private def checkFilteredSources(sources: Seq[File]): ScalafmtAnalysis = { - if (sources.nonEmpty) { + if (sources.nonEmpty) log.info(s"Checking ${sources.size} Scala sources ($baseDir)...") - } val unformatted = Set.newBuilder[File] withFormattedSources(Unit, sources) { (_, file, input, output) => - val diff = if (errorHandling.printDiff) { - DiffUtils.unifiedDiff("/" + asRelative(file), input, output) - } else "" + val diff = + if (errorHandling.printDiff) + DiffUtils.unifiedDiff("/" + asRelative(file), input, output) + else "" val suffix = if (diff.isEmpty) "" else '\n' + diff log.warn(s"$file isn't formatted properly!$suffix") unformatted += file @@ -404,9 +403,8 @@ object ScalafmtPlugin extends AutoPlugin { pathname.getAbsolutePath.startsWith(targetDirectory) ) .get - } else { + } else Nil - } } private def scalafmtTask( @@ -500,11 +498,10 @@ object ScalafmtPlugin extends AutoPlugin { scalafmtCheck := getScalafmtSourcesTask(scalafmtCheckTask).value, scalafmtSbtCheck := getScalafmtSbtTasks(scalafmtSbtCheckTask).value, scalafmtDoFormatOnCompile := Def.settingDyn { - if (scalafmtOnCompile.value) { + if (scalafmtOnCompile.value) scalafmt in resolvedScoped.value.scope - } else { + else Def.task(()) - } }.value, sources in Compile := (sources in Compile) .dependsOn(scalafmtDoFormatOnCompile) diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala index 048d13a..49826ca 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtSbtReporter.scala @@ -32,14 +32,13 @@ class ScalafmtSbtReporter( res.write(" [") res.write(file.toString) res.write(']') - if (null != e && !toThrow) { + if (null != e && !toThrow) if (errorHandling.detailedErrorEnabled) e.printStackTrace(new PrintWriter(res)) else if (messageOpt ne nestedMessage) nestedMessage.foreach { x => res.write(": ") res.write(x) } - } res.toString }