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

feat(cli): support max width arg #470

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion core/src/main/java/com/facebook/ktfmt/cli/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Main(
}
if (parsedArgs.fileNames.isEmpty()) {
err.println(
"Usage: ktfmt [--dropbox-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...")
"Usage: ktfmt [--dropbox-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--max-width=<number>] [--stdin-name=<name>] [--do-not-remove-unused-imports] File1.kt File2.kt ...")
err.println("Or: ktfmt @file")
return 1
}
Expand Down
24 changes: 17 additions & 7 deletions core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ data class ParsedArgs(
var setExitIfChanged = false
var removeUnusedImports = true
var stdinName: String? = null
var maxWidth = FormattingOptions.DEFAULT_MAX_WIDTH

for (arg in args) {
when {
Expand All @@ -63,6 +64,11 @@ data class ParsedArgs(
arg == "--dry-run" || arg == "-n" -> dryRun = true
arg == "--set-exit-if-changed" -> setExitIfChanged = true
arg == "--do-not-remove-unused-imports" -> removeUnusedImports = false
arg.startsWith("--max-width=") ->
maxWidth = parseKeyValueArg("--max-width", arg)?.let {
try { it.toInt() } catch (e: NumberFormatException) { null }
}
?: return ParseResult.Error("Found option '${arg}', expected '${"--max-width"}=<number>'")
arg.startsWith("--stdin-name=") ->
stdinName =
parseKeyValueArg("--stdin-name", arg)
Expand All @@ -75,13 +81,17 @@ data class ParsedArgs(
}

return ParseResult.Ok(
ParsedArgs(
fileNames,
formattingOptions.copy(removeUnusedImports = removeUnusedImports),
dryRun,
setExitIfChanged,
stdinName,
))
ParsedArgs(
fileNames,
formattingOptions.copy(
removeUnusedImports = removeUnusedImports,
maxWidth = maxWidth,
),
dryRun,
setExitIfChanged,
stdinName,
)
)
}

private fun parseKeyValueArg(key: String, arg: String): String? {
Expand Down
18 changes: 18 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ class ParsedArgsTest {
assertThat(parsed.formattingOptions.removeUnusedImports).isFalse()
}

@Test
fun `parseOptions recognizes --max-width`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--max-width=120")))
assertThat(parsed.formattingOptions.maxWidth).isEqualTo(120)
}

@Test
fun `parseOptions accepts --max-width with empty value`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--max-width="))
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions --max-width without value`() {
val parseResult = ParsedArgs.parseOptions(arrayOf("--max-width"))
assertThat(parseResult).isInstanceOf(ParseResult.Error::class.java)
}

@Test
fun `parseOptions recognizes --stdin-name`() {
val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--stdin-name=my/foo.kt")))
Expand Down