From 96a7b1e2539eef43044f676f60400d22265fd115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nivaldo=20Bondan=C3=A7a?= Date: Tue, 11 Jun 2024 11:33:34 -0700 Subject: [PATCH] Default style is the Meta style, making that explicit Summary: Instead of having this implied, let's make it explicit what is the default style Reviewed By: strulovich Differential Revision: D58395863 fbshipit-source-id: 46a9f0c2d79b673e668e17b4f9ef2cbe373119e0 --- CHANGELOG.md | 1 + core/src/main/java/com/facebook/ktfmt/cli/Main.kt | 2 +- .../src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt | 3 ++- .../main/java/com/facebook/ktfmt/format/Formatter.kt | 6 ++++-- .../test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt | 10 ++++++++-- .../java/com/facebook/ktfmt/format/FormatterTest.kt | 7 ++++--- .../java/com/facebook/ktfmt/testutil/KtfmtTruth.kt | 4 ++-- .../com/facebook/ktfmt/intellij/UiFormatterStyle.java | 3 ++- 8 files changed, 24 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc7a079d..c7acfee1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). ### Changed - Preserves blank spaces between when clauses (https://github.com/facebook/ktfmt/issues/342) +- Named the default style as `Formatter.META_FORMAT` / `--meta-style` ### Fixed - Compilation issues with online formatter (https://github.com/facebook/ktfmt/commit/8605080cb0aadb7eaba20f3b469d6ddafe32c941) diff --git a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt index c305bf15..7f57aa89 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/Main.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/Main.kt @@ -37,7 +37,7 @@ private const val EXIT_CODE_SUCCESS = 0 private val USAGE = """ - Usage: ktfmt [--google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=] [--do-not-remove-unused-imports] File1.kt File2.kt ... + Usage: ktfmt [--meta-style | --google-style | --kotlinlang-style] [--dry-run] [--set-exit-if-changed] [--stdin-name=] [--do-not-remove-unused-imports] File1.kt File2.kt ... Or: ktfmt @file """ .trimIndent() diff --git a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt index 041cb407..41a6e30b 100644 --- a/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt +++ b/core/src/main/java/com/facebook/ktfmt/cli/ParsedArgs.kt @@ -51,7 +51,7 @@ data class ParsedArgs( /** parseOptions parses command-line arguments passed to ktfmt. */ fun parseOptions(args: Array): ParseResult { val fileNames = mutableListOf() - var formattingOptions = FormattingOptions() + var formattingOptions = Formatter.META_FORMAT var dryRun = false var setExitIfChanged = false var removeUnusedImports = true @@ -59,6 +59,7 @@ data class ParsedArgs( for (arg in args) { when { + arg == "--meta-style" -> formattingOptions = Formatter.META_FORMAT arg == "--google-style" -> formattingOptions = Formatter.GOOGLE_FORMAT arg == "--kotlinlang-style" -> formattingOptions = Formatter.KOTLINLANG_FORMAT arg == "--dry-run" || arg == "-n" -> dryRun = true diff --git a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt index 9f29e0f0..a8b3560e 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt @@ -42,6 +42,8 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset object Formatter { + @JvmField val META_FORMAT = FormattingOptions() + @JvmField val GOOGLE_FORMAT = FormattingOptions( @@ -66,7 +68,7 @@ object Formatter { */ @JvmStatic @Throws(FormatterException::class, ParseError::class) - fun format(code: String): String = format(FormattingOptions(), code) + fun format(code: String): String = format(META_FORMAT, code) /** * format formats the Kotlin code given in 'code' with 'removeUnusedImports' and returns it as a @@ -75,7 +77,7 @@ object Formatter { @JvmStatic @Throws(FormatterException::class, ParseError::class) fun format(code: String, removeUnusedImports: Boolean): String = - format(FormattingOptions(removeUnusedImports = removeUnusedImports), code) + format(META_FORMAT.copy(removeUnusedImports = removeUnusedImports), code) /** * format formats the Kotlin code given in 'code' with the 'maxWidth' and returns it as a string. diff --git a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt index 2e27f3f2..8f252e6d 100644 --- a/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/cli/ParsedArgsTest.kt @@ -56,10 +56,16 @@ class ParsedArgsTest { val formattingOptions = parsed.formattingOptions - val defaultFormattingOptions = FormattingOptions() + val defaultFormattingOptions = Formatter.META_FORMAT assertThat(formattingOptions).isEqualTo(defaultFormattingOptions) } + @Test + fun `parseOptions recognizes --meta-style`() { + val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--meta-style", "foo.kt"))) + assertThat(parsed.formattingOptions).isEqualTo(Formatter.META_FORMAT) + } + @Test fun `parseOptions recognizes --dropbox-style`() { val parsed = assertSucceeds(ParsedArgs.parseOptions(arrayOf("--kotlinlang-style", "foo.kt"))) @@ -199,7 +205,7 @@ class ParsedArgsTest { private fun parseResultOk( fileNames: List = emptyList(), - formattingOptions: FormattingOptions = FormattingOptions(), + formattingOptions: FormattingOptions = Formatter.META_FORMAT, dryRun: Boolean = false, setExitIfChanged: Boolean = false, removedUnusedImports: Boolean = true, diff --git a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt index 4e8acc27..c54b564c 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -16,6 +16,7 @@ package com.facebook.ktfmt.format +import com.facebook.ktfmt.format.Formatter.META_FORMAT import com.facebook.ktfmt.testutil.assertFormatted import com.facebook.ktfmt.testutil.assertThatFormatting import com.google.common.truth.Truth.assertThat @@ -1470,7 +1471,7 @@ class FormatterTest { .trimMargin() assertThatFormatting(code) - .withOptions(FormattingOptions(removeUnusedImports = false)) + .withOptions(META_FORMAT.copy(removeUnusedImports = false)) .isEqualTo(code) } @@ -4281,7 +4282,7 @@ class FormatterTest { |} |""" .trimMargin() - assertThatFormatting(code).withOptions(FormattingOptions(maxWidth = 22)).isEqualTo(expected) + assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 22)).isEqualTo(expected) } @Test @@ -5366,7 +5367,7 @@ class FormatterTest { |class MyClass {} |""" .trimMargin() - assertThatFormatting(code).withOptions(FormattingOptions(maxWidth = 33)).isEqualTo(expected) + assertThatFormatting(code).withOptions(META_FORMAT.copy(maxWidth = 33)).isEqualTo(expected) } @Test diff --git a/core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt b/core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt index 1e992379..ba80d89c 100644 --- a/core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt +++ b/core/src/test/java/com/facebook/ktfmt/testutil/KtfmtTruth.kt @@ -44,7 +44,7 @@ import org.junit.Assert */ fun assertFormatted( @Language("kts") code: String, - formattingOptions: FormattingOptions = FormattingOptions(), + formattingOptions: FormattingOptions = Formatter.META_FORMAT, deduceMaxWidth: Boolean = false, ) { val first = code.lines().first() @@ -81,7 +81,7 @@ fun assertThatFormatting(@Language("kts") code: String): FormattedCodeSubject { class FormattedCodeSubject(metadata: FailureMetadata, private val code: String) : Subject(metadata, code) { - private var options: FormattingOptions = FormattingOptions() + private var options: FormattingOptions = Formatter.META_FORMAT private var allowTrailingWhitespace = false fun withOptions(options: FormattingOptions): FormattedCodeSubject { diff --git a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/UiFormatterStyle.java b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/UiFormatterStyle.java index 243aaff5..ac7ae57a 100644 --- a/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/UiFormatterStyle.java +++ b/ktfmt_idea_plugin/src/main/java/com/facebook/ktfmt/intellij/UiFormatterStyle.java @@ -18,12 +18,13 @@ import static com.facebook.ktfmt.format.Formatter.GOOGLE_FORMAT; import static com.facebook.ktfmt.format.Formatter.KOTLINLANG_FORMAT; +import static com.facebook.ktfmt.format.Formatter.META_FORMAT; import com.facebook.ktfmt.format.FormattingOptions; /** Configuration options for the formatting style. */ enum UiFormatterStyle { - DEFAULT("Default", new FormattingOptions()), + META("Meta (default)", META_FORMAT), GOOGLE("Google (internal)", GOOGLE_FORMAT), KOTLINLANG("Kotlinlang", KOTLINLANG_FORMAT);