Skip to main content

Module format

Module format 

Source
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’s id @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 @N ordinals,
  • the contents of # capnpfmt: off / # capnpfmt: on regions (stage 6).

Bails (returns None) on any parse error so we never reformat broken input.

Structs§

FormatOutput
Formatter result: the new text plus any unwrappable long-line warnings.
LongLineWarning
One byte range in the original text that exceeded max_width after 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 None if the buffer can’t be safely formatted (parse errors). When the input is already canonical, the returned text equals the input — callers should compare to decide whether to emit a TextEdit.