Emacs-Kotlin-Mode-Maintainers / kotlin-mode

Kotlin major mode for Emacs
GNU General Public License v3.0
163 stars 43 forks source link

Multiline lambda parameters #54

Open ctbarbour opened 4 years ago

ctbarbour commented 4 years ago

According to the Code Convention documentation:

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

So the example should probably read:

class Test {
    fun tryAdd(a: Int?, b: Int?): Int? {
        var result: Int? = null
        a?.let { lhs ->
            b?.let { rhs ->
                result = lhs + rhs
            }
        }
        return result
    }
}

However, when putting parameters on the first line Kotlin-mode will indent the next line as follows:

class Test {                                                                                                                                                                             
    fun tryAdd(a: Int?, b: Int?): Int? {                                                                                                                                                 
        var result: Int? = null                                                                                                                                                          
        a?.let { lhs ->                                                                                                                                                                  
                     b?.let { rhs ->                                                                                                                                                     
                                  result = lhs + rhs                                                                                                                                     
                     }                                                                                                                                                                   
        }                                                                                                                                                                                
        return result                                                                                                                                                                    
    }                                                                                                                                                                                    
}

Further when adding normal declarations like variable assignment the first line is indented further than the remaining lines in the lambda:

class Test {                                                                                                                                                                             
    fun tryAdd(a: Int?, b: Int?): Int? {                                                                                                                                                 
        var result: Int? = null                                                                                                                                                          
        a?.let { lhs ->                                                                                                                                                                  
                     val test = "variable assignment"                                                                                                                                    
                 val anotherVariable = "another"                                                                                                                                         
                 b?.let { rhs ->                                                                                                                                                         
                              result = lhs + rhs                                                                                                                                         
                 }                                                                                                                                                                       
        }                                                                                                                                                                                
        return result                                                                                                                                                                    
    }                                                                                                                                                                                    
}
taku0 commented 4 years ago

53 will fix this.

class Test {
    fun tryAdd(a: Int?, b: Int?): Int? {
        var result: Int? = null
        a?.let { lhs ->
            b?.let { rhs ->
                result = lhs + rhs
            }
        }
        return result
    }
}
class Test {
    fun tryAdd(a: Int?, b: Int?): Int? {
        var result: Int? = null
        a?.let { lhs ->
            val test = "variable assignment"
            val anotherVariable = "another"
            b?.let { rhs ->
                result = lhs + rhs
            }
        }
        return result
    }
}
foo {
    context: Context,
    environment: Env
    ->
    context.configureEnv(environment)
}

foo { context: Context, // Non-standard style
      environment: Env
      ->
    context.configureEnv(environment)
}
ctbarbour commented 4 years ago

I'll give that a try. Thanks!

rudolf-adamkovic commented 2 years ago

This still does not work, does it?

assets.open(name).use { source ->
                            // indents here
}

Please reopen. #53 seems to have stalled.

bricka commented 1 year ago

I was also annoyed by this and have an MR up to fix it: https://github.com/Emacs-Kotlin-Mode-Maintainers/kotlin-mode/pull/73

taku0 commented 1 year ago

Now fixed as #53 is merged.