Skip to content

Commit

Permalink
FileOps: move getFileMatcher from dynamic runner
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 29, 2021
1 parent b88bb78 commit 91a577a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.scalafmt.cli

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

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

private def getFileMatcher(paths: Seq[Path]): Path => Boolean = {
require(paths.nonEmpty)
val (files, dirs) = paths.partition(FileOps.isRegularFile)
(x: Path) =>
files.contains(x) || {
val filename = x.toString()
dirs.exists { dir =>
val dirname = dir.toString()
filename.startsWith(dirname) && (
filename.length == dirname.length ||
filename.charAt(dirname.length) == File.separatorChar
)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,26 @@ object FileOps {
else if (file.isRegularFile) Some(Success(file.path))
else Some(Failure(new AccessDeniedException(s"Config not a file: $file")))

def getFileMatcher(paths: Seq[Path]): Path => Boolean = {
val dirBuilder = Seq.newBuilder[Path]
val fileBuilder = Set.newBuilder[Path]
paths.foreach { path =>
if (isRegularFile(path)) fileBuilder += path else dirBuilder += path
}
val dirs = dirBuilder.result()
val files = fileBuilder.result()
x =>
files(x) || {
val filename = x.toString()
val sep = x.getFileSystem.getSeparator
dirs.exists { dir =>
val dirname = dir.toString()
filename.startsWith(dirname) && {
filename.length == dirname.length ||
filename.startsWith(sep, dirname.length)
}
}
}
}

}

0 comments on commit 91a577a

Please sign in to comment.