kotlin-language-server icon indicating copy to clipboard operation
kotlin-language-server copied to clipboard

[Proposal] Refactor formatting logic to use external command(s)

Open rami3l opened this issue 3 years ago • 2 comments

After reading several issues about code formatting support (https://github.com/fwcd/kotlin-language-server/issues/29, https://github.com/fwcd/kotlin-language-server/issues/303, https://github.com/fwcd/kotlin-language-server/issues/304), I think it might be feasible to just make a configurable subprocess command and launch it when code formatting is triggered, in the following steps (to be completed):

  1. Fix the default formatting style (of the plugin and of the codebase): https://github.com/fwcd/kotlin-language-server/pull/330.
  2. Add option to customize formatting: TODO.

This proposal is made with the following considerations in mind:

  • There are various ways of formatting a Kotlin codebase. Apart from the ktlint vs. ktfmt debate, each has different installation options (for just Gradle for example, there are existing Gradle plugins like ktlint-gradle and ktfmt-gradle, and there's also spotless).

  • This has been done in similar projects. For example, rust-analyzer simply launches rustfmt to handle this.

  • However, given the fact that there is no rustfmt equivalent in the Kotlin world, and that some might want to use this plugin without an explicit per-project formatter config, it's acceptable to keep the embed ktfmt as the default formatter.

What do you think of this idea? :)

rami3l avatar Feb 19 '22 10:02 rami3l

I think that's a great idea. Can external formatters handle things like ranged formatting? KLS uses a virtual file system, but perhaps there are CLI formatters that work e.g. on Kotlin code passed to stdin.

fwcd avatar Mar 18 '22 16:03 fwcd

@fwcd Ah it's complicated... I thought that ktfmt would have something like --lines but it's an open issue at https://github.com/facebookincubator/ktfmt/issues/218.

So https://github.com/google/vim-codefmt/pull/183 used a workaround which might be interesting to look at...

rami3l avatar Mar 18 '22 21:03 rami3l