From 30dfa12c990dbf461fe27a86533baaf86d4af8ff Mon Sep 17 00:00:00 2001 From: nickreid Date: Wed, 19 Oct 2022 15:38:46 -0700 Subject: [PATCH] Preserve indentation of trailing comments in comma-separated lists (#357) Summary: Pull Request resolved: https://github.com/facebook/ktfmt/pull/357 Reviewed By: strulovich Differential Revision: D40469643 Pulled By: cgrushko fbshipit-source-id: 9583504e794ce6646bf39bff4e5dbdcc6bc94120 --- .../ktfmt/format/KotlinInputAstVisitor.kt | 12 +++++- .../facebook/ktfmt/format/FormatterTest.kt | 39 +++++++++++++++++++ .../format/GoogleStyleFormatterKtTest.kt | 15 +++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt index 8534f584..898b70e5 100644 --- a/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt +++ b/core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt @@ -1093,7 +1093,17 @@ class KotlinInputAstVisitor( } if (postfix != null) { - builder.token(postfix) + if (breakAfterLastElement) { + // Indent trailing comments to the same depth as list items. We really have to fight + // googlejavaformat here for some reason. + builder.blankLineWanted(OpsBuilder.BlankLineWanted.NO) + builder.block(expressionBreakNegativeIndent) { + builder.breakOp(breakType, "", ZERO) + builder.token(postfix, expressionBreakIndent) + } + } else { + builder.token(postfix) + } } return nameTag 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 ce170a1b..040374d9 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt @@ -6423,6 +6423,45 @@ class FormatterTest { .trimMargin(), deduceMaxWidth = true) + @Test + fun `array-literal in annotation`() = + assertFormatted( + """ + |-------------------------------- + |@Anno( + | array = + | [ + | someItem, + | andAnother, + | noTrailingComma]) + |class Host + | + |@Anno( + | array = + | [ + | someItem, + | andAnother, + | withTrailingComma, + | ]) + |class Host + | + |@Anno( + | array = + | [ + | // Comment + | someItem, + | // Comment + | andAnother, + | // Comment + | withTrailingComment + | // Comment + | // Comment + | ]) + |class Host + |""" + .trimMargin(), + deduceMaxWidth = true) + companion object { /** Triple quotes, useful to use within triple-quoted strings. */ private const val TQ = "\"\"\"" 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 c4f5c0a4..b09fb486 100644 --- a/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt +++ b/core/src/test/java/com/facebook/ktfmt/format/GoogleStyleFormatterKtTest.kt @@ -1290,6 +1290,21 @@ class GoogleStyleFormatterKtTest { | ] |) |class Host + | + |@Anno( + | array = + | [ + | // Comment + | someItem, + | // Comment + | andAnother, + | // Comment + | withTrailingComment + | // Comment + | // Comment + | ] + |) + |class Host |""" .trimMargin(), formattingOptions = Formatter.GOOGLE_FORMAT,