Skip to content

Commit

Permalink
Preserve indentation of trailing comments in comma-separated lists (#357
Browse files Browse the repository at this point in the history
)

Summary: Pull Request resolved: #357

Reviewed By: strulovich

Differential Revision: D40469643

Pulled By: cgrushko

fbshipit-source-id: 9583504e794ce6646bf39bff4e5dbdcc6bc94120
  • Loading branch information
nreid260 authored and facebook-github-bot committed Oct 19, 2022
1 parent e29190a commit 30dfa12
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "\"\"\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 30dfa12

Please sign in to comment.