Expand description
ktrs serve: a long-lived formatter for build tools (the Java shim in java/), so a build pays
for process startup once instead of once per file.
Protocol v2, over stdin/stdout. Every message is a frame: a 4-byte big-endian length, then that
many bytes. The server sends one frame on start, ktrs-serve 2 <version>, then answers each
request frame with one response frame, in order, until stdin closes (exit 0). v2 adds ktlint
requests; v1 (ktfmt only) requests and responses are unchanged.
A request is UTF-8 key=value header lines, an empty line, then the source code. tool=ktlint
makes it a ktlint request ([crate::serve_ktlint] documents its keys); otherwise it is a ktfmt one:
| key | values | default |
|---|---|---|
tool | ktfmt, ktlint | ktfmt |
style | meta, google, kotlinlang | meta |
max-width, block-indent, continuation-indent | positive integers | the style’s |
remove-unused-imports | true, false | true |
trailing-commas | none, only_add, complete | the style’s |
path | the file’s path, for .editorconfig and to prefix messages | none |
editorconfig | true, false: apply .editorconfig at path over the above | false |
A response is status=ok or status=error, changed=true|false (ok only), any tool-specific
header lines (ok only), an empty line, then the formatted code or the error message (ktfmt’s CLI
wording, one line per diagnostic). Unknown keys and bad values are errors for that request only; a
broken frame ends the server (exit 2).