diff --git a/plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala b/plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala new file mode 100644 index 0000000..40d910d --- /dev/null +++ b/plugin/src/main/scala/org/scalafmt/sbt/CompatibilityScalafmtSession.scala @@ -0,0 +1,13 @@ +package org.scalafmt.sbt + +import java.nio.file.Path +import org.scalafmt.interfaces.{Scalafmt, 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 ca05da6..02f18f3 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 CompatibilityScalafmtSession(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 )