Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbsoluteFile: add apply(String*) #3708

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading