diff --git a/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtRunner.scala b/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtRunner.scala index 721a8615d9..2522f3ecc5 100644 --- a/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtRunner.scala +++ b/scalafmt-cli/src/main/scala/org/scalafmt/cli/ScalafmtRunner.scala @@ -2,7 +2,7 @@ package org.scalafmt.cli import java.io.OutputStreamWriter -import org.scalafmt.util.{AbsoluteFile, FileOps} +import org.scalafmt.util.AbsoluteFile trait ScalafmtRunner { private[cli] def run( @@ -65,7 +65,7 @@ trait ScalafmtRunner { filesWithFetch( options.files, if (m == GitFiles) options.gitOps.lsTree - else FileOps.listFiles + else _.listFiles ) case DiffFiles(branch) => diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala index 62cd00e527..bb0970acfa 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/AbsoluteFile.scala @@ -3,6 +3,8 @@ package org.scalafmt.util import java.io.File import java.nio.file.Path +import scala.io.Codec + /** Wrapper around java.io.File with an absolute path. */ sealed abstract case class AbsoluteFile(jfile: File) { def path: String = jfile.getPath @@ -18,6 +20,11 @@ sealed abstract case class AbsoluteFile(jfile: File) { @inline def isRegularFile: Boolean = FileOps.isRegularFile(asPath) + @inline def listFiles: Seq[AbsoluteFile] = FileOps.listFiles(jfile).map(/) + @inline def readFile(implicit codec: Codec): String = FileOps.readFile(asPath) + @inline def writeFile(content: String)(implicit codec: Codec): Unit = + FileOps.writeFile(asPath, content) + override def toString(): String = path } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/FileOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/FileOps.scala index 612199dc61..c218d04466 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/FileOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/FileOps.scala @@ -18,10 +18,6 @@ object FileOps { listFiles(new File(path)) } - def listFiles(file: AbsoluteFile): Vector[AbsoluteFile] = { - listFiles(file.jfile).map(file./) - } - def listFiles(file: File): Vector[String] = { if (file.isFile) { Vector(file.getAbsolutePath) @@ -50,10 +46,6 @@ object FileOps { } } - def readFile(file: AbsoluteFile)(implicit codec: Codec): String = { - readFile(file.asPath) - } - def readFile(file: Path)(implicit codec: Codec): String = { new String(Files.readAllBytes(file), codec.charSet) } @@ -61,12 +53,6 @@ object FileOps { def getFile(path: String*): Path = Paths.get(path.head, path.tail: _*) - def writeFile(file: AbsoluteFile, content: String)(implicit - codec: Codec - ): Unit = { - writeFile(file.asPath, content) - } - def writeFile(path: Path, content: String)(implicit codec: Codec): Unit = { val bytes = content.getBytes(codec.charSet) Files.write(path, bytes) diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/ScalafmtProps.scala b/scalafmt-tests/src/test/scala/org/scalafmt/ScalafmtProps.scala index 57fd11748d..9db9c991ad 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/ScalafmtProps.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/ScalafmtProps.scala @@ -7,7 +7,6 @@ import scala.meta.testkit._ import org.scalafmt.CompatCollections.ParConverters._ import org.scalafmt.config.ScalafmtConfig import org.scalafmt.util.AbsoluteFile -import org.scalafmt.util.FileOps import org.scalafmt.util.FormatAssertions import org.scalameta.logger import munit.FunSuite @@ -102,10 +101,7 @@ class ScalafmtProps extends FunSuite with FormatAssertions { logger.elem(summary) logger.elem(report) logger.elem(summary) - FileOps.writeFile( - AbsoluteFile.userDir / "target" / "scalafmt-props.md", - report - ) + (AbsoluteFile.userDir / "target" / "scalafmt-props.md").writeFile(report) } } diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala index 3b00fd0a76..0f3f2f5b00 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/cli/CliTest.scala @@ -445,7 +445,7 @@ trait CliTestBehavior { this: AbstractCliTest => } val config = Cli.getConfig(Array("foo.scala"), options).get Cli.run(config) - val obtained = FileOps.readFile(workingDir / "foo.scala") + val obtained = (workingDir / "foo.scala").readFile assertNoDiff(obtained, expected) } diff --git a/scalafmt-tests/src/test/scala/org/scalafmt/cli/FakeGitOps.scala b/scalafmt-tests/src/test/scala/org/scalafmt/cli/FakeGitOps.scala index 82411b9435..bdb661f3f1 100644 --- a/scalafmt-tests/src/test/scala/org/scalafmt/cli/FakeGitOps.scala +++ b/scalafmt-tests/src/test/scala/org/scalafmt/cli/FakeGitOps.scala @@ -1,12 +1,10 @@ package org.scalafmt.cli import org.scalafmt.util.AbsoluteFile -import org.scalafmt.util.FileOps import org.scalafmt.util.GitOps class FakeGitOps(root: AbsoluteFile) extends GitOps { - override def lsTree(dir: AbsoluteFile): Vector[AbsoluteFile] = - FileOps.listFiles(dir) + override def lsTree(dir: AbsoluteFile): Seq[AbsoluteFile] = dir.listFiles override def rootDir: Option[AbsoluteFile] = Some(root) override def status: Seq[AbsoluteFile] = lsTree(root) override def diff(