Skip to content

Commit

Permalink
ScalafmtRunner: filter Path not AbsoluteFile
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 27, 2021
1 parent 216e339 commit 341d136
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ScalafmtCoreRunner extends ScalafmtRunner {
options.customExcludes
)

val inputMethods = getInputMethods(options, filterMatcher.matchesFile)
val inputMethods = getInputMethods(options, filterMatcher.matchesPath)
if (inputMethods.isEmpty && options.mode.isEmpty && !options.stdIn)
throw NoMatchingFiles

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.scalafmt.cli

import java.io.File
import java.nio.file.Path
import java.util.concurrent.atomic.{AtomicInteger, AtomicReference}

import org.scalafmt.CompatCollections.ParConverters._
import org.scalafmt.Error.{MisformattedFile, NoMatchingFiles}
import org.scalafmt.dynamic.ScalafmtDynamicError
import org.scalafmt.interfaces.Scalafmt
import org.scalafmt.interfaces.ScalafmtSession
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.FileOps

import scala.meta.internal.tokenizers.PlatformTokenizerCache
import util.control.Breaks._
Expand All @@ -32,11 +33,10 @@ object ScalafmtDynamicRunner extends ScalafmtRunner {
return reporter.getExitCode // XXX: returning
}

def sessionMatcher(x: AbsoluteFile): Boolean =
session.matchesProjectFilters(x.path)
val filterMatcher: AbsoluteFile => Boolean =
options.customFilesOpt.fold(sessionMatcher _) { customFiles =>
val customMatcher = getFileMatcher(customFiles)
val sessionMatcher = session.matchesProjectFilters _
val filterMatcher: Path => Boolean =
options.customFilesOpt.fold(sessionMatcher) { customFiles =>
val customMatcher = getFileMatcher(customFiles.map(_.path))
x => customMatcher(x) && sessionMatcher(x)
}
val inputMethods = getInputMethods(options, filterMatcher)
Expand Down Expand Up @@ -81,12 +81,10 @@ object ScalafmtDynamicRunner extends ScalafmtRunner {
inputMethod.write(formatResult, input, options)
}

private def getFileMatcher(
paths: Seq[AbsoluteFile]
): AbsoluteFile => Boolean = {
private def getFileMatcher(paths: Seq[Path]): Path => Boolean = {
require(paths.nonEmpty)
val (files, dirs) = paths.partition(_.isRegularFile)
(x: AbsoluteFile) =>
val (files, dirs) = paths.partition(FileOps.isRegularFile)
(x: Path) =>
files.contains(x) || {
val filename = x.toString()
dirs.exists { dir =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.scalafmt.cli

import java.io.OutputStreamWriter
import java.nio.file.Path

import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.GitOps.Implicit
Expand Down Expand Up @@ -30,7 +31,7 @@ trait ScalafmtRunner {

protected def getInputMethods(
options: CliOptions,
filter: AbsoluteFile => Boolean
filter: Path => Boolean
): Seq[InputMethod] = {
if (options.stdIn) {
Seq(InputMethod.StdinCode(options.assumeFilename, options.common.in))
Expand All @@ -46,7 +47,7 @@ trait ScalafmtRunner {
/** Returns file paths defined via options.{customFiles,customExclude} */
private[this] def getFilesFromCliOptions(
options: CliOptions,
canFormat: AbsoluteFile => Boolean
canFormat: Path => Boolean
): Seq[AbsoluteFile] = {
val gitOps = options.gitOps
val files = options.fileFetchMode match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.scalafmt.sysops

import scala.language.implicitConversions
import scala.sys.process.ProcessLogger
import scala.util.{Failure, Success, Try}
import java.nio.file.Path
Expand All @@ -15,10 +16,14 @@ object GitOps {
new GitOpsImpl(workingDirectory)
}

private implicit def implicitPathToAbsoluteFileMatcher(
f: Path => Boolean
): AbsoluteFile => Boolean = (x: AbsoluteFile) => f(x.path)

private def getMatchingFiles(
files: Seq[AbsoluteFile],
respectProjectFilters: Boolean,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
)(listDir: Seq[AbsoluteFile] => Seq[AbsoluteFile]): Seq[AbsoluteFile] = {
val matchingFiles = Seq.newBuilder[AbsoluteFile]
val dirs = Seq.newBuilder[AbsoluteFile]
Expand All @@ -27,7 +32,7 @@ object GitOps {
// DESNOTE(2017-05-19, pjrt): A plain, fully passed file will (try to) be
// formatted regardless of what it is or where it is.
// NB: Unless respectProjectFilters is also specified.
else if (!respectProjectFilters || matches(x)) matchingFiles += x
else if (!respectProjectFilters || matches(x.path)) matchingFiles += x
}
matchingFiles.result() ++ listDir(dirs.result()).filter(matches)
}
Expand All @@ -39,38 +44,38 @@ object GitOps {
.getCanonicalConfigFile(obj.workingDirectory, config)
.orElse(obj.rootDir.flatMap(FileOps.tryGetConfigInDir))

def getFiles(matches: AbsoluteFile => Boolean): Seq[AbsoluteFile] =
def getFiles(matches: Path => Boolean): Seq[AbsoluteFile] =
obj.lsTree(obj.workingDirectory).filter(matches)

def getFiles(
files: Seq[AbsoluteFile],
respectProjectFilters: Boolean,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
): Seq[AbsoluteFile] =
getMatchingFiles(files, respectProjectFilters, matches)(obj.lsTree)

def getDirFiles(matches: AbsoluteFile => Boolean): Seq[AbsoluteFile] =
def getDirFiles(matches: Path => Boolean): Seq[AbsoluteFile] =
obj.workingDirectory.listFiles.filter(matches)

def getDirFiles(
files: Seq[AbsoluteFile],
respectProjectFilters: Boolean,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
): Seq[AbsoluteFile] =
getMatchingFiles(files, respectProjectFilters, matches)(
_.flatMap(_.listFiles)
)

def getDiffFiles(
branch: String,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
): Seq[AbsoluteFile] =
obj.diff(branch).filter(matches)

def getDiffFiles(
branch: String,
respectProjectFilters: Boolean,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
)(files: Seq[AbsoluteFile]): Seq[AbsoluteFile] =
getMatchingFiles(files, respectProjectFilters, matches)(x =>
obj.diff(branch, x: _*)
Expand All @@ -80,13 +85,13 @@ object GitOps {
branch: String,
files: Seq[Path],
respectProjectFilters: Boolean,
matches: AbsoluteFile => Boolean
matches: Path => Boolean
): Seq[AbsoluteFile] =
getDiffFiles(branch, respectProjectFilters, matches)(
obj.workingDirectory.join(files)
)

def getChangedFiles(matches: AbsoluteFile => Boolean): Seq[AbsoluteFile] =
def getChangedFiles(matches: Path => Boolean): Seq[AbsoluteFile] =
obj.status().filter(matches)

}
Expand Down

0 comments on commit 341d136

Please sign in to comment.