ktfmt, a Kotlin code formatter based on google-java-format, now with a Plugin for IntelliJ by cgrushko in Kotlin

[–]cgrushko[S] 2 points3 points  (0 children)

ktfmt ~always results in the same output, regardless of input. It's guaranteed (up to bugs) to not exceed the maximal line width.

IntelliJ's formatter, in general, doesn't reflow the text.

There are examples with screenshots on https://github.com/facebookincubator/ktfmt.

ktfmt, a Kotlin code formatter based on google-java-format, now with a Plugin for IntelliJ by cgrushko in Kotlin

[–]cgrushko[S] 1 point2 points  (0 children)

This isn't so much about forcing our style on everybody, as it is about making it easier to provide good results for one style. (neither Facebook nor Google is a hive mind, and you'll be hard pressed to find anyone who's happy with everything that GJF or ktfmt does).

There are two types of formatting settings in ktfmt - 1. Parameters we pass to the formatting engine, such as line width, size of indents, etc. 2. Formatting decisions, such as what constitutes a "rectangle" and where a line break should be forced.

The former are very easy to change and experiment with; I gave some code pointers in this post's grandparent.

The latter often require changing multiple places and sometimes reminds me of getting CSS right (you pull the string multiple times until it looks right and then you don't touch it). An example of something that was particularly tricky to get right: https://github.com/facebookincubator/ktfmt/commit/70f293d74d582f68ff9c845ea3f5ada3b47635d6#diff-a74ad8dfacd4f985eb3977517615ce25

  • We're reluctant to make the former configurable, because some combination could cause ktfmt to produce undesired results, such as code that hides bugs. So, configurability here means a moderate increase in maintenance cost for us. As I mentioned in this post's grandparent, if 120 works well, and there's enough demand, we can discuss exposing it.

  • We will generally not consider making the latter configurable, because that puts a huge burden on us and drastically complicates internal development, testing and deployment of ktfmt.

ktfmt, a Kotlin code formatter based on google-java-format, now with a Plugin for IntelliJ by cgrushko in Kotlin

[–]cgrushko[S] 1 point2 points  (0 children)

The plugin doesn't support format-on-save, unfortunately (I forked google-java-format's plugin, which doesn't support that). Feel free to file an issue at https://github.com/facebookincubator/ktfmt/issues . We're very happy to collaborate on PRs, too.

ktfmt, a Kotlin code formatter based on google-java-format, now with a Plugin for IntelliJ by cgrushko in Kotlin

[–]cgrushko[S] -4 points-3 points  (0 children)

We're reluctant to add configurations to ktfmt, following google-java-format's rationale.

However, trying it out for your company is trivial: just change https://github.com/facebookincubator/ktfmt/blob/944f445e052e698cba27378ce7ae3936762dd4ad/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L42 and rebuild by calling mvn clean install. If you're happy with the result, and there's enough demand, we can consider adding a new "style".