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

Keep baseDir as Path, use its .relative() instead #203

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Changes from all commits
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
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
@@ -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()
@@ -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 {
@@ -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", ""))
@@ -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")