Skip to content

Commit

Permalink
Keep baseDir as Path, use its .relative() instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Meltzer authored and kitbellew committed Dec 24, 2021
1 parent 68813f5 commit 9fec92e
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ object ScalafmtPlugin extends AutoPlugin {
scalafmtSession
}

private lazy val baseDir: File = currentProject.base.getCanonicalFile
private lazy val baseDir: Path = currentProject.base.getCanonicalFile.toPath

@inline private def asRelative(file: File): File =
file.relativeTo(baseDir).getOrElse(file)
@inline private def asRelative(file: File): String =
baseDir.relativize(file.getCanonicalFile.toPath).toString

private def filterFiles(sources: Seq[File]): Seq[File] = {
val filter = getFileFilter()
Expand All @@ -185,7 +185,7 @@ object ScalafmtPlugin extends AutoPlugin {
}

private def getFileFilter(): Path => Boolean = {
def gitOps = GitOps.FactoryImpl(AbsoluteFile(baseDir.toPath))
def gitOps = GitOps.FactoryImpl(AbsoluteFile(baseDir))
def getFromFiles(getFiles: => Seq[AbsoluteFile], gitCmd: => String) = {
def gitMessage = s"[git $gitCmd] ($baseDir)"
Try(getFiles) match {
Expand Down Expand Up @@ -286,7 +286,7 @@ object ScalafmtPlugin extends AutoPlugin {
else prev.failedScalafmtCheck & outDiff.unmodified
if (prevFailed.nonEmpty) {
val files: Seq[String] =
prevFailed.map(asRelative(_).toString)(collection.breakOut)
prevFailed.map(asRelative)(collection.breakOut)
val prefix =
s"$baseDir: ${files.length} files aren't formatted properly:\n"
log.warn(files.sorted.mkString(prefix, "\n", ""))
Expand All @@ -310,7 +310,7 @@ object ScalafmtPlugin extends AutoPlugin {
val unformatted = Set.newBuilder[File]
withFormattedSources(Unit, sources) { (_, file, input, output) =>
val diff = if (errorHandling.printDiff) {
DiffUtils.unifiedDiff("/" + asRelative(file).getPath, input, output)
DiffUtils.unifiedDiff("/" + asRelative(file), input, output)
} else ""
val suffix = if (diff.isEmpty) "" else '\n' + diff
log.warn(s"$file isn't formatted properly!$suffix")
Expand Down

0 comments on commit 9fec92e

Please sign in to comment.