question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chained trailing lambdas are formatted incorrectly

See original GitHub issue

ktfmt 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:open
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
nreid260commented, Mar 21, 2022

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:

-----------------------------------------------------
someVar = foo.bar { some -> lambda }.zip(named = arg)
--------------------------------------------
someVar = foo.bar { some -> lambda }.zip(
  named = arg
)
------------------------------
someVar = foo
  .bar { some -> lambda }
  .zip(named = arg)
------------------------------ // This is tricky combo case
someVar =
  someReallyReallyLongRoot
    .bar { some -> lambda }
    .zip(named = arg)
----------------
someVar = foo
  .bar { some ->
    lambda
  }
  .zip(
    named = arg
  )
1reaction
strulovichcommented, Mar 21, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found