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

CliArgParser: allow additional values for --mode #3108

Merged
merged 1 commit into from
Feb 5, 2022
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 @@ -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)"
}