Skip to content

Commit

Permalink
ScalafmtSessionFactory usage (#100)
Browse files Browse the repository at this point in the history
* scalafmt version update

* ScalafmtSession usage (#96)

* Renamed CompatibilityScalafmtSession
  • Loading branch information
poslegm authored May 12, 2020
1 parent b8314d9 commit bf481bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 15 additions & 4 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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("[", ",", "]")}"
Expand All @@ -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
)
Expand Down

0 comments on commit bf481bc

Please sign in to comment.