Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix indent of IS_EXPRESSION, PREFIX_EXPRESSION and POSTFIX_EXPRESSION #2125

Merged
merged 2 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* Log message `Format was not able to resolve all violations which (theoretically) can be autocorrected in file ... in 3 consecutive runs of format` is now only displayed in case a new ktlint rule is actually needed. [#2129](https://github.com/pinterest/ktlint/issues/2129)
* Fix wrapping of function signature in case the opening brace of the function body block exceeds the maximum line length. Fix upsert of whitespace into composite nodes. `function-signature` [#2130](https://github.com/pinterest/ktlint/issues/2130)
* Fix spacing around colon in annotations `spacing-around-colon` ([#2093](https://github.com/pinterest/ktlint/issues/2093))
* Fix indent of IS_EXPRESSION, PREFIX_EXPRESSION and POSTFIX_EXPRESSION in case it contains a linebreak `indent` [#2094](https://github.com/pinterest/ktlint/issues/2094)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IDENTIFIER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IF
import com.pinterest.ktlint.rule.engine.core.api.ElementType.IS_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC_END
import com.pinterest.ktlint.rule.engine.core.api.ElementType.KDOC_LEADING_ASTERISK
Expand All @@ -50,6 +51,8 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_DECLARATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OPEN_QUOTE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PARENTHESIZED
import com.pinterest.ktlint.rule.engine.core.api.ElementType.POSTFIX_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PREFIX_EXPRESSION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY_ACCESSOR
Expand Down Expand Up @@ -263,6 +266,11 @@ public class IndentationRule :
node.elementType == USER_TYPE ->
startIndentContext(node)

node.elementType == IS_EXPRESSION ||
node.elementType == PREFIX_EXPRESSION ||
node.elementType == POSTFIX_EXPRESSION ->
startIndentContext(node)

node.elementType == DELEGATED_SUPER_TYPE_ENTRY ||
node.elementType == ANNOTATED_EXPRESSION ||
node.elementType == TYPE_REFERENCE ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5340,6 +5340,63 @@ internal class IndentationRuleTest {
.hasNoLintViolations()
}

@Test
fun `Issue 2094 - Given a malformed IS_EXPRESSION`() {
val code =
"""
fun foo(any: Any) =
any is
Foo
""".trimIndent()
val formattedCode =
"""
fun foo(any: Any) =
any is
Foo
""".trimIndent()
indentationRuleAssertThat(code)
.hasLintViolation(3, 1, "Unexpected indentation (4) (should be 8)")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2094 - Given a malformed PREFIX_EXPRESSION`() {
val code =
"""
fun foo(value: Int) =
++
value
""".trimIndent()
val formattedCode =
"""
fun foo(value: Int) =
++
value
""".trimIndent()
indentationRuleAssertThat(code)
.hasLintViolation(3, 1, "Unexpected indentation (4) (should be 8)")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2094 - Given a malformed POSTFIX_EXPRESSION`() {
val code =
"""
fun foo(value: Int) =
--
value
""".trimIndent()
val formattedCode =
"""
fun foo(value: Int) =
--
value
""".trimIndent()
indentationRuleAssertThat(code)
.hasLintViolation(3, 1, "Unexpected indentation (4) (should be 8)")
.isFormattedAs(formattedCode)
}

private companion object {
val INDENT_STYLE_TAB =
INDENT_STYLE_PROPERTY to PropertyType.IndentStyleValue.tab
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,11 @@ class MultilineExpressionWrappingTest {
fun foo(any: Any) = any is
Foo
""".trimIndent()
// TODO: https://github.com/pinterest/ktlint/issues/2094 Fix formatting by indent rule
val formattedCode =
"""
fun foo(any: Any) =
any is
Foo
Foo
""".trimIndent()
multilineExpressionWrappingAssertThat(code)
.addAdditionalRuleProvider { IndentationRule() }
Expand Down Expand Up @@ -615,12 +614,11 @@ class MultilineExpressionWrappingTest {
fun foo(any: Int) = ++
42
""".trimIndent()
// TODO: https://github.com/pinterest/ktlint/issues/2094 Fix formatting by indent rule
val formattedCode =
"""
fun foo(any: Int) =
++
42
42
""".trimIndent()
multilineExpressionWrappingAssertThat(code)
.addAdditionalRuleProvider { IndentationRule() }
Expand Down