Skip to content

Commit

Permalink
AbsoluteFile: add apply(String*)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Nov 26, 2023
1 parent e46fe8d commit 69d718f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.scalafmt.config.RewriteScala3Settings._
import org.scalafmt.rewrite.FormatTokensRewrite
import org.scalafmt.rewrite.RedundantBraces
import org.scalafmt.sysops.AbsoluteFile
import org.scalafmt.sysops.FileOps
import org.scalafmt.sysops.OsSpecific._
import org.scalafmt.util.LoggerOps
import org.scalafmt.util.ValidationOps
Expand Down Expand Up @@ -209,9 +208,7 @@ case class ScalafmtConfig(
}

def getConfigFor(filename: String): Try[ScalafmtConfig] = {
val absfile = AbsoluteFile(FileOps.getFile(filename))
@inline def otherDialect(style: ScalafmtConfig): Boolean =
!style.dialect.isEquivalentTo(dialect)
val absfile = AbsoluteFile(filename)
def onLang[A](f: (ProjectFiles.Layout, String) => A): Option[A] =
project.layout.flatMap { layout =>
layout.getLang(absfile).map { lang => f(layout, lang) }
Expand All @@ -223,7 +220,7 @@ case class ScalafmtConfig(
}
val pmStyle = pmStyles.collectFirst {
case (pm, style) if pm.matches(absfile.path) =>
if (otherDialect(style)) style
if (!style.dialect.isEquivalentTo(dialect)) style
else
style.withDialect(onLang {
_.getDialectByLang(_)(style.dialect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ object AbsoluteFile {
def apply(file: File): AbsoluteFile = apply(file.toPath)
def apply(path: Path): AbsoluteFile = new AbsoluteFile(path.toAbsolutePath)

@inline def apply(path: Seq[String]): AbsoluteFile =
apply(FileOps.getFile(path))
@inline def apply(head: String, tail: String*): AbsoluteFile =
apply(FileOps.getPath(head, tail: _*))

def fromPathIfAbsolute(path: String): Option[AbsoluteFile] = {
val file = FileOps.getFile(path)
if (file.isAbsolute) Some(new AbsoluteFile(file)) else None
}

def userDir = AbsoluteFile(FileOps.getFile(System.getProperty("user.dir")))
def userDir = AbsoluteFile(System.getProperty("user.dir"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private class GitOpsImpl(val workingDirectory: AbsoluteFile) extends GitOps {
private lazy val tryRoot: Try[AbsoluteFile] = {
val cmd = Seq("git", "rev-parse", "--show-toplevel")
Try(execImpl(cmd)).flatMap { x =>
val file = AbsoluteFile(FileOps.getFile(x))
val file = AbsoluteFile(x)
if (file.isDirectory) Success(file)
else Failure(new InvalidPathException(x, "not a directory"))
}
Expand Down

0 comments on commit 69d718f

Please sign in to comment.