diff --git a/.scalafmt.conf b/.scalafmt.conf index d51bbd6e..cafde549 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,5 +1,5 @@ -version = "2.3.2" +version = "2.5.2" project.git=true -align=none +align.preset=none assumeStandardLibraryStripMargin = true onTestFailure = "To fix this, run `./bin/scalafmt` from the project base directory" diff --git a/bin/scalafmt b/bin/scalafmt index 01f8e61f..ff06d999 100755 Binary files a/bin/scalafmt and b/bin/scalafmt differ diff --git a/src/main/scala/scalafix/internal/sbt/ScalafixCompletions.scala b/src/main/scala/scalafix/internal/sbt/ScalafixCompletions.scala index 68f5c963..e1cded04 100644 --- a/src/main/scala/scalafix/internal/sbt/ScalafixCompletions.scala +++ b/src/main/scala/scalafix/internal/sbt/ScalafixCompletions.scala @@ -16,7 +16,8 @@ class ScalafixCompletions( private type P = Parser[String] private type ArgP = Parser[ShellArgs.Arg] - private type KVArgP = Parser[ArgP] // nested parser allows to match the key only + private type KVArgP = + Parser[ArgP] // nested parser allows to match the key only private val sep: P = token("=" | " ").examples("=") private val string: P = StringBasic diff --git a/src/main/scala/scalafix/internal/sbt/ScalafixCoursier.scala b/src/main/scala/scalafix/internal/sbt/ScalafixCoursier.scala index eb0d05ea..10f365ff 100644 --- a/src/main/scala/scalafix/internal/sbt/ScalafixCoursier.scala +++ b/src/main/scala/scalafix/internal/sbt/ScalafixCoursier.scala @@ -11,14 +11,16 @@ import scalafix.sbt.BuildInfo import scala.collection.JavaConverters._ object ScalafixCoursier { - private def scalafixCliModule: Module = Module.of( - "ch.epfl.scala", - s"scalafix-cli_${BuildInfo.scala212}" - ) - private def scalafixCli: Dependency = Dependency.of( - scalafixCliModule, - BuildInfo.scalafixVersion - ) + private def scalafixCliModule: Module = + Module.of( + "ch.epfl.scala", + s"scalafix-cli_${BuildInfo.scala212}" + ) + private def scalafixCli: Dependency = + Dependency.of( + scalafixCliModule, + BuildInfo.scalafixVersion + ) def scalafixCliJars( repositories: Seq[cs.Repository] diff --git a/src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala b/src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala index aa37f1f9..d1f6439e 100644 --- a/src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala +++ b/src/main/scala/scalafix/internal/sbt/ScalafixInterface.scala @@ -91,13 +91,14 @@ class ScalafixInterface private ( api: ScalafixAPI, toolClasspath: URLClassLoader, mainCallback: ScalafixMainCallback - ) = this( - api - .newArguments() - .withMainCallback(mainCallback) - .withToolClasspath(toolClasspath), - Seq(Arg.ToolClasspath(toolClasspath)) - ) + ) = + this( + api + .newArguments() + .withMainCallback(mainCallback) + .withToolClasspath(toolClasspath), + Seq(Arg.ToolClasspath(toolClasspath)) + ) // Accumulates the classpath via classloader delegation, as only the last Arg.ToolClasspath is considered // diff --git a/src/main/scala/scalafix/internal/sbt/SemanticRuleValidator.scala b/src/main/scala/scalafix/internal/sbt/SemanticRuleValidator.scala index 1dd86f4b..baab21b7 100644 --- a/src/main/scala/scalafix/internal/sbt/SemanticRuleValidator.scala +++ b/src/main/scala/scalafix/internal/sbt/SemanticRuleValidator.scala @@ -43,25 +43,25 @@ class SemanticdbNotFound( } s"""|The semanticdb-scalac compiler plugin is required to run semantic rules like $names. - |To fix this problem for this sbt shell session, run `scalafixEnable` and try again. - |To fix this problem permanently for your build, add the following settings to build.sbt: - | - |$recommendedSetting - |""".stripMargin + |To fix this problem for this sbt shell session, run `scalafixEnable` and try again. + |To fix this problem permanently for your build, add the following settings to build.sbt: + | + |$recommendedSetting + |""".stripMargin } private def atLeastSbt13(scalaVersion: String) = s"""inThisBuild( - | List( - | scalaVersion := "$scalaVersion", - | semanticdbEnabled := true, - | semanticdbVersion := scalafixSemanticdb.revision - | ) - |) - |""".stripMargin + | List( + | scalaVersion := "$scalaVersion", + | semanticdbEnabled := true, + | semanticdbVersion := scalafixSemanticdb.revision + | ) + |) + |""".stripMargin private val atMostSbt12 = s"""addCompilerPlugin(scalafixSemanticdb) - |scalacOptions += "-Yrangepos" - |""".stripMargin + |scalacOptions += "-Yrangepos" + |""".stripMargin } diff --git a/src/main/scala/scalafix/sbt/ScalafixEnable.scala b/src/main/scala/scalafix/sbt/ScalafixEnable.scala index ff2fd9e3..4218f4fb 100644 --- a/src/main/scala/scalafix/sbt/ScalafixEnable.scala +++ b/src/main/scala/scalafix/sbt/ScalafixEnable.scala @@ -35,16 +35,17 @@ object ScalafixEnable { briefHelp = "Configure libraryDependencies, scalaVersion and scalacOptions for scalafix.", detail = """1. enables the semanticdb-scalac compiler plugin - |2. sets scalaVersion to latest Scala version supported by scalafix - |3. add -Yrangepos to scalacOptions""".stripMargin + |2. sets scalaVersion to latest Scala version supported by scalafix + |3. add -Yrangepos to scalacOptions""".stripMargin ) { s => val extracted = Project.extract(s) val settings: Seq[Setting[_]] = for { (p, fullVersion) <- projectsWithMatchingScalaVersion(s) - isEnabled = libraryDependencies - .in(p) - .get(extracted.structure.data) - .exists(_.exists(_.name == "semanticdb-scalac")) + isEnabled = + libraryDependencies + .in(p) + .get(extracted.structure.data) + .exists(_.exists(_.name == "semanticdb-scalac")) if !isEnabled setting <- List( scalaVersion.in(p) := fullVersion, diff --git a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala index 9dec5c20..a219d4ff 100644 --- a/src/main/scala/scalafix/sbt/ScalafixPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixPlugin.scala @@ -141,8 +141,8 @@ object ScalafixPlugin extends AutoPlugin { case els => stdoutLogger.error( s"""|Invalid rule: $els - |Expected format: ${DependencyRule.format} - |""".stripMargin + |Expected format: ${DependencyRule.format} + |""".stripMargin ) throw new ScalafixFailed(List(ScalafixError.CommandLineError)) } @@ -219,53 +219,57 @@ object ScalafixPlugin extends AutoPlugin { mainInterface: ScalafixInterface, shellArgs: ShellArgs, config: Configuration - ): Def.Initialize[Task[Unit]] = Def.task { - val files = filesToFix(shellArgs, config).value - runArgs(mainInterface.withArgs(Arg.Paths(files)), streams.value) - } + ): Def.Initialize[Task[Unit]] = + Def.task { + val files = filesToFix(shellArgs, config).value + runArgs(mainInterface.withArgs(Arg.Paths(files)), streams.value) + } private def scalafixSemantic( ruleNames: Seq[String], mainArgs: ScalafixInterface, shellArgs: ShellArgs, config: Configuration - ): Def.Initialize[Task[Unit]] = Def.taskDyn { - val dependencies = allDependencies.value - val files = filesToFix(shellArgs, config).value - val withScalaInterface = mainArgs.withArgs( - Arg.ScalaVersion(scalaVersion.value), - Arg.ScalacOptions(scalacOptions.value) - ) - val errors = new SemanticRuleValidator( - new SemanticdbNotFound(ruleNames, scalaVersion.value, sbtVersion.value) - ).findErrors(files, dependencies, withScalaInterface) - if (errors.isEmpty) { - Def.task { - val semanticInterface = withScalaInterface.withArgs( - Arg.Paths(files), - Arg.Classpath(fullClasspath.value.map(_.data.toPath)) - ) - runArgs(semanticInterface, streams.value) - } - } else { - Def.task { - if (errors.length == 1) { - throw new InvalidArgument(errors.head) - } else { - val message = errors.zipWithIndex - .map { case (msg, i) => s"[E${i + 1}] $msg" } - .mkString(s"${errors.length} errors\n", "\n", "") - throw new InvalidArgument(message) + ): Def.Initialize[Task[Unit]] = + Def.taskDyn { + val dependencies = allDependencies.value + val files = filesToFix(shellArgs, config).value + val withScalaInterface = mainArgs.withArgs( + Arg.ScalaVersion(scalaVersion.value), + Arg.ScalacOptions(scalacOptions.value) + ) + val errors = new SemanticRuleValidator( + new SemanticdbNotFound(ruleNames, scalaVersion.value, sbtVersion.value) + ).findErrors(files, dependencies, withScalaInterface) + if (errors.isEmpty) { + Def.task { + val semanticInterface = withScalaInterface.withArgs( + Arg.Paths(files), + Arg.Classpath(fullClasspath.value.map(_.data.toPath)) + ) + runArgs(semanticInterface, streams.value) + } + } else { + Def.task { + if (errors.length == 1) { + throw new InvalidArgument(errors.head) + } else { + val message = errors.zipWithIndex + .map { case (msg, i) => s"[E${i + 1}] $msg" } + .mkString(s"${errors.length} errors\n", "\n", "") + throw new InvalidArgument(message) + } } } } - } private def runArgs( interface: ScalafixInterface, streams: TaskStreams ): Unit = { - val paths = interface.args.collect { case Arg.Paths(paths) => paths }.flatten + val paths = interface.args.collect { + case Arg.Paths(paths) => paths + }.flatten if (paths.nonEmpty) { val cacheKeyArgs = interface.args.collect { case cacheKey: Arg.CacheKey => cacheKey diff --git a/src/main/scala/scalafix/sbt/ScalafixTestkitPlugin.scala b/src/main/scala/scalafix/sbt/ScalafixTestkitPlugin.scala index 696bbe40..bff25148 100644 --- a/src/main/scala/scalafix/sbt/ScalafixTestkitPlugin.scala +++ b/src/main/scala/scalafix/sbt/ScalafixTestkitPlugin.scala @@ -19,33 +19,34 @@ object ScalafixTestkitPlugin extends AutoPlugin { } import autoImport._ - override def projectSettings: Seq[Def.Setting[_]] = List( - resourceGenerators.in(Test) += Def.task { - val props = new java.util.Properties() - val values = Map[String, Seq[File]]( - "sourceroot" -> - List(baseDirectory.in(ThisBuild).value), - "inputClasspath" -> - scalafixTestkitInputClasspath.value.map(_.data), - "inputSourceDirectories" -> - scalafixTestkitInputSourceDirectories.value, - "outputSourceDirectories" -> - scalafixTestkitOutputSourceDirectories.value - ) - values.foreach { - case (key, files) => - props.put( - key, - files.iterator.filter(_.exists()).mkString(pathSeparator) - ) + override def projectSettings: Seq[Def.Setting[_]] = + List( + resourceGenerators.in(Test) += Def.task { + val props = new java.util.Properties() + val values = Map[String, Seq[File]]( + "sourceroot" -> + List(baseDirectory.in(ThisBuild).value), + "inputClasspath" -> + scalafixTestkitInputClasspath.value.map(_.data), + "inputSourceDirectories" -> + scalafixTestkitInputSourceDirectories.value, + "outputSourceDirectories" -> + scalafixTestkitOutputSourceDirectories.value + ) + values.foreach { + case (key, files) => + props.put( + key, + files.iterator.filter(_.exists()).mkString(pathSeparator) + ) + } + props.put("scalaVersion", scalaVersion.value) + props.put("scalacOptions", scalacOptions.value.mkString("|")) + val out = + managedResourceDirectories.in(Test).value.head / + "scalafix-testkit.properties" + IO.write(props, "Input data for scalafix testkit", out) + List(out) } - props.put("scalaVersion", scalaVersion.value) - props.put("scalacOptions", scalacOptions.value.mkString("|")) - val out = - managedResourceDirectories.in(Test).value.head / - "scalafix-testkit.properties" - IO.write(props, "Input data for scalafix testkit", out) - List(out) - } - ) + ) } diff --git a/src/sbt-test/sbt-scalafix/cross-build/project/ScalafixTestUtility.scala b/src/sbt-test/sbt-scalafix/cross-build/project/ScalafixTestUtility.scala index 17d4686f..7f191ba7 100644 --- a/src/sbt-test/sbt-scalafix/cross-build/project/ScalafixTestUtility.scala +++ b/src/sbt-test/sbt-scalafix/cross-build/project/ScalafixTestUtility.scala @@ -42,15 +42,15 @@ object ScalafixTestUtility { .map { diff => val msg = s"""File: $file - |Obtained output: - |---------------- - |$obtained - |Expected: - |--------- - |$expected - |Diff: - |----- - |$diff""".stripMargin + |Obtained output: + |---------------- + |$obtained + |Expected: + |--------- + |$expected + |Diff: + |----- + |$diff""".stripMargin streams.log.error(file) streams.log.error(msg) diff --git a/src/sbt-test/sbt-scalafix/suppress/project/ScalafixTestUtility.scala b/src/sbt-test/sbt-scalafix/suppress/project/ScalafixTestUtility.scala index 17d4686f..7f191ba7 100644 --- a/src/sbt-test/sbt-scalafix/suppress/project/ScalafixTestUtility.scala +++ b/src/sbt-test/sbt-scalafix/suppress/project/ScalafixTestUtility.scala @@ -42,15 +42,15 @@ object ScalafixTestUtility { .map { diff => val msg = s"""File: $file - |Obtained output: - |---------------- - |$obtained - |Expected: - |--------- - |$expected - |Diff: - |----- - |$diff""".stripMargin + |Obtained output: + |---------------- + |$obtained + |Expected: + |--------- + |$expected + |Diff: + |----- + |$diff""".stripMargin streams.log.error(file) streams.log.error(msg) diff --git a/src/test/scala/scalafix/internal/sbt/LoggingOutputStreamSuite.scala b/src/test/scala/scalafix/internal/sbt/LoggingOutputStreamSuite.scala index 33662abc..9b7f1045 100644 --- a/src/test/scala/scalafix/internal/sbt/LoggingOutputStreamSuite.scala +++ b/src/test/scala/scalafix/internal/sbt/LoggingOutputStreamSuite.scala @@ -121,7 +121,8 @@ class LoggingOutputStreamSuite extends AnyFunSuite with Matchers { test("capture very long messages") { withStubLogger(Level.Warn) { (outputStream, logs) => - val veryLongMessage = "a" * 1000000 // this would exhaust memory on quadratic implementations + val veryLongMessage = + "a" * 1000000 // this would exhaust memory on quadratic implementations outputStream.write(s"$veryLongMessage$sep".getBytes) logs.map(_._2) must be(mutable.Seq(veryLongMessage)) } diff --git a/src/test/scala/scalafix/internal/sbt/SbtCompletionsSuite.scala b/src/test/scala/scalafix/internal/sbt/SbtCompletionsSuite.scala index 843c7d3d..d02ee8e9 100644 --- a/src/test/scala/scalafix/internal/sbt/SbtCompletionsSuite.scala +++ b/src/test/scala/scalafix/internal/sbt/SbtCompletionsSuite.scala @@ -78,39 +78,39 @@ class SbtCompletionsSuite extends AnyFunSuite { val obtained = displays.mkString("\n").trim val expected = """|--auto-suppress-linter-errors - |--check - |--diff - |--diff-base= - |--files= - |--help - |--no-cache - |--rules= - |--stdout - |--syntactic - |--verbose - |--version - |DisableSyntax - | Reports an error for disabled features such as var or XML literals. - |ExplicitResultTypes - | Inserts type annotations for inferred public members. Only compatible with Scala 2.12.11. - |LeakingImplicitClassVal - | Adds 'private' to val parameters of implicit value classes - |NoAutoTupling - | Inserts explicit tuples for adapted argument lists for compatibility with -Yno-adapted-args - |NoValInForComprehension - | Removes deprecated val inside for-comprehension binders - |ProcedureSyntax - | Replaces deprecated procedure syntax with explicit ': Unit =' - |RemoveUnused - | Removes unused imports and terms that reported by the compiler under -Ywarn-unused - |SemanticRule - |SyntacticRule - |class: - |dependency: - |file: - |github: - |replace: - |""".stripMargin.trim.replaceAll("\r\n", "\n") + |--check + |--diff + |--diff-base= + |--files= + |--help + |--no-cache + |--rules= + |--stdout + |--syntactic + |--verbose + |--version + |DisableSyntax + | Reports an error for disabled features such as var or XML literals. + |ExplicitResultTypes + | Inserts type annotations for inferred public members. Only compatible with Scala 2.12.11. + |LeakingImplicitClassVal + | Adds 'private' to val parameters of implicit value classes + |NoAutoTupling + | Inserts explicit tuples for adapted argument lists for compatibility with -Yno-adapted-args + |NoValInForComprehension + | Removes deprecated val inside for-comprehension binders + |ProcedureSyntax + | Replaces deprecated procedure syntax with explicit ': Unit =' + |RemoveUnused + | Removes unused imports and terms that reported by the compiler under -Ywarn-unused + |SemanticRule + |SyntacticRule + |class: + |dependency: + |file: + |github: + |replace: + |""".stripMargin.trim.replaceAll("\r\n", "\n") if (obtained != expected) { println("\"\"\"|") obtained.lines.foreach { line => @@ -171,35 +171,35 @@ class SbtCompletionsSuite extends AnyFunSuite { checkArgs("--files --test") { args => assert(args == Left("""missing or invalid value - | --files --test - | ^""".stripMargin)) + | --files --test + | ^""".stripMargin)) } checkArgs("--test --rules=Foo --files=NotHere", SkipWindows) { args => assert(args == Left("""--files=NotHere - |missing or invalid value - | --test --rules=Foo --files=NotHere - | ^""".stripMargin)) + |missing or invalid value + | --test --rules=Foo --files=NotHere + | ^""".stripMargin)) } checkArgs("--test -f= --rules=Foo", SkipWindows) { args => assert(args == Left("""Expected non-whitespace character - |missing or invalid value - | --test -f= --rules=Foo - | ^""".stripMargin)) + |missing or invalid value + | --test -f= --rules=Foo + | ^""".stripMargin)) } checkArgs("--test -f --rules=Foo", SkipWindows) { args => assert(args == Left("""missing or invalid value - | --test -f --rules=Foo - | ^""".stripMargin)) + | --test -f --rules=Foo + | ^""".stripMargin)) } checkArgs("--test --rules=Foo -f", SkipWindows) { args => assert(args == Left("""-f - |missing or invalid value - | --test --rules=Foo -f - | ^""".stripMargin)) + |missing or invalid value + | --test --rules=Foo -f + | ^""".stripMargin)) } } diff --git a/src/test/scala/scalafix/internal/sbt/ScalafixAPISuite.scala b/src/test/scala/scalafix/internal/sbt/ScalafixAPISuite.scala index b658d6b9..98c5c55e 100644 --- a/src/test/scala/scalafix/internal/sbt/ScalafixAPISuite.scala +++ b/src/test/scala/scalafix/internal/sbt/ScalafixAPISuite.scala @@ -13,8 +13,8 @@ import scala.util.Properties class ScalafixAPISuite extends AnyFunSuite { - def assertNoDiff(obtained: String, expected: String)( - implicit pos: Position + def assertNoDiff(obtained: String, expected: String)(implicit + pos: Position ): Unit = { def strip(s: String) = s.trim.replaceAll("\r\n", "\n") val obtainedStrip = strip(obtained) @@ -72,9 +72,9 @@ class ScalafixAPISuite extends AnyFunSuite { assertNoDiff( obtainedOut, """|[error] Tmp.scala:3:12: error: [DisableSyntax.noSemicolons] semicolons are disabled - |[error] val x = 1; - |[error] ^ - |""".stripMargin + |[error] val x = 1; + |[error] ^ + |""".stripMargin ) } }