Skip to content

Commit

Permalink
Reformat Kotlin files
Browse files Browse the repository at this point in the history
Reviewed By: cgrushko

Differential Revision: D51248035

fbshipit-source-id: b612b96612929732960977a4cda55209155e5438
  • Loading branch information
generatedunixname89002005325672 authored and facebook-github-bot committed Nov 13, 2023
1 parent fa78077 commit c0511df
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
20 changes: 11 additions & 9 deletions core/src/main/java/com/facebook/ktfmt/format/Formatter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,8 @@ class GoogleStyleFormatterKtTest {

@Test
fun `tailing commas are removed when redundant`() {
val code =
"""
val code =
"""
|fun main() {
| fun <A, B,> foo() {}
|
Expand All @@ -930,9 +930,9 @@ class GoogleStyleFormatterKtTest {
| fun foo() {}
|}
|"""
.trimMargin()
.trimMargin()
val expected =
"""
"""
|fun main() {
| fun <A, B> foo() {}
|
Expand All @@ -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,
Expand Down Expand Up @@ -992,7 +992,7 @@ class GoogleStyleFormatterKtTest {
| fun foo() {}
|}
|"""
.trimMargin()
.trimMargin()
val expected =
"""
|fun main() {
Expand Down Expand Up @@ -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, //
Expand All @@ -1055,7 +1055,7 @@ class GoogleStyleFormatterKtTest {
| }
|}
|"""
.trimMargin()
.trimMargin()
val expected =
"""
|fun main() {
Expand All @@ -1071,15 +1071,15 @@ class GoogleStyleFormatterKtTest {
| }
|}
|"""
.trimMargin()
.trimMargin()
assertThatFormatting(code).withOptions(Formatter.GOOGLE_FORMAT).isEqualTo(expected)
}

@Test
fun `tailing commas are not added to empty lists`() {
// Use trailing comments to force the breaks
assertFormatted(
"""
"""
|fun main() {
| fun foo(
| //
Expand All @@ -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 //
Expand All @@ -1137,9 +1137,9 @@ class GoogleStyleFormatterKtTest {
| fun foo() {}
|}
|"""
.trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = false)
.trimMargin(),
formattingOptions = Formatter.GOOGLE_FORMAT,
deduceMaxWidth = false)
}

@Test
Expand Down

0 comments on commit c0511df

Please sign in to comment.