ktlint icon indicating copy to clipboard operation
ktlint copied to clipboard

indentation for expressions on newline

Open meesec opened this issue 3 years ago • 3 comments

Expected Behavior

This should not trigger any rules (and is correct in 0.39.0):

operator fun x(resources: Resources, entity: X):
    ListItem<SomeClass> =
        foo(
            resources = resources,
            ...
        )

Observed Behavior (in 0.41.0)

operator fun x(resources: Resources, entity: X):
    ListItem<SomeClass> =
    foo(
        resources = resources,
        ...
    )

Steps to Reproduce

the same can be observed for

foo()
List<Sth> = map {
    when (it.a) {
        a.b -> c
        else -> it
    }
}
bar()
foo()
positiveButtonText =
    R.string.my_long_res_name_that_forces_a_linebreak_string
bar()

Your Environment

  • Version of ktlint used: upgrade to 0.41.0 (used to work in 0.39.0)
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): plain ktlint on cmd
  • Version of Gradle used (if applicable):
  • Operating System and version: any
  • Link to your project (if it's a public repository): sry

meesec avatar May 25 '21 14:05 meesec

I confirm this incorrect behaviour in 0.41.0

vojkny avatar Jun 28 '21 11:06 vojkny

This issue will be solved by https://github.com/pinterest/ktlint/issues/1341. Note that code sample given in this issue do not adhere to the coding guideline and should be rewritten as documented in previously mentioned issue.

paul-dingemans avatar Jan 26 '22 20:01 paul-dingemans

operator fun x(resources: Resources, entity: X):
    ListItem<SomeClass> =
        foo(
            resources = resources,
            ...
        )

The experimental:function-signature rules which was released in 0.46.x is able to rewrite the examples above in accordance to the Kotlin Coding conventions and Android Kotlin Style guide. The return type ListItem<SomeClass> and the closing parenthesis on the function should be on the same line. Once that is fixed, it is clear that the identation should be 4 instead of 8 characters.

The second example is ambiguous. If I interpret it as:

fun foobar() {
    foo()
    val something: List<Sth> = map {
        when (it.a) {
            a.b -> c
            else -> it
        }
    }
    bar()
}

then its formatting is correct.

Same for third example:

fun foobar() {
    foo()
    positiveButtonText =
        R.string.my_long_res_name_that_forces_a_linebreak_string
    bar()
}

Please let me know if I misinterpreted the last two examples and provide some real working code snippets which demonstrate the problems.

paul-dingemans avatar Jul 06 '22 18:07 paul-dingemans

Original problem is also mentioned in #1217. As that issue contains more context and discussion, this issue is closed in favour of #1217. For second and third problem no feedback has been received since July 6th, 2022.

paul-dingemans avatar Nov 02 '22 15:11 paul-dingemans

And now actually closing the issue.

paul-dingemans avatar Nov 23 '22 17:11 paul-dingemans