Skip to content

Commit

Permalink
Fix continuation indent for a dot qualified array access expression i…
Browse files Browse the repository at this point in the history
…n ktlint_official code style only.

Closes pinterest#1540
  • Loading branch information
paul-dingemans committed Jan 5, 2023
1 parent 12f9522 commit b304451
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b304451

Please # to comment.