Skip to content

Commit

Permalink
PathMatcher: move to sysops, for sbt-scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 21, 2025
1 parent 73edbc5 commit af79a68
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object ScalafmtCoreRunner extends ScalafmtRunner {
ProjectFiles.FileMatcher(scalafmtConf.project, options.customExcludes),
)
catch {
case e: ScalafmtConfigException =>
case e @ (_: ScalafmtConfigException | _: sysops.ScalafmtSysException) =>
options.common.err.println(e.getMessage)
ExitCode.UnexpectedError.future
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,7 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
assertOut = out =>
assertContains(
out,
"""|Illegal regex in configuration: .*foo(
|reason: Unclosed group near index 6
"""|Invalid config: Invalid path patcher regex: /.*foo(/; Unclosed group near index 6
|.*foo(
|""".stripMargin,
),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.scalafmt.config

import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.FileOps
import org.scalafmt.sysops.OsSpecific._
import org.scalafmt.sysops._

import scala.meta.Dialect
import scala.meta.dialects
Expand Down Expand Up @@ -60,7 +58,7 @@ object ProjectFiles {
}

private def create(seq: Seq[String], f: String => PathMatcher) = seq
.map(inPathMatcherForm).distinct.map(f)
.map(OsSpecific.inPathMatcherForm).distinct.map(f)
private def nio(seq: Seq[String]) = create(seq, PlatformPathMatcher.apply)
private def regex(seq: Seq[String]) = create(seq, PathMatcher.Regex.apply)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package org.scalafmt.config

import org.scalafmt.Versions
import org.scalafmt.rewrite._
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.OsSpecific._
import org.scalafmt.sysops._
import org.scalafmt.util._

import scala.meta._
Expand Down Expand Up @@ -202,7 +201,7 @@ case class ScalafmtConfig(
val langResult = patStyles.collect { case (Left(lang), cfg) => lang -> cfg }
val pmResult = patStyles.collect { case (Right(pat), cfg) =>
val pattern =
if (pat(0) == '.') "glob:**" + pat else inPathMatcherForm(pat)
if (pat(0) == '.') "glob:**" + pat else OsSpecific.inPathMatcherForm(pat)
PlatformPathMatcher(pattern) -> cfg
}
(langResult, pmResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,9 @@ class DynamicSuite extends FunSuite {
|]
|""".stripMargin,
)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
"""|Invalid config: Illegal regex in configuration: .*foo(
|reason: Unclosed group near index 6
f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage,
"""|Invalid config: Invalid path patcher regex: /.*foo(/; Unclosed group near index 6
|.*foo(
|""".stripMargin,
)
Expand All @@ -297,10 +295,7 @@ class DynamicSuite extends FunSuite {
|""".stripMargin,
)
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
assertNoDiff(
err.takeRight(120),
"Invalid config: Illegal pattern in configuration: foo.scala",
)
assertNoDiff(err, "Invalid config: Invalid path matcher pattern: foo.scala")
}

check("config-cache") { f =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.scalafmt.config

import org.scalafmt.sysops.OsSpecific
package org.scalafmt.sysops

object PlatformPathMatcher {
private def fail(pattern: String, msg: String): Nothing =
throw new ScalafmtConfigException(
s"Illegal pattern in configuration: $pattern; $msg",
throw new ScalafmtSysException(
s"Invalid path matcher pattern: $pattern; $msg",
)
def apply(pattern: String): PathMatcher = {
val colon = pattern.indexOf(':')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.scalafmt.sysops

import java.nio.file

object PlatformPathMatcher {
val fs: file.FileSystem = file.FileSystems.getDefault

def apply(pattern: String): PathMatcher = {
try fs.getPathMatcher(pattern)
catch {
case e: IllegalArgumentException =>
val sb = new StringBuilder()
sb.append("Invalid path matcher pattern: ").append(pattern)
val err = e.getMessage
if (null != err && err.nonEmpty) sb.append("; ").append(err)
throw new ScalafmtSysException(sb.toString())
}
}.matches
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.scalafmt.sysops

import java.nio.file.Path
import java.util.regex.Pattern

trait PathMatcher {
def matches(path: Path): Boolean
}

object PathMatcher {

class Regex(pattern: Pattern) extends PathMatcher {
def matches(path: Path): Boolean = pattern.matcher(path.toString).find()
}

object Regex {
def apply(regex: String): Regex =
try new Regex(Pattern.compile(regex))
catch {
case e: Throwable =>
val msg = s"Invalid path patcher regex: /$regex/; ${e.getMessage}"
throw new ScalafmtSysException(msg)
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.scalafmt.sysops

class ScalafmtSysException(e: String) extends Exception(e)

0 comments on commit af79a68

Please sign in to comment.