Skip to content

Commit af79a68

Browse files
committed
PathMatcher: move to sysops, for sbt-scalafmt
1 parent 73edbc5 commit af79a68

File tree

11 files changed

+61
-69
lines changed

11 files changed

+61
-69
lines changed

scalafmt-cli/shared/src/main/scala/org/scalafmt/cli/ScalafmtCoreRunner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ object ScalafmtCoreRunner extends ScalafmtRunner {
2323
ProjectFiles.FileMatcher(scalafmtConf.project, options.customExcludes),
2424
)
2525
catch {
26-
case e: ScalafmtConfigException =>
26+
case e @ (_: ScalafmtConfigException | _: sysops.ScalafmtSysException) =>
2727
options.common.err.println(e.getMessage)
2828
ExitCode.UnexpectedError.future
2929
}

scalafmt-cli/shared/src/test/scala/org/scalafmt/cli/CliTest.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ class CliTest extends AbstractCliTest with CliTestBehavior {
808808
assertOut = out =>
809809
assertContains(
810810
out,
811-
"""|Illegal regex in configuration: .*foo(
812-
|reason: Unclosed group near index 6
811+
"""|Invalid config: Invalid path patcher regex: /.*foo(/; Unclosed group near index 6
813812
|.*foo(
814813
|""".stripMargin,
815814
),

scalafmt-core/jvm-native/src/main/scala/org/scalafmt/config/PlatformPathMatcher.scala

Lines changed: 0 additions & 16 deletions
This file was deleted.

scalafmt-core/shared/src/main/scala/org/scalafmt/config/PathMatcher.scala

Lines changed: 0 additions & 30 deletions
This file was deleted.

scalafmt-core/shared/src/main/scala/org/scalafmt/config/ProjectFiles.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.scalafmt.config
22

3-
import org.scalafmt.sysops.AbsoluteFile
4-
import org.scalafmt.sysops.FileOps
5-
import org.scalafmt.sysops.OsSpecific._
3+
import org.scalafmt.sysops._
64

75
import scala.meta.Dialect
86
import scala.meta.dialects
@@ -60,7 +58,7 @@ object ProjectFiles {
6058
}
6159

6260
private def create(seq: Seq[String], f: String => PathMatcher) = seq
63-
.map(inPathMatcherForm).distinct.map(f)
61+
.map(OsSpecific.inPathMatcherForm).distinct.map(f)
6462
private def nio(seq: Seq[String]) = create(seq, PlatformPathMatcher.apply)
6563
private def regex(seq: Seq[String]) = create(seq, PathMatcher.Regex.apply)
6664

scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package org.scalafmt.config
22

33
import org.scalafmt.Versions
44
import org.scalafmt.rewrite._
5-
import org.scalafmt.sysops.AbsoluteFile
6-
import org.scalafmt.sysops.OsSpecific._
5+
import org.scalafmt.sysops._
76
import org.scalafmt.util._
87

98
import scala.meta._
@@ -202,7 +201,7 @@ case class ScalafmtConfig(
202201
val langResult = patStyles.collect { case (Left(lang), cfg) => lang -> cfg }
203202
val pmResult = patStyles.collect { case (Right(pat), cfg) =>
204203
val pattern =
205-
if (pat(0) == '.') "glob:**" + pat else inPathMatcherForm(pat)
204+
if (pat(0) == '.') "glob:**" + pat else OsSpecific.inPathMatcherForm(pat)
206205
PlatformPathMatcher(pattern) -> cfg
207206
}
208207
(langResult, pmResult)

scalafmt-dynamic/jvm/src/test/scala/org/scalafmt/dynamic/DynamicSuite.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,9 @@ class DynamicSuite extends FunSuite {
277277
|]
278278
|""".stripMargin,
279279
)
280-
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
281280
assertNoDiff(
282-
err.takeRight(120),
283-
"""|Invalid config: Illegal regex in configuration: .*foo(
284-
|reason: Unclosed group near index 6
281+
f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage,
282+
"""|Invalid config: Invalid path patcher regex: /.*foo(/; Unclosed group near index 6
285283
|.*foo(
286284
|""".stripMargin,
287285
)
@@ -297,10 +295,7 @@ class DynamicSuite extends FunSuite {
297295
|""".stripMargin,
298296
)
299297
val err = f.assertThrows[ScalafmtDynamicError.ConfigParseError]().getMessage
300-
assertNoDiff(
301-
err.takeRight(120),
302-
"Invalid config: Illegal pattern in configuration: foo.scala",
303-
)
298+
assertNoDiff(err, "Invalid config: Invalid path matcher pattern: foo.scala")
304299
}
305300

306301
check("config-cache") { f =>

scalafmt-core/js/src/main/scala/org/scalafmt/config/PlatformPathMatcher.scala renamed to scalafmt-sysops/js/src/main/scala/org/scalafmt/sysops/PlatformPathMatcher.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package org.scalafmt.config
2-
3-
import org.scalafmt.sysops.OsSpecific
1+
package org.scalafmt.sysops
42

53
object PlatformPathMatcher {
64
private def fail(pattern: String, msg: String): Nothing =
7-
throw new ScalafmtConfigException(
8-
s"Illegal pattern in configuration: $pattern; $msg",
5+
throw new ScalafmtSysException(
6+
s"Invalid path matcher pattern: $pattern; $msg",
97
)
108
def apply(pattern: String): PathMatcher = {
119
val colon = pattern.indexOf(':')
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.scalafmt.sysops
2+
3+
import java.nio.file
4+
5+
object PlatformPathMatcher {
6+
val fs: file.FileSystem = file.FileSystems.getDefault
7+
8+
def apply(pattern: String): PathMatcher = {
9+
try fs.getPathMatcher(pattern)
10+
catch {
11+
case e: IllegalArgumentException =>
12+
val sb = new StringBuilder()
13+
sb.append("Invalid path matcher pattern: ").append(pattern)
14+
val err = e.getMessage
15+
if (null != err && err.nonEmpty) sb.append("; ").append(err)
16+
throw new ScalafmtSysException(sb.toString())
17+
}
18+
}.matches
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.scalafmt.sysops
2+
3+
import java.nio.file.Path
4+
import java.util.regex.Pattern
5+
6+
trait PathMatcher {
7+
def matches(path: Path): Boolean
8+
}
9+
10+
object PathMatcher {
11+
12+
class Regex(pattern: Pattern) extends PathMatcher {
13+
def matches(path: Path): Boolean = pattern.matcher(path.toString).find()
14+
}
15+
16+
object Regex {
17+
def apply(regex: String): Regex =
18+
try new Regex(Pattern.compile(regex))
19+
catch {
20+
case e: Throwable =>
21+
val msg = s"Invalid path patcher regex: /$regex/; ${e.getMessage}"
22+
throw new ScalafmtSysException(msg)
23+
}
24+
25+
}
26+
27+
}

0 commit comments

Comments
 (0)