-
Notifications
You must be signed in to change notification settings - Fork 42
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
extends ScalafmtSession { | ||
override def format(file: Path, code: String): String = | ||
instance.format(config, file, code) | ||
override def matchesProjectFilters(file: Path): Boolean = true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("[", ",", "]")}" | ||
|
@@ -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 | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LegacyScalafmtSession
orCompatibilityScalafmtSession
orEmulatedScalafmtSession
?