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

Wrong indentation for lambdas with many parameters #2816

Closed
ln-12 opened this issue Sep 26, 2024 · 5 comments · Fixed by #2823
Closed

Wrong indentation for lambdas with many parameters #2816

ln-12 opened this issue Sep 26, 2024 · 5 comments · Fixed by #2823
Milestone

Comments

@ln-12
Copy link

ln-12 commented Sep 26, 2024

The parameter list of a multiline lambda does not have the correct indentation. The corresponding unit test seems to do it wrong on purpose: https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt#L2607

Expected Behavior

Consider the last code block from https://kotlinlang.org/docs/coding-conventions.html#lambdas:

foo {
   context: Context,
   environment: Env
   ->
   context.configureEnv(environment)
}

The lambda parameters have the same indentation as the arrow and the following statement.

Observed Behavior

When turning on the Wrapping rule with autoCorrect enabled via detekt, the code is formatted like this:

foo {
        context: Context,
        environment: Env,
    ->
    context.configureEnv(environment)
}

Steps to Reproduce

Enable the Wrapping rule and let the check run for the following code:

class Env
class Context {
    fun configureEnv(env: Env) {}
}
fun foo(action: (context: Context, environment: Env) -> Unit) {}

fun bar() {
    foo {
        context: Context,
        environment: Env,
        ->
        context.configureEnv(environment)
    }
}

Your Environment

  • Version of ktlint used: I am using ktlint via detekt 1.23.7
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): ./gradlew detekt --auto-correct
@paul-dingemans
Copy link
Collaborator

You're right that this contradicts the Kotlin style guide.

Reasoning is that when the parameters and arrows are on the same line, the distinction between parameters and body becomes blurry.

fun bar() {
    foo {
        context: Context, environment: Env, ->
        context.configureEnv(environment)
    }
}

@ln-12
Copy link
Author

ln-12 commented Sep 28, 2024

I see, thanks for the fast reply!

Do you think that you could add a flag to toggle that behavior? I strongly prefer the official way and always add a blank line after the parameter list.

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Sep 29, 2024

My default reply to toggles is: https://pinterest.github.io/ktlint/latest/faq/#can-a-new-toggle-be-added-to-optionally-enabledisable-format-code-in-a-particular-way

This case however is non-standard as the default behavior is not compliant with code style. But, if implemented, the default for ktlint_official code style is not likely to be changed. For code style intellij-idea it seems logical to change the default behavior to be compliant so that it does not contradict default IDEA formatting.

@ln-12
Copy link
Author

ln-12 commented Oct 1, 2024

Ok, I see. That sounds like a reasonable way to go as far as I understand it. With that I could still switch it like with other flags to both options, independent of the selected code style, right? Then I would definitely support that idea.

@paul-dingemans
Copy link
Collaborator

paul-dingemans commented Oct 4, 2024

This case however is non-standard as the default behavior is not compliant with code style. But, if implemented, the default for ktlint_official code style is not likely to be changed. For code style intellij-idea it seems logical to change the default behavior to be compliant so that it does not contradict default IDEA formatting.

It should just be the other way around. The current formatting complies with the default formatting of Intellij IDEA. For code styles android_studio and intellij_idea it has always been an important requirement that the code formatted by ktlint should not conflict with the default formatting of the IDEA (see #1247). In code style ktlint_official this requirement has been dropped. So this code style should actually enforce the formatting below:

fun bar() {
    foo {
        context: Context,
        environment: Env,
        ->
        context.configureEnv(environment)
    }
}

I will not introduce a new configurable toggle for this, but enforce it based on the code style.

paul-dingemans added a commit that referenced this issue Oct 4, 2024
…al for code style `ktlint_official`

For other code styles the indentation does not comply with Kotlin Coding conventions as other the formatting would conflict with default IDEA formatting.

Closes #2816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants