Skip to content

Commit

Permalink
Fix double indentation in Elvis chains (#416)
Browse files Browse the repository at this point in the history
Summary:
This is consistent with other binary operators, and prevents a strange second indent in Elvis chains.

Pull Request resolved: #416

Reviewed By: strulovich

Differential Revision: D49260663

Pulled By: hick209

fbshipit-source-id: 21feb4fb57700bb93ba01aec43a2e7da7d2afa57
  • Loading branch information
nreid260 authored and facebook-github-bot committed Sep 14, 2023
1 parent a06e353 commit ccc2b74
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1207,20 +1207,31 @@ class KotlinInputAstVisitor(
val leftMostExpression = parts.first()
visit(leftMostExpression.left)
for (leftExpression in parts) {
when (leftExpression.operationToken) {
KtTokens.RANGE -> {}
KtTokens.ELVIS -> builder.breakOp(Doc.FillMode.INDEPENDENT, " ", expressionBreakIndent)
else -> builder.space()
}
builder.token(leftExpression.operationReference.text)
val isFirst = leftExpression === leftMostExpression
if (isFirst) {
builder.open(expressionBreakIndent)
}

when (leftExpression.operationToken) {
KtTokens.RANGE -> {}
KtTokens.ELVIS -> builder.space()
else -> builder.breakOp(Doc.FillMode.UNIFIED, " ", ZERO)
KtTokens.RANGE -> {
if (isFirst) {
builder.open(expressionBreakIndent)
}
builder.token(leftExpression.operationReference.text)
}
KtTokens.ELVIS -> {
if (isFirst) {
builder.open(expressionBreakIndent)
}
builder.breakOp(Doc.FillMode.UNIFIED, " ", ZERO)
builder.token(leftExpression.operationReference.text)
builder.space()
}
else -> {
builder.space()
if (isFirst) {
builder.open(expressionBreakIndent)
}
builder.token(leftExpression.operationReference.text)
builder.breakOp(Doc.FillMode.UNIFIED, " ", ZERO)
}
}
visit(leftExpression.right)
}
Expand Down
46 changes: 46 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 @@ -4379,6 +4379,52 @@ class FormatterTest {
.trimMargin(),
deduceMaxWidth = true)

@Test
fun `chain of Elvis operator`() =
assertFormatted(
"""
|---------------------------
|fun f() {
| return option1()
| ?: option2()
| ?: option3()
| ?: option4()
| ?: option5()
|}
|"""
.trimMargin(),
deduceMaxWidth = true)

@Test
fun `Elvis operator mixed with plus operator breaking on plus`() =
assertFormatted(
"""
|------------------------
|fun f() {
| return option1()
| ?: option2() +
| option3()
| ?: option4() +
| option5()
|}
|"""
.trimMargin(),
deduceMaxWidth = true)

@Test
fun `Elvis operator mixed with plus operator breaking on elvis`() =
assertFormatted(
"""
|---------------------------------
|fun f() {
| return option1()
| ?: option2() + option3()
| ?: option4() + option5()
|}
|"""
.trimMargin(),
deduceMaxWidth = true)

@Test
fun `handle comments in the middle of calling chain`() =
assertFormatted(
Expand Down

0 comments on commit ccc2b74

Please sign in to comment.