diff --git a/CHANGELOG.md b/CHANGELOG.md index 575d2c6cd6..cd9838a800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,7 @@ Code style `android` has been renamed to `android_studio`. Code formatted with t * Do not add the first line of a multiline body expression on the same line as the function signature in case function body expression wrapping property is set to `multiline`. `function-signature`. * Disable the `standard:filename` rule whenever Ktlint CLI is run with option `--stdin` ([#1742](https://github.com/pinterest/ktlint/issues/1742)) * The parameters of a function literal containing a multiline parameter list are aligned with first parameter whenever the first parameter is on the same line as the start of that function literal (not allowed in `ktlint_official` code style) `indent` ([#1756](https://github.com/pinterest/ktlint/issues/1756)). +* Fix continuation indent for a dot qualified array access expression in `ktlint_official` code style only `indent` ([#1740](https://github.com/pinterest/ktlint/issues/1540)). ### Changed * Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)). diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt index d7bb128226..ce8f8a7885 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt @@ -14,6 +14,7 @@ import com.pinterest.ktlint.core.api.editorconfig.INDENT_STYLE_PROPERTY import com.pinterest.ktlint.core.ast.ElementType.ANNOTATED_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY +import com.pinterest.ktlint.core.ast.ElementType.ARRAY_ACCESS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.ARROW import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.BINARY_WITH_TYPE @@ -260,7 +261,21 @@ public class IndentationRule : node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == USER_TYPE -> { - if (node.treeParent?.elementType != node.elementType) { + if (codeStyle == ktlint_official && + node.elementType == DOT_QUALIFIED_EXPRESSION && + node.treeParent?.elementType == ARRAY_ACCESS_EXPRESSION && + node.treeParent?.treeParent?.elementType == CALL_EXPRESSION + ) { + // Issue 1540: Deviate and fix from incorrect formatting in IntelliJ IDEA formatting and produce following: + // val fooBar2 = foo + // .bar[0] { + // "foobar" + // } + startIndentContext( + fromAstNode = node.treeParent, + toAstNode = node.treeParent.treeParent.lastChildLeafOrSelf(), + ) + } else if (node.treeParent?.elementType != node.elementType) { startIndentContext(node) } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt index 47fcf2913a..cb27b339ec 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt @@ -23,7 +23,7 @@ internal class IndentationRuleTest { internal fun setUp() { // The system property below can be set to "on" to enable extensive trace logging. Do not commit/push such // change to any branch as it pollutes the build output too much! - System.setProperty("KTLINT_UNIT_TEST_TRACE", "off") + System.setProperty("KTLINT_UNIT_TEST_TRACE", "on") // DO _NOT_ COMMIT } private val indentationRuleAssertThat = assertThatRule { IndentationRule() } @@ -4795,6 +4795,24 @@ internal class IndentationRuleTest { LintViolation(4, 1, "Unexpected indentation (19) (should be 12)"), ).isFormattedAs(formattedCode) } + + @Test + fun `Issue 1540 - Given a DOT_QUALIFIED_EXPRESSION wrapped inside an ARRAY_ACCESS_EXPRESSION`() { + val code = + """ + val fooBar1 = foo + .bar { + "foobar" + } + val fooBar2 = foo + .bar[0] { + "foobar" + } + """.trimIndent() + indentationRuleAssertThat(code) + .withEditorConfigOverride(CODE_STYLE_PROPERTY to ktlint_official) + .hasNoLintViolations() + } } private companion object {