Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Treat array access expressions as blocks.
Browse files Browse the repository at this point in the history
Otherwise, the formatter may prefer to insert a line break in the middle of such an expression, which is not right.

Fixes #19
  • Loading branch information
hovinen committed Mar 17, 2021
1 parent 00e6b83 commit 600dd7c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ internal class NodeScannerProvider(
KtNodeTypes.VALUE_PARAMETER, KtNodeTypes.SUPER_TYPE_ENTRY,
KtNodeTypes.SUPER_TYPE_CALL_ENTRY, KtNodeTypes.USER_TYPE,
KtNodeTypes.BINARY_WITH_TYPE, KtNodeTypes.DELEGATED_SUPER_TYPE_ENTRY,
KtNodeTypes.IS_EXPRESSION -> simpleBlockScanner.value
KtNodeTypes.IS_EXPRESSION, KtNodeTypes.ARRAY_ACCESS_EXPRESSION ->
simpleBlockScanner.value
KtNodeTypes.SHORT_STRING_TEMPLATE_ENTRY, KtNodeTypes.LONG_STRING_TEMPLATE_ENTRY ->
stringTemplateEntryScanner.value
else -> simpleScanner.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,20 @@ class KotlinFormatterTest {
)
}

@Test
fun `prefers not to break within array access expression`() {
val result =
KotlinFormatter(maxLineLength = 35)
.format("fun aFunction(): String = m[one to two]")

assertThat(result).isEqualTo(
"""
fun aFunction(): String =
m[one to two]
""".trimIndent()
)
}

@Test
fun `prefers to break before beginning of function literal`() {
val result =
Expand Down

0 comments on commit 600dd7c

Please sign in to comment.