Expand description
Cap’n Proto schema formatter.
The strategy is intentionally conservative: we walk the tree-sitter parse, collect leaf tokens, and decide the whitespace between each adjacent pair from a small ruleset keyed on the token kinds plus the current brace depth. Token interiors are copied verbatim, so identifiers / string literals / comment contents are never touched.
Things we DO touch:
- indentation (computed from
{...}nesting depth, 2 spaces per level), - spacing around
:(one space before, none after — Kenton’sid @0 :Id;form), - spacing around
=,,,(,),.,;,$,[],->, - blank-line conventions (one between top-level decls; collapse multi-blank runs),
- trailing whitespace (stripped on every line),
- final newline (always one).
Things we DON’T touch in v1:
- the contents of comment paragraphs (no reflow / wrap / bullet detection),
- the contents of doc-comment paragraphs that exceed
max_width(left as-is), - alignment of
@Nordinals, - the contents of
# capnpfmt: off/# capnpfmt: onregions (stage 6).
Bails (returns None) on any parse error so we never reformat broken input.
Structs§
- Format
Output - Formatter result: the new text plus any unwrappable long-line warnings.
- Long
Line Warning - One byte range in the original text that exceeded
max_widthafter formatting and couldn’t be auto-wrapped. The server publishes these as Diagnostics so the user notices them.
Functions§
- format_
document - Formatter entry point. Returns
Noneif the buffer can’t be safely formatted (parse errors). When the input is already canonical, the returnedtextequals the input — callers should compare to decide whether to emit aTextEdit.