From 4f3ec9d2fa977d4f8858899acfacfd7913aaaa70 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Tue, 1 Oct 2019 23:15:12 +0300 Subject: [PATCH 1/3] scalafmt version update --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index b0d5be2..52ca980 100644 --- a/build.sbt +++ b/build.sbt @@ -39,7 +39,7 @@ lazy val plugin = project .settings( moduleName := "sbt-scalafmt", libraryDependencies ++= List( - "org.scalameta" %% "scalafmt-dynamic" % "2.1.0-RC2" + "org.scalameta" %% "scalafmt-dynamic" % "2.1.0" ), scriptedBufferLog := false, scriptedLaunchOpts += s"-Dplugin.version=${version.value}" From 49599d98b230616c391f4589396eaa06d1c4a8e2 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Mon, 11 May 2020 20:46:19 +0300 Subject: [PATCH 2/3] ScalafmtSession usage (#96) --- .../scalafmt/sbt/PluginInstanceSession.scala | 11 +++++++++++ .../org/scalafmt/sbt/ScalafmtPlugin.scala | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala diff --git a/plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala b/plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala new file mode 100644 index 0000000..b940549 --- /dev/null +++ b/plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala @@ -0,0 +1,11 @@ +package org.scalafmt.sbt + +import java.nio.file.Path +import org.scalafmt.interfaces.{Scalafmt, ScalafmtSession} + +class PluginInstanceSession(config: Path, instance: Scalafmt) + extends ScalafmtSession { + override def format(file: Path, code: String): String = + instance.format(config, file, code) + override def matchesProjectFilters(file: Path): Boolean = true +} diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala index ca05da6..5d7d4e4 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala @@ -16,7 +16,7 @@ import sbt.util.Logger import scala.util.Failure import scala.util.Success import scala.util.Try -import org.scalafmt.interfaces.Scalafmt +import org.scalafmt.interfaces.{Scalafmt, ScalafmtSessionFactory} import sbt.librarymanagement.MavenRepository object ScalafmtPlugin extends AutoPlugin { @@ -104,10 +104,22 @@ object ScalafmtPlugin extends AutoPlugin { val repositories = resolvers.collect { case r: MavenRepository => r.root } - val scalafmtInstance = + val scalafmtSession = globalInstance .withReporter(reporter) .withMavenRepositories(repositories: _*) + .withRespectProjectFilters(true) match { + case t: ScalafmtSessionFactory => + val session = t.createSession(config.toAbsolutePath) + if (session == null) { + throw new MessageOnlyException( + "failed to create formatting session. Please report bug to https://github.com/scalameta/sbt-scalafmt" + ) + } + session + case instance => + new PluginInstanceSession(config.toAbsolutePath, instance) + } log.debug( s"Adding repositories ${repositories.mkString("[", ",", "]")}" @@ -116,8 +128,7 @@ object ScalafmtPlugin extends AutoPlugin { .map { file => val input = IO.read(file) val output = - scalafmtInstance.format( - config.toAbsolutePath, + scalafmtSession.format( file.toPath.toAbsolutePath, input ) From b7df7123ee07eaee929d42ceaf655e6e3ef0b29c Mon Sep 17 00:00:00 2001 From: Mikhail Chugunkov Date: Tue, 12 May 2020 12:59:02 +0300 Subject: [PATCH 3/3] Renamed CompatibilityScalafmtSession --- ...anceSession.scala => CompatibilityScalafmtSession.scala} | 6 ++++-- plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) rename plugin/src/main/scala/org/scalafmt/sbt/{PluginInstanceSession.scala => CompatibilityScalafmtSession.scala} (70%) diff --git a/plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala b/plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala similarity index 70% rename from plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala rename to plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala index b940549..40d910d 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala @@ -3,8 +3,10 @@ package org.scalafmt.sbt import java.nio.file.Path import org.scalafmt.interfaces.{Scalafmt, ScalafmtSession} -class PluginInstanceSession(config: Path, instance: Scalafmt) - extends ScalafmtSession { +private[sbt] class CompatibilityScalafmtSession( + config: Path, + instance: Scalafmt +) extends ScalafmtSession { override def format(file: Path, code: String): String = instance.format(config, file, code) override def matchesProjectFilters(file: Path): Boolean = true diff --git a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala index 5d7d4e4..02f18f3 100644 --- a/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala +++ b/plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala @@ -118,7 +118,7 @@ object ScalafmtPlugin extends AutoPlugin { } session case instance => - new PluginInstanceSession(config.toAbsolutePath, instance) + new CompatibilityScalafmtSession(config.toAbsolutePath, instance) } log.debug(