Skip to content

Commit

Permalink
Fix indent of when entry with a dot qualified expression instead of s…
Browse files Browse the repository at this point in the history
…imple value when trailing comma is required

Closes #1519
  • Loading branch information
paul-dingemans committed Aug 7, 2022
1 parent 6fdb80e commit aa1a664
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -148,6 +148,7 @@ Although, Ktlint 0.47.0 falls back on property `disabled_rules` whenever `ktlint
* Prevent class cast exception on ".editorconfig" property `ktlint_code_style` ([#1559](https://github.com/pinterest/ktlint/issues/1559))
* Handle trailing comma in enums `trailing-comma` ([#1542](https://github.com/pinterest/ktlint/pull/1542))
* Split rule `trailing-comma` into `trailing-comma-on-call-site` and `trailing-comma-on-declaration-site` ([#1555](https://github.com/pinterest/ktlint/pull/1555))
* Fix indent of when entry with a dot qualified expression instead of simple value when trailing comma is required ([#1519](https://github.com/pinterest/ktlint/pull/1519))

### Changed

Expand Down
Expand Up @@ -6,6 +6,7 @@ import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.containsLineBreakInRange
import com.pinterest.ktlint.core.ast.lineIndent
import com.pinterest.ktlint.core.ast.lineNumber
import com.pinterest.ktlint.core.ast.prevCodeLeaf
import com.pinterest.ktlint.core.ast.prevLeaf
Expand All @@ -28,6 +29,7 @@ import org.jetbrains.kotlin.psi.KtWhenExpression
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.nextLeaf
import org.jetbrains.kotlin.psi.psiUtil.parents
import org.jetbrains.kotlin.psi.psiUtil.prevLeaf
import org.jetbrains.kotlin.utils.addToStdlib.cast

Expand Down Expand Up @@ -265,7 +267,7 @@ public class TrailingCommaOnDeclarationSiteRule :
}
if (autoCorrect) {
if (addNewLineBeforeArrowInWhenEntry) {
val parentIndent = (prevNode.psi.parent.prevLeaf() as? PsiWhiteSpace)?.text ?: "\n"
val parentIndent = "\n" + prevNode.getWhenEntryIndent()
val leafBeforeArrow = (psi as KtWhenEntry).arrow?.prevLeaf()
if (leafBeforeArrow != null && leafBeforeArrow is PsiWhiteSpace) {
val newLine = KtPsiFactory(prevNode.psi).createWhiteSpace(parentIndent)
Expand Down Expand Up @@ -307,6 +309,12 @@ public class TrailingCommaOnDeclarationSiteRule :
}
}

private fun ASTNode.getWhenEntryIndent() =
// The when entry can be a simple value but might also be a complex expression.
parents()
.last { it.elementType == ElementType.WHEN_ENTRY }
.lineIndent()

private fun isMultiline(element: PsiElement): Boolean = when {
element.parent is KtFunctionLiteral -> isMultiline(element.parent)
element is KtFunctionLiteral -> containsLineBreakInRange(element.valueParameterList!!, element.arrow!!)
Expand Down
Expand Up @@ -273,6 +273,40 @@ class TrailingCommaOnDeclarationSiteRuleTest {
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 1519 - Given a when entry which is not a simple value but a dot qualified expression and that the trailing comma is required on declaration site then add it when missing`() {
val code =
"""
fun foo(bar: Any): String = when(bar) {
bar.foobar1(), bar.foobar2() -> "a"
bar.foobar3(), bar.foobar4() // The comma should be inserted before the comment
-> "a"
bar.foobar5(),
bar.foobar6() /* The comma should be inserted before the comment */
-> "a"
else -> "b"
}
""".trimIndent()
val formattedCode =
"""
fun foo(bar: Any): String = when(bar) {
bar.foobar1(), bar.foobar2() -> "a"
bar.foobar3(), bar.foobar4(), // The comma should be inserted before the comment
-> "a"
bar.foobar5(),
bar.foobar6(), /* The comma should be inserted before the comment */
-> "a"
else -> "b"
}
""".trimIndent()
ruleAssertThat(code)
.withEditorConfigOverride(allowTrailingCommaProperty to true)
.hasLintViolations(
LintViolation(3, 33, "Missing trailing comma before \"->\""),
LintViolation(6, 18, "Missing trailing comma before \"->\"")
).isFormattedAs(formattedCode)
}

@Test
fun `Given that the trailing comma is not allowed on declaration site then remove it from the destructuring declaration when present`() {
val code =
Expand Down

0 comments on commit aa1a664

Please sign in to comment.