kotlin-language-server
kotlin-language-server copied to clipboard
[Proposal] Refactor formatting logic to use external command(s)
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):
- Fix the default formatting style (of the plugin and of the codebase): https://github.com/fwcd/kotlin-language-server/pull/330.
- 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
ktlintvs.ktfmtdebate, each has different installation options (for just Gradle for example, there are existing Gradle plugins likektlint-gradleandktfmt-gradle, and there's alsospotless). -
This has been done in similar projects. For example,
rust-analyzersimply launchesrustfmtto handle this. -
However, given the fact that there is no
rustfmtequivalent 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 embedktfmtas the default formatter.
What do you think of this idea? :)
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
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...