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

Commit

Permalink
Treat an is expression as a block so that the formatter doesn't prefe…
Browse files Browse the repository at this point in the history
…r to break within it.
  • Loading branch information
hovinen committed Feb 1, 2021
1 parent 89f8581 commit 830910a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class TokenPreprocessor {
resultStack.peek()
.tokens
.add(ForcedBreakToken(count = token.content.count { it == '\n' }))
} else if (!(lastToken is ForcedBreakToken || lastToken is
ClosingForcedBreakToken)
} else if (!(lastToken is ForcedBreakToken ||
lastToken is ClosingForcedBreakToken)
) {
resultStack.peek().tokens.add(token)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,10 @@ internal class NodeScannerProvider(
KtNodeTypes.WHEN_ENTRY, KtNodeTypes.ANNOTATION_ENTRY, KtNodeTypes.PREFIX_EXPRESSION,
KtNodeTypes.VALUE_PARAMETER, KtNodeTypes.SUPER_TYPE_ENTRY,
KtNodeTypes.SUPER_TYPE_CALL_ENTRY, KtNodeTypes.USER_TYPE,
KtNodeTypes.BINARY_WITH_TYPE, KtNodeTypes.DELEGATED_SUPER_TYPE_ENTRY ->
simpleBlockScanner.value
KtNodeTypes.BINARY_WITH_TYPE, KtNodeTypes.DELEGATED_SUPER_TYPE_ENTRY,
KtNodeTypes.IS_EXPRESSION -> simpleBlockScanner.value
KtNodeTypes.SHORT_STRING_TEMPLATE_ENTRY, KtNodeTypes.LONG_STRING_TEMPLATE_ENTRY ->
stringTemplateEntryScanner.value
KtNodeTypes.OBJECT_LITERAL -> simpleScanner.value
else -> simpleScanner.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,19 @@ class KotlinFormatterTest {
)
}

@Test
fun `prefers not to break before is operator`() {
val result =
KotlinFormatter(maxLineLength = 26).format("val something = variable is MyClass")

assertThat(result).isEqualTo(
"""
val something =
variable is MyClass
""".trimIndent()
)
}

@Test
fun `format breaks at logical operator in a while statement`() {
val result =
Expand Down

0 comments on commit 830910a

Please sign in to comment.