Expand description
daml-fmt: a code formatter for Daml, built on the daml-parser pipeline.
Strategy: reconstruct source from parser spans and the lossless
token+trivia stream, never inside string or comment spans (CLAUDE.md:
comments are sacred). Pure reindent and whitespace candidates pass through a
token-equivalence gate: re-lex and require the laid-out token stream,
including Daml’s virtual layout tokens (VLBrace/VSemi/VRBrace), to
match their immediate input. Layout-organizing rewrites intentionally change
layout shape, so the corpus desugar oracle and idempotence checks are the
safety bar for those rules.
The shipping backend is layout_ast (AST-driven, own-design canonical
layout, and NOT aimed at matching an external formatter baseline).
normalize_gaps below is the proven, token-gated whitespace + colon-spacing
pass it composes on top of the structural reindent:
- trailing-whitespace: strip spaces/tabs before a newline; one final newline.
- colon-spacing:
name : Type->name: Type(drop same-line spaces before a lone:type-annotation colon; never::, never a line-leading colon).
§Example
let src = "module M where\nfoo : Int\nfoo = 1\n";
let formatted = daml_fmt::format_source(src);
assert_eq!(formatted, "module M where\nfoo: Int\nfoo = 1\n");§Formatter options
use daml_fmt::{FormatOptions, ImportOrder, format_source_with_options};
let src = "module M where\nimport DA.Optional\nimport DA.List\n\nx = []\n";
// Default: organize imports.
let organized = format_source_with_options(src, FormatOptions::default());
// Preserve declaration order when package identity must stay stable.
let preserved = format_source_with_options(
src,
FormatOptions::new().with_import_order(ImportOrder::Preserve),
);
assert_eq!(organized, "module M where\nimport DA.List\nimport DA.Optional\n\nx = []\n");
assert_eq!(preserved, src);§API posture
This crate is pre-1.0. ImportOrder is #[non_exhaustive] so downstream
match arms stay forward-compatible when new import strategies appear; use
Default for the standard strategy and std::fmt::Display for stable
user-facing labels.
FormatOptions uses private fields and with_* helpers so new switches can
ship with defaults without breaking downstream struct literals.
Modules§
Structs§
- Format
Coverage - Structural formatter coverage over modeled constructs.
- Format
Diagnostic - A formatter input diagnostic with typed location and category.
- Format
Error - Formatting failed because the source has lexical or parser diagnostics.
- Format
Options - Formatter behavior switches.
- Format
Rule Parse Error - Error returned when parsing an unknown formatter rule id.
- Format
Rule Set - A normalized set of formatter rules.
Enums§
- Format
Rule - A discrete formatter rule that can be enabled or disabled.
- Import
Order - Import ordering strategy for formatter output.
Functions§
- coverage
- Count AST formatter structural edit candidates over modeled constructs.
- format_
source - Format Daml source with default formatter options.
- format_
source_ with_ options - Format Daml source with explicit formatter options.
- lex_
diagnostics - Lexer diagnostics for
src. - source_
diagnostics - Source diagnostics for
src, including lexical and parser diagnostics. - try_
format_ source - Format Daml source with default formatter options, rejecting malformed input.
- try_
format_ source_ with_ options - Format Daml source with explicit formatter options, rejecting malformed input.