Kotlin / Kotlin/ktfmt

Weird lambda formatting when wrapping long line

Open
#454 3 comments 0 reactions 0 assignees View on GitHub
formatting-discussions
Dominant language
Kotlin
Stars
1.3k
Forks
116
Avg merge
4d 10h
Merged PRs (30d)
15

Description

If I have the following code,

```kotlin
val input = "abc"
val example =
try {
input.fakeFunction(a = "aaaaaaaaaaaaaaa", b = "bbbbbbbbbbb", c = "cccccccccccc").map { strng -> strng.replace("a", "b") }
} catch (error: Exception) {
println()
}
```

`ktfmt` will wrap it as follows:

```kotlin
val input = "abc"
val example =
try {
input.fakeFunction(a = "aaaaaaaaaaaaaaa", b = "bbbbbbbbbbb", c = "cccccccccccc").map { strng
->
strng.replace("a", "b")
}
} catch (error: Exception) {
println()
}
```

The fact that it doesn’t simply put the whole `.map { ... }` on a new line is odd to me. And the `->` on its own line is also unusual. This issue can be reproduced in the online `ktfmt` [playground](https://facebook.github.io/ktfmt/) by setting style to `kotlinlang` and line length to 100.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example in the ktfmt online playground with kotlinlang style and a line length of 100. Trace the formatter behavior for the long chained call and lambda; done means the output avoids placing the lambda arrow alone on a line and uses a less surprising wrap for the .map block.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.