Skip to content

Commit

Permalink
Add --check cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
droptheplot committed Aug 15, 2019
1 parent 143e9fe commit 486d403
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ object CliArgParser {
opt[Unit]("test")
.action((_, c) => c.copy(testing = true))
.text("test for mis-formatted code, exits with status 1 on failure.")
opt[Unit]("check")
.action((_, c) => c.copy(check = true))
.text(
"test for mis-formatted code, exits with status 1 on first failure."
)
opt[File]("migrate2hocon")
.action(
(file, c) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ case class CliOptions(
quiet: Boolean = false,
stdIn: Boolean = false,
noStdErr: Boolean = false,
list: Boolean = false
list: Boolean = false,
check: Boolean = false
) {
// These default values are copied from here.
// https://github.com/scalameta/scalafmt/blob/f2154330afa0bc4a0a556598adeb116eafecb8e3/scalafmt-core/shared/src/main/scala/org/scalafmt/config/ScalafmtConfig.scala#L127-L162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object InputMethod {
options: CliOptions
): ExitCode = {
val codeChanged = formatted != original
if (options.testing) {
if (options.testing || options.check) {
if (codeChanged) {
throw MisformattedFile(
new File(filename),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.scalafmt.interfaces.Scalafmt
import org.scalafmt.util.AbsoluteFile

import scala.meta.internal.tokenizers.PlatformTokenizerCache
import util.control.Breaks._

object ScalafmtDynamicRunner extends ScalafmtRunner {
override private[cli] def run(
Expand Down Expand Up @@ -44,22 +45,26 @@ object ScalafmtDynamicRunner extends ScalafmtRunner {
val termDisplay = newTermDisplay(options, inputMethods, termDisplayMessage)

val exitCode = new AtomicReference(ExitCode.Ok)
inputMethods.foreach { inputMethod =>
val instance =
// Use scalafmt-dynamic that ignores exclude filters for fully qualified paths
if (fqpns.contains(inputMethod)) scalafmtInstanceIgnoreFilters
else scalafmtInstance
try {
val code = handleFile(inputMethod, instance, options)
exitCode.getAndUpdate(new UnaryOperator[ExitCode] {
override def apply(t: ExitCode): ExitCode =
ExitCode.merge(code, t)
})
} catch {
case e: MisformattedFile => reporter.error(e.file.toPath, e)
breakable {
inputMethods.foreach { inputMethod =>
val instance =
// Use scalafmt-dynamic that ignores exclude filters for fully qualified paths
if (fqpns.contains(inputMethod)) scalafmtInstanceIgnoreFilters
else scalafmtInstance
try {
val code = handleFile(inputMethod, instance, options)
exitCode.getAndUpdate(new UnaryOperator[ExitCode] {
override def apply(t: ExitCode): ExitCode =
ExitCode.merge(code, t)
})
} catch {
case e: MisformattedFile =>
reporter.error(e.file.toPath, e)
if (options.check) break
}
PlatformTokenizerCache.megaCache.clear()
termDisplay.taskProgress(termDisplayMessage, counter.incrementAndGet())
}
PlatformTokenizerCache.megaCache.clear()
termDisplay.taskProgress(termDisplayMessage, counter.incrementAndGet())
}

val exit = ExitCode.merge(exitCode.get, reporter.getExitCode)
Expand Down

0 comments on commit 486d403

Please sign in to comment.