Skip to main content

Module source_format

Module source_format 

Source
Expand description

The canonical LOGOS source formatter.

One rule set shared by every formatting surface (the LSP’s textDocument/formatting provider, its on-type formatter, and largo fmt):

  • code lines reindent to 4 spaces per lexed nesting level — the depth comes from the lexer’s own Indent/Dedent reading, so the canonical form is the structure the program already has,
  • trailing whitespace is removed from code lines; CRLF normalizes to LF,
  • multiline-string interiors are content — not a byte is touched,
  • ## Note/## Example bodies are the author’s prose — untouched (markdown hard-breaks and nested-list indents survive),
  • comment-only lines keep their author indent (tabs still normalize),
  • the presence or absence of a final newline is preserved.

The prime directive, locked by formatter_locks.rs in the test suite: formatting never changes the token stream — and it is a fixed point (format_source(format_source(s)) == format_source(s)).

Functions§

format_line
Format a single line: normalize leading whitespace (tab → 4 spaces) and strip trailing whitespace. Content between the indent and the trailing whitespace is untouched. Line-local — the on-type formatting rule; the whole-document format_source adds structure awareness on top.
format_source
Format a whole source text: structural reindentation for code, raw passthrough for string interiors and prose, LF endings, preserved final newline.