From 91a577a8e097acb48e5c1a51c72b488c40738e9c Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 28 Nov 2021 23:47:02 -0800 Subject: [PATCH] FileOps: move getFileMatcher from dynamic runner --- .../scalafmt/cli/ScalafmtDynamicRunner.scala | 19 +--------------- .../scala/org/scalafmt/sysops/FileOps.scala | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtDynamicRunner.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtDynamicRunner.scala index 1c438c0c39..0e5bf17fc8 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtDynamicRunner.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtDynamicRunner.scala @@ -1,6 +1,5 @@ package org.scalafmt.cli -import java.io.File import java.nio.file.Path import java.util.concurrent.atomic.{AtomicInteger, AtomicReference} @@ -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) @@ -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 - ) - } - } - } - } diff --git a/scalafmt-sysops/shared/src/main/scala/org/scalafmt/sysops/FileOps.scala b/scalafmt-sysops/shared/src/main/scala/org/scalafmt/sysops/FileOps.scala index 73d72c5e9e..6719a83f6e 100644 --- a/scalafmt-sysops/shared/src/main/scala/org/scalafmt/sysops/FileOps.scala +++ b/scalafmt-sysops/shared/src/main/scala/org/scalafmt/sysops/FileOps.scala @@ -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) + } + } + } + } + }