From c0511dfde0179d4e1e35ca34f3c9c909572b95ce Mon Sep 17 00:00:00 2001 From: generatedunixname89002005325672 Date: Mon, 13 Nov 2023 09:21:02 -0800 Subject: [PATCH] Reformat Kotlin files Reviewed By: cgrushko Differential Revision: D51248035 fbshipit-source-id: b612b96612929732960977a4cda55209155e5438 --- .../com/facebook/ktfmt/format/Formatter.kt | 20 ++++---- .../ktfmt/format/RedundantElementManager.kt | 6 +-- .../format/GoogleStyleFormatterKtTest.kt | 46 +++++++++---------- 3 files changed, 37 insertions(+), 35 deletions(-) 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 a4191c5c..c02f51a3 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/Formatter.kt @@ -19,8 +19,8 @@ package com.facebook.ktfmt.format import com.facebook.ktfmt.debughelpers.printOps import com.facebook.ktfmt.format.FormattingOptions.Style.DROPBOX import com.facebook.ktfmt.format.FormattingOptions.Style.GOOGLE -import com.facebook.ktfmt.format.RedundantElementManager.dropRedundantElements import com.facebook.ktfmt.format.RedundantElementManager.addRedundantElements +import com.facebook.ktfmt.format.RedundantElementManager.dropRedundantElements import com.facebook.ktfmt.format.WhitespaceTombstones.indexOfWhitespaceTombstone import com.facebook.ktfmt.kdoc.Escaping import com.facebook.ktfmt.kdoc.KDocCommentsHelper @@ -45,7 +45,9 @@ import org.jetbrains.kotlin.psi.psiUtil.startOffset object Formatter { @JvmField - val GOOGLE_FORMAT = FormattingOptions(style = GOOGLE, blockIndent = 2, continuationIndent = 2, manageTrailingCommas = true) + val GOOGLE_FORMAT = + FormattingOptions( + style = GOOGLE, blockIndent = 2, continuationIndent = 2, manageTrailingCommas = true) /** A format that attempts to reflect https://kotlinlang.org/docs/coding-conventions.html. */ @JvmField @@ -88,13 +90,13 @@ object Formatter { checkEscapeSequences(kotlinCode) return kotlinCode - .let { convertLineSeparators(it) } - .let { sortedAndDistinctImports(it) } - .let { dropRedundantElements(it, options) } - .let { prettyPrint(it, options, "\n") } - .let { addRedundantElements(it, options) } - .let { convertLineSeparators(it, Newlines.guessLineSeparator(kotlinCode)!!) } - .let { if (shebang.isEmpty()) it else shebang + "\n" + it } + .let { convertLineSeparators(it) } + .let { sortedAndDistinctImports(it) } + .let { dropRedundantElements(it, options) } + .let { prettyPrint(it, options, "\n") } + .let { addRedundantElements(it, options) } + .let { convertLineSeparators(it, Newlines.guessLineSeparator(kotlinCode)!!) } + .let { if (shebang.isEmpty()) it else shebang + "\n" + it } } /** prettyPrint reflows 'code' using google-java-format's engine. */ diff --git a/core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt b/core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt index c9713b5a..b6423f01 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/RedundantElementManager.kt @@ -28,9 +28,9 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset /** - * Adds and removes elements that are not strictly needed in the code, such as semicolons and - * unused imports. -*/ + * Adds and removes elements that are not strictly needed in the code, such as semicolons and unused + * imports. + */ object RedundantElementManager { /** Remove extra semicolons and unused imports, if enabled in the [options] */ fun dropRedundantElements(code: String, options: FormattingOptions): String { diff --git a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt index 78a9f082..7b6c6321 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -915,8 +915,8 @@ class GoogleStyleFormatterKtTest { @Test fun `tailing commas are removed when redundant`() { - val code = - """ + val code = + """ |fun main() { | fun foo() {} | @@ -930,9 +930,9 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin() + .trimMargin() val expected = - """ + """ |fun main() { | fun foo() {} | @@ -946,15 +946,15 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin() + .trimMargin() assertThatFormatting(code).withOptions(Formatter.GOOGLE_FORMAT).isEqualTo(expected) } @Test fun `tailing commas are added when missing`() { - // Use trailing comments to force the breaks - val code = - """ + // Use trailing comments to force the breaks + val code = + """ |fun main() { | fun < | A, @@ -992,7 +992,7 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin() + .trimMargin() val expected = """ |fun main() { @@ -1033,15 +1033,15 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin() + .trimMargin() assertThatFormatting(code).withOptions(Formatter.GOOGLE_FORMAT).isEqualTo(expected) } @Test fun `tailing commas that are always removed`() { - // Use trailing comments to force the breaks - val code = - """ + // Use trailing comments to force the breaks + val code = + """ |fun main() { | foo { | a, // @@ -1055,7 +1055,7 @@ class GoogleStyleFormatterKtTest { | } |} |""" - .trimMargin() + .trimMargin() val expected = """ |fun main() { @@ -1071,7 +1071,7 @@ class GoogleStyleFormatterKtTest { | } |} |""" - .trimMargin() + .trimMargin() assertThatFormatting(code).withOptions(Formatter.GOOGLE_FORMAT).isEqualTo(expected) } @@ -1079,7 +1079,7 @@ class GoogleStyleFormatterKtTest { fun `tailing commas are not added to empty lists`() { // Use trailing comments to force the breaks assertFormatted( - """ + """ |fun main() { | fun foo( | // @@ -1104,15 +1104,15 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin(), - formattingOptions = Formatter.GOOGLE_FORMAT, - deduceMaxWidth = false) + .trimMargin(), + formattingOptions = Formatter.GOOGLE_FORMAT, + deduceMaxWidth = false) } @Test fun `tailing commas are not added to single-element lists`() { assertFormatted( - """ + """ |fun main() { | fun foo( | a: Int // @@ -1137,9 +1137,9 @@ class GoogleStyleFormatterKtTest { | fun foo() {} |} |""" - .trimMargin(), - formattingOptions = Formatter.GOOGLE_FORMAT, - deduceMaxWidth = false) + .trimMargin(), + formattingOptions = Formatter.GOOGLE_FORMAT, + deduceMaxWidth = false) } @Test