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

Fix indentation of a multiline parameter list inside a function literal for code style ktlint_official #2823

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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