Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalafmtSessionFactory usage #100

Merged
merged 4 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions plugin/src/main/scala/org/scalafmt/sbt/PluginInstanceSession.scala
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. should it be package-private?
  2. perhaps rename to something like LegacyScalafmtSession or CompatibilityScalafmtSession or EmulatedScalafmtSession?

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line wasn't there before. probably a good change. could it potentially cause some complaints?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's on-by-default. Just explicitly declare this setting because sbt plugin hasn't other inputs than filters specified in config.

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("[", ",", "]")}"
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