Chained trailing lambdas are formatted incorrectly
See original GitHub issuektfmt currently formats this piece of code like this:
private fun initializeRepository(repositoryDir: File) {
val builder = FileRepositoryBuilder()
repository =
runCatching { builder.setGitDir(repositoryDir).build() }.getOrElse { e ->
e.printStackTrace()
null
}
}
While I would expect it to be something like this:
private fun initializeRepository(repositoryDir: File) {
val builder = FileRepositoryBuilder()
- repository =
- runCatching { builder.setGitDir(repositoryDir).build() }.getOrElse { e ->
- e.printStackTrace()
- null
- }
+ repository = runCatching {
+ builder.setGitDir(repositoryDir).build()
+ }.getOrElse { e ->
+ e.printStackTrace()
+ null
+ }
}
It seems that only the last lambda is considered for line breaks and not the intermediate one, which results in ktfmt assuming this line does not fit with the assignment and thus puts it on a new one.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Issues · facebook/ktfmt - GitHub
Chained trailing lambdas are formatted incorrectly. #295 opened on Feb 25 by msfjarvis · 8. Sort modifiers based on Kotlin conventions.
Read more >Coding conventions | Kotlin
In lambdas which are short and not nested, it's recommended to use the it convention instead of declaring the parameter explicitly. In nested ......
Read more >unnecessary-lambda-assignment / C3001 - Pylint 2.16.0-dev ...
Used when a lambda expression is assigned to variable rather than defining a standard function with the "def" keyword. Problematic code:.
Read more >Ruby Style Guide
== vs eql? Blocks, Procs & Lambdas. Proc Application Shorthand; Single-line Blocks Delimiters; Explicit Block Argument; Trailing Comma in Block Parameters ...
Read more >What is the purpose of trailing lambda syntax (Kotlin)?
This syntax gives Kotlin great DSL capabilities, it makes functions look like language constructions. For example:
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I believe under my proposal, we’d get something similar to what’s suggested in https://github.com/facebookincubator/ktfmt/issues/295#issuecomment-1052305658
This is a combination of two kind of tricky problems: what to do with long fluent chains, and should assignment statements be blocklike. There are approaches for both of these separately, but I don’t think they’ll combine very well. It’s easier to see why if we look at more potential cases that would need to work:
Yes, this is pretty pereceptive. Unfortunately, some people for different methods would expect the given version.
@nreid260 is trying to find a golden path to describe our core rules to decide what to do. Maybe he would like to chime in.
In the short term, we’re unlikely to do this change though.