Skip to main content

Crate daml_fmt

Crate daml_fmt 

Source
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§

config

Structs§

FormatCoverage
Structural formatter coverage over modeled constructs.
FormatDiagnostic
A formatter input diagnostic with typed location and category.
FormatError
Formatting failed because the source has lexical or parser diagnostics.
FormatOptions
Formatter behavior switches.
FormatRuleParseError
Error returned when parsing an unknown formatter rule id.
FormatRuleSet
A normalized set of formatter rules.

Enums§

FormatRule
A discrete formatter rule that can be enabled or disabled.
ImportOrder
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.