ktfmt: Wrong indentation in a lambda

Introduced in 0.20

class SomeClass {
    fun aLongMethodWhereLambdaIsNotInTheSameLine(foo: String, bar: String, baz: String) =
        someLambda {
        someClass.someMethod(foo = foo, bar = bar, baz = baz)
    }
}

should be

class SomeClass {
    fun aLongMethodWhereLambdaIsNotInTheSameLine(foo: String, bar: String, baz: String) =
        someLambda {
            someClass.someMethod(foo = foo, bar = bar, baz = baz)
        }
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (19 by maintainers)

Most upvoted comments

We (I) actually prefer reverting this - it was a feature request by Google, actually 😃 (it complicates the code and I agree it produces inconsistencies) @nreid260 - let me know.

On the Google side, we’re considering that it might be better to revert. We’d prefer a formatting that is consistently adequate vs one that is usually good but fails a few percent of the time. We’re also not sure whether presenting scoping function names on the same line is more readable, or just more terse.

I did some digging into how to fix this bug, and while it’s possible, it’s pretty hacky. The underlying problem is that google_java_format strongly adheres to the rectangle rule, which https://github.com/facebookincubator/ktfmt/commit/6c7dd8dd6aaf1daa7d6633efb78b924c2e534fe1 violates. In order to express https://github.com/facebookincubator/ktfmt/commit/6c7dd8dd6aaf1daa7d6633efb78b924c2e534fe1 formatting correctly, we need to futz around with conditional indents across many syntactic levels.

This issue is common to all “block-like expressions”, which Kotlin has several of (e.g. if, when). If we could add support for such constructs directly into google_java_format, it might make sense, but it currently isn’t obvious how to do that. Until then, consistency across all block-like expressions is also desirable.

@JavierSegoviaCordoba we’ll try 😃 in the meanwhile, don’t wait for us - clone the repo and build at head and see if it’s of use for you at all.