Skip to content

Commit

Permalink
CliArgParser: allow additional values for --mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 5, 2022
1 parent 8e279bd commit 7b736df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ object CliArgParser {
.text(
s"""Sets the files to be formatted fetching mode.
|Options:
| diff - format files listed in `git diff` against master
| changed - format files listed in `git status` (latest changes against previous commit)""".stripMargin
|${FileFetchMode.help}
|""".stripMargin
)
opt[String]("diff-branch")
.action((branch, c) => c.copy(mode = Option(DiffFiles(branch))))
Expand Down
39 changes: 31 additions & 8 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/FileFetchMode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,59 @@ import scopt.Read

/** Determines how we fetch files for formatting
*/
sealed trait FileFetchMode
sealed trait FileFetchMode {
def desc: String
}

object FileFetchMode {

private val available: Map[String, FileFetchMode] = Map(
"diff" -> DiffFiles("master"),
"changed" -> ChangedFiles,
"any" -> RecursiveSearch,
"anygit" -> GitFiles
)

def help = available
.map { case (k, v) => s"$k: ${v.desc}" }
.mkString(" ", "\n ", "")

/** The read instance is practically is not exhaustive due to the
* RecursiveSearch and GitFiles are the fallback used in the absence of other
* options
*/
implicit val read: Read[FileFetchMode] = Read.reads {
case "diff" => DiffFiles("master")
case "changed" => ChangedFiles
implicit val read: Read[FileFetchMode] = Read.reads { x =>
available
.getOrElse(x, throw new IllegalArgumentException(s"unknown mode: $x"))
}

}

/** A simple recursive strategy where each directory is expanded
*/
case object RecursiveSearch extends FileFetchMode
case object RecursiveSearch extends FileFetchMode {
def desc: String = "format any files found in current directory"
}

/** A call to `git ls-files --name-only <dir>`
*/
case object GitFiles extends FileFetchMode
case object GitFiles extends FileFetchMode {
def desc: String = "format any git-tracked files found in current directory"
}

/** A call to `git diff --name-only --diff-filter=d <branch>`
*
* When this is set, files passed via the cli are ignored.
*/
final case class DiffFiles(branch: String) extends FileFetchMode
final case class DiffFiles(branch: String) extends FileFetchMode {
def desc: String = s"format files listed in `git diff` against $branch"
}

/** A call to `git status --porcelain`
*
* When this is set, files passed via the cli are ignored.
*/
case object ChangedFiles extends FileFetchMode
case object ChangedFiles extends FileFetchMode {
def desc: String =
"format files listed in `git status` (latest changes against previous commit)"
}

0 comments on commit 7b736df

Please sign in to comment.