Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double indentation in Elvis chains #416

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading