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

[Feature request] Formatter selection / formatting options

Open dr-eme opened this issue 3 years ago • 4 comments

Motivation

We use ktfmt (aka google java style) formatting, and it would be great if to have different formatters options, or at least a way to configure the default one, for instance the default indentation.

Description

Be able to select formatters (e.g. see the ones supported by spotless)

Alternatives considered

Configure formatting options

dr-eme avatar Aug 06 '21 09:08 dr-eme

@dr-eme is https://github.com/pinterest/ktlint what you are after?

dewijones92 avatar Aug 08 '21 08:08 dewijones92

ktlint allows to change some formatting options via the file .editorconfig (https://github.com/pinterest/ktlint#editorconfig). I was not able to make this work. What did actually work, was changing the VS Code setting editor.tabSize. Unfortunately I was not able to configure other formatiing options (e.g. max_line_length) of ktlint. I had a quick look at the source code (https://github.com/fwcd/kotlin-language-server/blob/main/server/src/main/kotlin/org/javacs/kt/formatting/Formatter.kt). This seems currently not to be possible or did someone manage that?

stenzengel avatar Dec 08 '21 13:12 stenzengel

@stenzengel As you might have noticed, in https://github.com/fwcd/kotlin-language-server/pull/238 the ktlint dependency has been swapped with ktfmt, and there is a default formatting configuration. This might be a choice of personal taste, but I wonder if it should be the default config since this will conflict with both ktlint and ktfmt when options.tabSize == 4.

See:

https://github.com/fwcd/kotlin-language-server/blob/705b4e90f70ece54aa3a040d0c305a635844e6c6/server/src/main/kotlin/org/javacs/kt/formatting/Formatter.kt#L11-L15

... and, in the original initializer in ktfmt:

val blockIndent: Int = 2,
val continuationIndent: Int = 4,

PS: If this plugin wants to be opinionated in formatting configurations, its best shot might be to stick with the official style guide, which has been added in ktfmt as a preset:

  /** A format that attempts to reflect https://kotlinlang.org/docs/coding-conventions.html. */
  @JvmField
  val KOTLINLANG_FORMAT = FormattingOptions(style = GOOGLE, blockIndent = 4, continuationIndent = 4)

... and if we follow this path, there is currently a workaround to configure the formatter in a per-project basis if you're using VSCode: Custom Local Formatters.

rami3l avatar Feb 19 '22 10:02 rami3l

It's ideal if we can choose ktlint or ktfmt in settings.json.

tats-u avatar Aug 27 '23 07:08 tats-u