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

Enforce multiline lambda parameters to be on the first line #2121

Closed
t3hk0d3 opened this issue Jul 10, 2023 · 4 comments · Fixed by #2137
Closed

Enforce multiline lambda parameters to be on the first line #2121

t3hk0d3 opened this issue Jul 10, 2023 · 4 comments · Fixed by #2137

Comments

@t3hk0d3
Copy link

t3hk0d3 commented Jul 10, 2023

Expected Rule behavior

Currently it is possible to have lambdas to be formatted like following:

foo.bar { 
    param -> 
        param.doSomething()
         // ...
}

However this is incorrect behaviour according to Kotlin Code Conventions:

https://kotlinlang.org/docs/coding-conventions.html#lambdas

When declaring parameter names in a multiline lambda, put the names on the first line, followed by the arrow and the newline:

appendCommaSeparated(properties) { prop ->
    val propertyValue = prop.get(obj)  // ...
}

Additional information

@paul-dingemans
Copy link
Collaborator

Currently it is possible to have lambdas to be formatted like following:

foo.bar { 
    param -> 
        param.doSomething()
         // ...
}

Code above is not accepted by ktlint. With code style intellij_idea or android_studio, this code is formatted as:

fun foo2() {
    foo1.bar {
            param ->
        param.doSomething()
    }
}

which indeed deviates from the kotlin coding conventions.

The idea behind this deviation is that code sample like below:

fun foo3() {
    foo1.bar {
        param1,
        param2 ->
            param1.doSomething()
            param2.doSomething()
    }
}

are nicely formatted as:

fun foo3() {
    foo1.bar {
            param1,
            param2 ->
        param1.doSomething()
        param2.doSomething()
    }
}

which is more consist with indentation of parameters in a function call:

fun foo(
    param1: Any,
    param2: Any
)  {
    // do something
}

Note that with code style ktlint_official there is additional formatting to change code below:

fun foo4() {
    foo1.bar { param1,
        param2 ->
            param1.doSomething()
            param2.doSomething()
    }
}

to

fun foo4() {
    foo1.bar {
            param1,
            param2,
        ->
        param1.doSomething()
        param2.doSomething()
    }
}

@paul-dingemans
Copy link
Collaborator

I have misread the original post and focused too much on the indentation only.

The original post identifies that code:

foo.bar { 
    param -> 
        param.doSomething()
         // ...
}

should be formatted as:

foo.bar {  param -> 
    param.doSomething()
    // ...
}

to comply with:

When declaring parameter names in a multiline lambda, put the names on the first line, followed by the arrow and the newline:

For this a new rule is needed. This rule should also take care of:

If the parameter list is too long to fit on a line, put the arrow on a separate line:

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

@paul-dingemans
Copy link
Collaborator

The Kotlin Coding conventions are not entirely comsistent regard indenting of a multiline parameter list.

https://kotlinlang.org/docs/coding-conventions.html#lambdas

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

https://kotlinlang.org/docs/coding-conventions.html#parameters-in-lambdas

fun main() {
    val x = {
            x: Comparable<Number>,
            y: Iterable<Number>, // trailing comma
        ->
        println("1")
    }
    println(x)
}

paul-dingemans added a commit that referenced this issue Jul 20, 2023
This rule enforces the parameter list of a function literal to be formatted consistently

Closes #2121
paul-dingemans added a commit that referenced this issue Jul 22, 2023
This rule enforces the parameter list of a function literal to be formatted consistently

Closes #2121
@t3hk0d3
Copy link
Author

t3hk0d3 commented Jul 23, 2023

Thanks for a rapid fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants