Skip to content

Commit

Permalink
Move Elvis operator to end of broken lines
Browse files Browse the repository at this point in the history
This is consistent with other binary operators, and prevents a strange second indent in Elvis chains.
  • Loading branch information
nreid260 committed Aug 24, 2023
1 parent 6384761 commit d72a0aa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1199,20 +1199,23 @@ 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)
}
else -> {
builder.space()
if (isFirst) {
builder.open(expressionBreakIndent)
}
builder.token(leftExpression.operationReference.text)
builder.breakOp(Doc.FillMode.UNIFIED, " ", ZERO)
}
}
visit(leftExpression.right)
}
Expand Down
54 changes: 50 additions & 4 deletions core/src/test/java/com/facebook/ktfmt/format/FormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2577,8 +2577,8 @@ class FormatterTest {
|class f {
| private val somePropertyWithBackingOne
| get() =
| _somePropertyWithBackingOne
| ?: Classname.getStuff<SomePropertyRelatedClassProvider>(requireContext())[
| _somePropertyWithBackingOne ?:
| Classname.getStuff<SomePropertyRelatedClassProvider>(requireContext())[
| somePropertiesProvider, somePropertyCallbacks]
| .also { _somePropertyWithBackingOne = it }
|}
Expand Down Expand Up @@ -4250,8 +4250,54 @@ class FormatterTest {
| someObject
| .someMethodReturningCollection()
| .map { it.someProperty }
| .find { it.contains(someSearchValue) }
| ?: someDefaultValue
| .find { it.contains(someSearchValue) } ?:
| someDefaultValue
|}
|"""
.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(),
Expand Down

0 comments on commit d72a0aa

Please sign in to comment.