facebook / ktfmt

A program that reformats Kotlin source code to comply with the common community standard for Kotlin code conventions.
https://facebook.github.io/ktfmt/
Apache License 2.0
865 stars 68 forks source link

One line lambdas in `.kts` files #181

Open JavierSegoviaCordoba opened 3 years ago

JavierSegoviaCordoba commented 3 years ago

I am trying in the 2021.1 the IDEA plugin with 0.20.

When I reformat the code in .kts files, I think some lambdas should be treated differently.

For example

plugins {
    id("some-id")
}

becomes

plugins { id("some-id") }

Should ktfmt have the same .kt behavior for .kts files? It is strange to see a lot of usual config blocks in a single line.

cgrushko commented 3 years ago

Interesting. Should this be applied to top-level only? or all lambdas in kts files?

JavierSegoviaCordoba commented 3 years ago

I was doubting about that too. I think all code samples from Gradle will use lambdas in multiline, so initially I think all lambdas should do that (or at least all lambdas inside:

  1. settings.gradle.kts
  2. build.gradle.kts
  3. something.gradle.kts
  4. something.settings.gradle.kts

What do you think if a regex is used to do only in files which ends with gradle.kts?

They can be at the root of a project, but in src/main/kotlin too (precompiled plugins).

JavierSegoviaCordoba commented 3 years ago

Definitely, I think this should be supported in some way.

I have disabled *kts support but I am using it with the IDEA plugin to think a lot about this and I think it is really strange to see the Kotlin code so compacted in Gradle configurations except for the usuals (chaining lambda methods like map).

The problem I see is how we can know if a lambda inside a Gradle file is for a Gradle configuration or it is, indeed, just a supplement method like I mentioned before.

Do you have something thought about this @cgrushko?

cgrushko commented 3 years ago

perhaps all top-level lambdas in kts files should be multiline?

JavierSegoviaCordoba commented 3 years ago

Yeah, I think so too