Skip to content

Commit

Permalink
Fix indentation of a multiline parameter list inside a function liter…
Browse files Browse the repository at this point in the history
…al for code style `ktlint_official` (#2823)

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

Closes #2816
  • Loading branch information
paul-dingemans authored Oct 4, 2024
1 parent 7655731 commit d829568
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 114 deletions.
25 changes: 23 additions & 2 deletions documentation/snapshot/docs/rules/standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ Enforces the parameters of a function literal and the arrow to be written on the

If the function literal contains multiple parameters and at least one parameter other than the first parameter starts on a new line than all parameters and the arrow are wrapped to separate lines.

=== "[:material-heart:](#) Ktlint"
=== "[:material-heart:](#) Ktlint (ktlint_official)"

```kotlin
val foobar1 = { foo + bar }
Expand All @@ -775,6 +775,27 @@ If the function literal contains multiple parameters and at least one parameter
foo + bar
}
val foobar5 = { foo: Foo, bar: Bar -> foo + bar }
val foobar6 =
{
foo: Foo,
bar: Bar,
->
foo + bar
}

// Assume that the last allowed character is
// at the X character on the right X
val foobar7 =
barrrrrrrrrrrrrr {
fooooooooooooooo: Foo
->
foo.repeat(2)
}
```

=== "[:material-heart:](#) Ktlint (non ktlint_official)"

```kotlin
val foobar6 =
{
foo: Foo,
Expand All @@ -787,7 +808,7 @@ If the function literal contains multiple parameters and at least one parameter
// at the X character on the right X
val foobar7 =
barrrrrrrrrrrrrr {
fooooooooooooooo: Foo
fooooooooooooooo: Foo
->
foo.repeat(2)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ internal class RuleExecutionContext private constructor(
@Deprecated(message = "Remove in Ktlint 2.0")
private fun ((offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision).onlyEmit() =
{
offset: Int,
errorMessage: String,
canBeAutoCorrected: Boolean,
offset: Int,
errorMessage: String,
canBeAutoCorrected: Boolean,
->
this(offset, errorMessage, canBeAutoCorrected)
Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,20 @@ public class IndentationRule :

private fun ASTNode.calculateIndentOfFunctionLiteralParameters() =
if (isFirstParameterOfFunctionLiteralPrecededByNewLine()) {
// val fieldExample =
// LongNameClass {
// paramA,
// paramB,
// paramC ->
// ClassB(paramA, paramB, paramC)
// }
indentConfig.indent.repeat(2)
if (codeStyle == ktlint_official) {
// Indent with single indent as defined in Kotlin Coding conventions
indentConfig.indent
} else {
// Comply with default IDEA formatting although it is not compliant with Kotlin Coding conventions
// val fieldExample =
// LongNameClass {
// paramA,
// paramB,
// paramC ->
// ClassB(paramA, paramB, paramC)
// }
indentConfig.indent.repeat(2)
}
} else {
// Allow default IntelliJ IDEA formatting:
// val fieldExample =
Expand Down
Loading

0 comments on commit d829568

Please sign in to comment.