ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

Stop unwrapping wrapped function chaining

Open tir38 opened this issue 2 years ago • 1 comments

Using version 0.44.

Initial

fun main() {
     return StringBuilder()
      .append("Stop ")
      .append("making this ")
      .append("one line")
      .toString()
}

After formatting

fun main() {
  return StringBuilder().append("Stop ").append("making this ").append("one line").toString()
}

I would expect ktfmt to leave this section alone. The kotlinlang style guide doesn't say that you should or should not use wrapped function chaining, but if you are you should follow these guidelines:

https://kotlinlang.org/docs/coding-conventions.html#wrap-chained-calls

tir38 avatar Sep 23 '23 21:09 tir38

Whenever something like this happens, I use a dummy // force br line comment to prevent this behavior.

fun main() {
     return StringBuilder() // force br
      .append("Stop ")
      .append("making this ")
      .append("one line")
      .toString()
}

okarmazin avatar Sep 29 '23 13:09 okarmazin

This is not something we plan to work on as one of the ideas of ktfmt is that spaces and new lines would not influence the output of the formatter. This is not fully obeyed as we do preserve some empty new lines, but for this particular case we don't plan to make an exception.

You're welcome to submit a PR for this though, I'd gladly review it and personally I also like this suggestion

hick209 avatar Jun 05 '24 15:06 hick209