rustledger_parser/cst/format.rs
1//! Opinionated CST-backed formatter (phase 4.1 of #1262).
2//!
3//! [`format_source`] is a pure function `&str → String`: it
4//! reparses the input into a CST and emits text in one canonical
5//! form per AST shape. Two semantically-equivalent inputs produce
6//! byte-identical output; idempotence (`f(f(x)) == f(x)`) follows
7//! trivially.
8//!
9//! Replaces the pre-#1262 source-level formatter that took
10//! `(source, ParseResult, FormatConfig)` and re-emitted via the
11//! AST-driven `rustledger_core::format` path. Typed-directive
12//! synthesis (`rustledger_core::format::format_directives`) still
13//! lives in `rustledger-core` for callers that build a directive
14//! from scratch (e.g., `rledger add`, importer extract, FFI
15//! `format.entry`) — that's a different shape of input and is
16//! out of scope here.
17//!
18//! # Typed-directive emit: known coupling
19//!
20//! The typed-directive path is a two-pass shim: callers run
21//! `core::format::format_directives` to get bean-format-style text,
22//! then run that text back through [`format_source`] for the
23//! canonical pass. This keeps the FINAL byte sequence single-
24//! sourced (always emitted by this module), but it means
25//! `core::format` is permanently load-bearing as a parser-clean
26//! intermediate and every canonical-form rule needs the legacy
27//! emitter to produce SOMETHING the new parser accepts.
28//!
29//! Call sites (`rustledger-ffi-wasi::router::canonical_format_directives`,
30//! `rustledger::cmd::add_cmd::canonical_format_directive`,
31//! `rustledger::cmd::extract_cmd`) all guard the round-trip with
32//! an explicit `parse(&raw)` step that bails on parse errors, so a
33//! divergence between the two emitters surfaces as a hard error
34//! instead of silently dropping content.
35//!
36//! The eventual fix is a typed-directive emit path on this module
37//! (`format_directive(&Directive) -> String`) that bypasses the
38//! source-string round-trip. Tracked in a follow-up issue.
39//!
40//! # Canonical form (locked in the PR-decision comment on #1262)
41//!
42//! - Indent inside a directive body: 2 spaces. Tabs converted.
43//! - Blank lines between directives: preserved from the source
44//! (#1325). Grouped directives (consecutive `open`s, a `price`
45//! feed) stay grouped; the formatter does not insert or collapse
46//! blank lines, matching Python `bean-format`.
47//! - Blank lines inside a directive: 0.
48//! - Number lexical form: thousands separators dropped; user
49//! decimal-place count preserved.
50//! - Comment content: verbatim.
51//! - Comment positions: normalized to the attachment slot
52//! (header-trailing / inter-directive / body-internal /
53//! posting-trailing).
54//! - Cost spec spacing: `{cost CCY}` (no inner padding).
55//! - Tag/link order on a transaction header: source order, after
56//! the strings.
57//! - Trailing newline at EOF: always exactly one.
58//! - Line endings: LF; CRLF inputs normalized.
59//! - Leading BOM: dropped.
60//!
61//! No `FormatConfig` parameter. One canonical form, no knobs.
62
63use crate::cst::ast::{self, AstNode, AstToken, MetaEntry, SourceFile};
64
65/// Pre-computed alignment data for a whole source file.
66///
67/// Bean-format-style two-axis alignment. The **number field** is a
68/// fixed-width slot starting at column `number_col` and `number_width`
69/// chars wide, into which each posting's number / arithmetic
70/// expression is right-justified. Shorter numbers are left-padded
71/// with spaces, so the currency column (right after the field) is
72/// uniform across the whole file even when individual numbers have
73/// different widths or signs.
74///
75/// - `number_col` = INDENT + max(account width with optional `flag `) + 2
76/// - `number_width` = max rendered width of any posting's number /
77/// arithmetic expression (sign included)
78///
79/// `PostingAlignment` is `Copy` and `Default` (the all-zero state);
80/// the default is the alignment used for files that contain no
81/// postings (no transactions, or transactions with no AMOUNT).
82/// Marked `#[non_exhaustive]` so that a future column-derivation
83/// rule can add fields without breaking downstream consumers.
84///
85/// **Name choice.** The type is qualified by its semantic purpose
86/// (posting layout column widths) so the public path
87/// `rustledger_parser::format::PostingAlignment` doesn't compete
88/// with future generic "alignment" types (text justification,
89/// memory layout, etc.).
90#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
91#[non_exhaustive]
92pub struct PostingAlignment {
93 /// 0-indexed column at which the right-justified number field
94 /// starts.
95 pub number_col: usize,
96 /// Width of the number field; shorter numbers are left-padded
97 /// with spaces so the currency column stays uniform.
98 pub number_width: usize,
99}
100
101/// Two-space indent for directive bodies (postings, metadata).
102const INDENT: &str = " ";
103
104/// Format a Beancount source file in opinionated canonical form.
105///
106/// Reparses internally — callers that already have a CST in hand
107/// and want to avoid the double-parse can use [`format_node`].
108///
109/// Returns canonical text; output always ends with exactly one
110/// trailing newline (even for an empty file, where the output is
111/// just `"\n"`).
112///
113/// **Line-ending normalization runs BEFORE parsing.** The lexer
114/// does not treat bare `\r` as a line terminator, so a classic-
115/// Mac-authored `directive\r…\rdirective\r` would otherwise parse
116/// as a single broken directive and the rest of the user's ledger
117/// would be silently dropped. We normalize `\r\n` and bare `\r`
118/// to `\n` first, then parse — matching the canonical-form
119/// promise that line endings are LF-only on output.
120#[must_use]
121pub fn format_source(source: &str) -> String {
122 let (stripped, _had_bom) = crate::bom::strip_leading(source);
123 let normalized = crlf_to_lf_outside_strings(stripped);
124 let parsed = SourceFile::parse(&normalized);
125 format_node(parsed.syntax())
126}
127
128/// Like [`format_source`] but reuses the caller's
129/// [`crate::ParseResult`] instead of re-parsing `source`.
130///
131/// Skips both expensive pre-passes the bare `format_source` runs
132/// every call: the lex+parse from `SourceFile::parse(&normalized)`,
133/// and the `O(N_postings)` `compute_alignment` walk. Both pieces
134/// are already on `parse_result` (in `syntax_root` and
135/// `alignment` respectively, populated by `parse_via_cst`). For
136/// any consumer that already holds a `ParseResult` — the LSP
137/// `format_document` handler, the FFI `format.source` endpoint,
138/// the WASM `ParsedLedger::format` bridge — this entry skips two
139/// redundant traversals of the file.
140///
141/// **Output equivalence with `format_source`.** Pinned by
142/// `parse_result_alignment_cache::format_source_with_parsed_matches_format_source_under_fallback`
143/// (the fallback exercises broken sources) and
144/// `cst::format::tests::format_source_with_parsed_matches_format_source`
145/// (the cache path exercises clean sources) across LF / CRLF /
146/// BOM / parse-error / mixed-line-ending fixtures. The cache-
147/// path equivalence holds because the formatter rebuilds output
148/// from each directive's typed values rather than echoing
149/// trivia, so the CRLF-vs-LF difference in the underlying CST
150/// trivia never reaches the output. The fallback path is
151/// byte-trivially equivalent (it IS `format_source`).
152///
153/// **CRLF re-injection is still the caller's responsibility.**
154/// Same as `format_source`: this function always returns LF;
155/// LSP consumers that need to preserve CRLF for Windows-
156/// authored files call [`lf_to_crlf_outside_strings`] on the
157/// returned text.
158///
159/// **Parse-error fallback.** When `parse_result.errors` is
160/// non-empty, this function delegates to `format_source(source)`
161/// — losing the cache benefit but preserving byte-identity for
162/// inputs whose CST diverges from what `format_source`'s
163/// pre-parse normalization would produce. Concretely: bare-`\r`
164/// (classic Mac) line terminators are normalized to LF by
165/// `format_source` before parsing, but `parse_via_cst` does NOT
166/// normalize them — so the cached CST treats them as broken
167/// content and `parse_result.errors` is non-empty. The fallback
168/// path keeps the byte-identity claim total instead of
169/// "holds-only-when-clean".
170///
171/// **Stale `parse_result` is the caller's responsibility.** The
172/// producer-side cache invariant (see
173/// [`crate::ParseResult::alignment`] rustdoc) says
174/// `parse_result` must come from a fresh `parse(source)` with
175/// the same `source`. A `debug_assert_eq!` compares the CST's
176/// text length against `source.len() - bom_offset` to catch the
177/// most common mismatched-pair class (different documents have
178/// different lengths) in debug builds; release builds skip the
179/// check. Identical-length mismatches still pass silently —
180/// the rustdoc-level contract remains the source of truth.
181///
182/// # Panics
183///
184/// Panics if `parse_result.syntax_root` is not a `SOURCE_FILE`
185/// (always true for results produced by [`crate::parse`]).
186///
187/// In debug builds, panics on a `(parse_result, source)`
188/// length-mismatch via `debug_assert_eq!`. Release builds
189/// silently emit possibly-wrong output (the producer-only
190/// invariant is the caller's responsibility).
191#[must_use]
192pub fn format_source_with_parsed(parse_result: &crate::ParseResult, source: &str) -> String {
193 // Parse-error fallback. See the function rustdoc for the
194 // rationale: `parse_via_cst` does not run the same input
195 // normalization `format_source` does (no CRLF/bare-CR
196 // normalize), so for sources containing bare-`\r` line
197 // terminators the cached CST is wrong-shaped and the cache
198 // path would diverge from `format_source`. Delegating
199 // preserves byte-identity unconditionally.
200 if !parse_result.errors.is_empty() {
201 return format_source(source);
202 }
203 let node = parse_result.syntax_node();
204 // Defensive length check (debug-only). Catches the most
205 // common form of `(parse_result, source)` mismatched pair —
206 // different documents with different lengths. The CST's
207 // text range is BOM-stripped, so we add back the BOM bytes
208 // if the parser saw one.
209 //
210 // Computed outside the `debug_assert_eq!` to avoid clippy's
211 // `debug_assert_with_mut_call` (`syntax_node()` does an Arc
212 // bump, which clippy treats as state mutation in a debug
213 // context).
214 let cst_len =
215 usize::from(node.text_range().len()) + if parse_result.has_leading_bom { 3 } else { 0 };
216 debug_assert_eq!(
217 cst_len,
218 source.len(),
219 "format_source_with_parsed called with a `source` whose length doesn't \
220 match the CST stored in `parse_result`. The two arguments came from \
221 different documents — the cache path will emit text for the wrong \
222 buffer. See `ParseResult::alignment` rustdoc for the producer-only \
223 invariant.",
224 );
225 format_node_with_alignment(&node, parse_result.alignment)
226}
227
228/// Like [`format_source`], but returns the parse errors instead
229/// of silently formatting around them.
230///
231/// `format_source` is intentionally infallible — the canonical
232/// formatter must still emit *something* for a file the parser
233/// could only recover from. Tooling that wants to refuse to
234/// rewrite a file with parse errors (the `rledger format` CLI,
235/// the LSP `format` handler) previously had to call `parse`
236/// out-of-band, inspect `errors`, then call `format_source` on
237/// the SAME input — a contract two functions cooperated on
238/// implicitly, and the kind of pairing a future caller could
239/// easily forget. This helper makes the contract explicit.
240///
241/// Returns `Ok(formatted)` if and only if `parse(source).errors`
242/// would be empty. Otherwise returns the parse errors verbatim,
243/// in the same order the parser emitted them.
244///
245/// # Errors
246///
247/// Returns `Err(Vec<ParseError>)` containing every parse error
248/// the underlying [`parse`](crate::parse) call would surface for
249/// `source`. The caller decides whether to abort, render the
250/// errors, or fall back to a non-canonical pass.
251pub fn try_format_source(source: &str) -> Result<String, Vec<crate::ParseError>> {
252 let result = crate::parse(source);
253 if !result.errors.is_empty() {
254 return Err(result.errors);
255 }
256 // Reuse the parse + alignment we already produced for the
257 // error gate instead of letting `format_source` re-parse +
258 // re-walk every posting. Byte-identical output pinned by
259 // `format_source_with_parsed_matches_format_source`.
260 Ok(format_source_with_parsed(&result, source))
261}
262
263/// Convert every `\n` line terminator OUTSIDE string literals back
264/// to `\r\n`, leaving `\n` characters inside strings (and inside
265/// comments… see below) untouched.
266///
267/// The canonical form emitted by [`format_source`] is LF-only.
268/// Editors that round-trip Windows-authored files want to see CRLF
269/// echoed back on every line. This helper bridges the two by
270/// walking the canonical output with the shared `SourceState`
271/// state machine. The walker respects:
272///
273/// - String literals: bytes pass through verbatim. The user's
274/// original line endings inside a multi-line narration / note /
275/// document string are preserved.
276/// - Line comments (`;`, `%`, `#!`, `#+`): the comment's
277/// terminating newline IS a real structural line terminator, so
278/// it gets converted to CRLF; bytes inside the comment region
279/// (which can include arbitrary characters, notably stray `"`)
280/// pass through without flipping the in-string state. `#!` and
281/// `#+` open a comment at any column — the lexer's
282/// `SHEBANG` / `EMACS_DIRECTIVE` regexes carry no line-start
283/// anchor, and the state machine matches that classification.
284///
285/// The helper lives in this module rather than the LSP crate
286/// because its correctness depends on the lexer's `STRING` and
287/// comment rules. Keep it co-located with the formatter so a
288/// lexer change forces a co-evaluation here.
289#[must_use]
290pub fn lf_to_crlf_outside_strings(s: &str) -> String {
291 let mut out = String::with_capacity(s.len() + s.matches('\n').count());
292 // BOM is data, not classification input. We re-prepend it
293 // verbatim and let the body start fresh in Code state. The
294 // sibling crlf_to_lf_outside_strings does the same so the two
295 // walkers handle a leading-BOM file identically.
296 let (body, bom) = match s.strip_prefix('\u{FEFF}') {
297 Some(rest) => (rest, "\u{FEFF}"),
298 None => (s, ""),
299 };
300 out.push_str(bom);
301 let mut chars = body.chars().peekable();
302 let mut state = SourceState::Code;
303 let mut prev_was_backslash = false;
304 while let Some(ch) = chars.next() {
305 let peek = chars.peek().copied();
306 match state {
307 SourceState::InString => out.push(ch),
308 SourceState::InComment | SourceState::Code => {
309 if ch == '\n' {
310 out.push_str("\r\n");
311 } else {
312 out.push(ch);
313 }
314 }
315 }
316 state = advance_source_state(ch, peek, state, &mut prev_was_backslash);
317 }
318 out
319}
320
321/// Render typed Beancount `Directive`s in the canonical form
322/// emitted by [`format_source`].
323///
324/// Two-pass pipeline:
325///
326/// 1. Synthesize a source string via the typed-directive emitter
327/// in `rustledger_core::format::format_directives`. That
328/// emitter is `Directive → text`; its output is bean-format-
329/// style, parser-clean, and used here purely as an
330/// intermediate.
331/// 2. Re-parse the synthesized text. If the legacy emitter
332/// produced something the new parser cannot fully accept,
333/// return [`CanonicalizeError::ReparseFailed`] rather than
334/// silently emitting the recoverable subset — that silent-loss
335/// failure mode is what the older `crates/rustledger/tests/
336/// format_compat.rs` (deleted in phase 4.1, distinct from the
337/// phase 4.2 file-pair suite at `crates/rustledger-parser/
338/// tests/format_compat/`) used to guard against. The new file-
339/// pair suite exercises `format_source`, not this two-pass
340/// shim; a future change to `canonicalize_directives`'s error
341/// semantics needs its own dedicated regression test.
342/// 3. Run the re-parsed text through [`format_source`] for the
343/// canonical pass.
344///
345/// Single source of truth for the synthesize → canonicalize
346/// shim. Every consumer that builds a typed `Directive` in memory
347/// and wants canonical text — `rledger add`, `rledger extract`,
348/// the FFI `format.entry` / `format.entries` endpoints — should
349/// call this function instead of reinventing the pipeline.
350pub fn canonicalize_directives<'a, I>(
351 directives: I,
352 config: &rustledger_core::format::FormatConfig,
353) -> Result<String, CanonicalizeError>
354where
355 I: IntoIterator<Item = &'a rustledger_core::Directive>,
356 I::IntoIter: ExactSizeIterator,
357{
358 // Take the count off the ExactSizeIterator without
359 // collecting — the legacy emitter only walks the iterator
360 // once, so we don't need to materialize a Vec just to know
361 // how many directives the caller passed.
362 let iter = directives.into_iter();
363 let input_count = iter.len();
364 let raw = rustledger_core::format::format_directives(iter, config);
365 let parse_result = crate::parse(&raw);
366 if !parse_result.errors.is_empty() {
367 return Err(CanonicalizeError::ReparseFailed {
368 errors: parse_result
369 .errors
370 .iter()
371 .map(ToString::to_string)
372 .collect(),
373 });
374 }
375 // Count check covers the only Directive variants we have
376 // today (12, all of which surface on parse_result.directives).
377 // If a future `rustledger_core::Directive` variant is added
378 // that the parser routes to a different `ParseResult`
379 // collection (e.g., a typed Pushtag whose legacy text the
380 // parser puts on a `pragmas` field), this check needs to
381 // include that field too — otherwise a perfectly healthy
382 // round-trip would always report DirectiveCountMismatch. The
383 // compile-time `_directive_variant_fixture_coverage` match
384 // pins the variant set we're committed to here; any new
385 // variant breaks that match and surfaces this same
386 // maintenance need.
387 let reparsed_count = parse_result.directives.len();
388 if reparsed_count != input_count {
389 return Err(CanonicalizeError::DirectiveCountMismatch {
390 input: input_count,
391 reparsed: reparsed_count,
392 });
393 }
394 Ok(format_source(&raw))
395}
396
397/// Error returned by [`canonicalize_directives`].
398///
399/// Marked `#[non_exhaustive]` so that adding a future variant
400/// (e.g. a `CanonicalizationTimeout` for an async path, or a new
401/// guard for a future canonical-form rule) does not become a
402/// SemVer-breaking change. Consumers must use a `_ => …` arm.
403#[derive(Debug, Clone)]
404#[non_exhaustive]
405pub enum CanonicalizeError {
406 /// The synthesized intermediate failed to re-parse cleanly.
407 /// Carries the rendered error messages so callers can surface
408 /// a diagnostic; the source text itself is not retained
409 /// because it's an internal intermediate the caller has no
410 /// control over.
411 ReparseFailed {
412 /// One rendered message per parse error from the
413 /// intermediate text. Capped at the parser's own error
414 /// limit so this field is bounded.
415 errors: Vec<String>,
416 },
417 /// The synthesized intermediate parsed cleanly but produced a
418 /// different directive count than the input. This indicates
419 /// the legacy emitter and the new parser disagree on what
420 /// constitutes a directive — typically a future
421 /// `rustledger_core::Directive` variant whose legacy text the
422 /// CST parser silently swallows as comments / error-recovery
423 /// trivia. Without this guard, the call would round-trip to
424 /// truncated text with no error returned.
425 DirectiveCountMismatch {
426 /// Number of directives the caller passed in.
427 input: usize,
428 /// Number of directives the parser recovered from the
429 /// synthesized text.
430 reparsed: usize,
431 },
432}
433
434impl std::fmt::Display for CanonicalizeError {
435 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
436 match self {
437 Self::ReparseFailed { errors } => {
438 let preview: Vec<&str> = errors.iter().take(3).map(String::as_str).collect();
439 write!(
440 f,
441 "canonical formatter failed to re-parse the synthesized \
442 directive text ({} error(s)): {}",
443 errors.len(),
444 preview.join("; ")
445 )
446 }
447 Self::DirectiveCountMismatch { input, reparsed } => write!(
448 f,
449 "the canonical formatter could not emit {input} directive(s) \
450 without loss ({reparsed} survived the round-trip). This is \
451 an rledger bug; please report it with the input directives.",
452 ),
453 }
454 }
455}
456
457impl std::error::Error for CanonicalizeError {}
458
459/// Replace CRLF and bare-CR line terminators with LF, but ONLY
460/// outside string literals.
461///
462/// String literals (`"…"`) can contain raw `\r` and `\n` per the
463/// lexer's `STRING` rule; folding CR inside a string would mutate
464/// the user's data. Uses the shared `SourceState` state machine
465/// to track string / comment boundaries.
466///
467/// Cheap fast path: if the input contains no `\r`, returns the
468/// source slice borrowed (no allocation). Used by
469/// [`format_source`] before parsing so the lexer never has to see
470/// legacy line endings. Exposed publicly under [`crlf_to_lf_outside_strings`]
471/// for tooling (CLI `--diff`, format-equivalence checks) that
472/// needs the same string-aware normalization.
473pub fn crlf_to_lf_outside_strings(src: &str) -> std::borrow::Cow<'_, str> {
474 if !src.contains('\r') {
475 return std::borrow::Cow::Borrowed(src);
476 }
477 // Re-prepend the BOM verbatim and let the body start fresh in
478 // Code state. The state machine no longer needs line-start
479 // tracking — the lexer's `SHEBANG` / `EMACS_DIRECTIVE` regexes
480 // have no line-start anchor, so `#!`/`#+` open a comment at
481 // any column, and the state machine mirrors that.
482 let (body, bom) = match src.strip_prefix('\u{FEFF}') {
483 Some(rest) => (rest, "\u{FEFF}"),
484 None => (src, ""),
485 };
486 let mut out = String::with_capacity(src.len());
487 out.push_str(bom);
488 let mut chars = body.chars().peekable();
489 let mut state = SourceState::Code;
490 let mut prev_was_backslash = false;
491 while let Some(ch) = chars.next() {
492 let peek = chars.peek().copied();
493 match state {
494 SourceState::InString => out.push(ch),
495 _ => {
496 if ch == '\r' {
497 out.push('\n');
498 if peek == Some('\n') {
499 chars.next();
500 }
501 } else {
502 out.push(ch);
503 }
504 }
505 }
506 state = advance_source_state(ch, peek, state, &mut prev_was_backslash);
507 }
508 std::borrow::Cow::Owned(out)
509}
510
511/// `true` iff `src` contains at least one `\r` byte OUTSIDE a
512/// string literal — i.e. the byte sequence the canonical
513/// formatter would fold to `\n` via
514/// [`crlf_to_lf_outside_strings`].
515///
516/// This is the explicit predicate companion to the Cow return of
517/// [`crlf_to_lf_outside_strings`]. Tooling that only needs to
518/// know whether the fold would change bytes (the CLI `--diff`
519/// "CR-bearing line endings folded" cause line, the LSP
520/// did-the-formatter-touch-this guard) should call this instead
521/// of matching on `Cow::Owned`, which conflates allocation with
522/// semantic change. A future optimization that pre-allocated the
523/// Cow even on a no-op fold would silently invert that
524/// match-on-Cow guard; this predicate keeps the question
525/// answered by the bytes, not by allocation behavior.
526#[must_use]
527pub fn cr_outside_strings_present(src: &str) -> bool {
528 if !src.contains('\r') {
529 return false;
530 }
531 let body = src.strip_prefix('\u{FEFF}').unwrap_or(src);
532 let mut chars = body.chars().peekable();
533 let mut state = SourceState::Code;
534 let mut prev_was_backslash = false;
535 while let Some(ch) = chars.next() {
536 let peek = chars.peek().copied();
537 if matches!(state, SourceState::Code | SourceState::InComment) && ch == '\r' {
538 return true;
539 }
540 state = advance_source_state(ch, peek, state, &mut prev_was_backslash);
541 }
542 false
543}
544
545/// Per-character walker state for line-ending normalization passes
546/// that must respect string-literal and comment boundaries.
547///
548/// Used by both line-ending helpers: a flat `is_in_string` boolean
549/// is not enough because a quote character inside a `;`/`%` /
550/// `#!` / `#+` comment is data, not a string delimiter.
551#[derive(Debug, Clone, Copy, PartialEq, Eq)]
552enum SourceState {
553 /// In normal code. `"` opens a string; `;` / `%` / `#!` /
554 /// `#+` opens a comment; everything else is just bytes.
555 Code,
556 /// Inside `"…"`. Bytes pass through; an unescaped `"` exits.
557 InString,
558 /// Inside `;…\n`, `%…\n`, `#!…\n`, or `#+…\n`. Bytes pass
559 /// through until LF/CR.
560 InComment,
561}
562
563/// One-step state transition shared by both line-ending helpers.
564///
565/// Returns the state AFTER consuming `ch`. The string-escape
566/// bookkeeping (`prev_was_backslash`) updates in place. Comment
567/// opener detection covers all four line-comment lexemes: `;` and
568/// `%` open a comment unconditionally; `#!` and `#+` open one at
569/// any column — the lexer's `#![^\n\r]*` / `#\+[^\n\r]*` regexes
570/// have NO line-start anchor, so a mid-line `#!` or `#+` is still
571/// a `SHEBANG` / `EMACS_DIRECTIVE` token. A `#` followed by
572/// anything else is a `TAG` / `HASH` token, not a comment.
573const fn advance_source_state(
574 ch: char,
575 peek: Option<char>,
576 state: SourceState,
577 prev_was_backslash: &mut bool,
578) -> SourceState {
579 match state {
580 SourceState::InString => {
581 let is_close = ch == '"' && !*prev_was_backslash;
582 *prev_was_backslash = ch == '\\' && !*prev_was_backslash;
583 if is_close {
584 SourceState::Code
585 } else {
586 SourceState::InString
587 }
588 }
589 SourceState::InComment => {
590 if matches!(ch, '\n' | '\r') {
591 SourceState::Code
592 } else {
593 SourceState::InComment
594 }
595 }
596 SourceState::Code => {
597 let is_hash_line_comment = ch == '#' && matches!(peek, Some('!' | '+'));
598 if ch == '"' {
599 *prev_was_backslash = false;
600 SourceState::InString
601 } else if matches!(ch, ';' | '%') || is_hash_line_comment {
602 SourceState::InComment
603 } else {
604 SourceState::Code
605 }
606 }
607 }
608}
609
610/// Format a `SOURCE_FILE` syntax node in opinionated canonical form.
611///
612/// The bare-node entry for callers that already parsed the CST
613/// (typically LSP formatting providers). Output rules are the
614/// same as [`format_source`].
615///
616/// Internally runs [`compute_alignment`] on `node` to derive the
617/// file-wide column targets. Hot paths that hold a precomputed
618/// `PostingAlignment` (e.g., via [`crate::ParseResult::alignment`]) should
619/// call [`format_node_with_alignment`] instead to skip the
620/// per-call walk. Equivalence pinned by
621/// `format_node_equals_format_node_with_alignment` in this file's
622/// tests.
623#[must_use]
624pub fn format_node(node: &crate::SyntaxNode) -> String {
625 let source_file =
626 SourceFile::cast(node.clone()).expect("format_node called on non-SOURCE_FILE node");
627 let alignment = compute_alignment(&source_file);
628 format_node_with_alignment(node, alignment)
629}
630
631/// Like [`format_node`] but skips the per-call
632/// [`compute_alignment`] walk by accepting a precomputed
633/// `PostingAlignment`.
634///
635/// The cache pattern: parse → take `ParseResult::alignment` (the
636/// pre-computed file-wide alignment, populated by `parse_via_cst`)
637/// → call this function. Subsequent formatting calls on the same
638/// `ParseResult` pay only the per-call emit cost, not the
639/// `O(N_postings)` pre-pass.
640///
641/// `alignment` MUST match what `compute_alignment(&SourceFile::cast(node).unwrap())` would
642/// return for the given `node` — passing a mismatched alignment
643/// is allowed but produces output with non-canonical column
644/// widths. Use `PostingAlignment::default()` for files known to have no
645/// postings (no transactions, or transactions with no AMOUNT).
646///
647/// # Panics
648///
649/// Panics if `node`'s kind is not `SOURCE_FILE`.
650#[must_use]
651pub fn format_node_with_alignment(node: &crate::SyntaxNode, alignment: PostingAlignment) -> String {
652 // Precondition check (debug-only). The bare `format_node`
653 // delegate already validated the kind via the
654 // `SourceFile::cast` it performs for `compute_alignment`, so
655 // for the most common call path (bare → with_alignment) the
656 // debug_assert is a redundant no-op in release. External
657 // direct callers of this entry point (FFI, future LSP
658 // handlers calling `format_node_with_alignment` with a
659 // `parse_result.alignment` cache) get the panic in debug
660 // builds; in release, a wrong-kind `node` produces empty or
661 // malformed output rather than panicking — acceptable for
662 // a precondition that's guaranteed by the call's typed
663 // contract.
664 debug_assert_eq!(
665 node.kind(),
666 crate::SyntaxKind::SOURCE_FILE,
667 "format_node_with_alignment called on non-SOURCE_FILE node (got {:?})",
668 node.kind(),
669 );
670 let mut out = String::new();
671 // Walk every direct child in source order so file-level comments
672 // (file-leading per phase-2.0 trivia attachment, plus file-
673 // trailing) interleave correctly with directives. Inter-directive
674 // and same-line trailing comments live INSIDE the next/owning
675 // directive and surface from `emit_directive`'s leading-trivia
676 // pass.
677 //
678 // Blank-line policy at the top level: PRESERVE the author's blank
679 // lines between directives rather than normalizing to exactly one.
680 // Between two directives, emit as many blank lines as the source
681 // had — including zero, so deliberately grouped runs (consecutive
682 // `open`s, a dense `price` feed) stay grouped instead of being
683 // double-spaced (#1325). This matches Python `bean-format` and the
684 // rest of the beancount formatter lineage (fava,
685 // beancount-language-server, beancount-mode), all of which leave
686 // blank-line structure untouched and only realign amounts.
687 //
688 // Adjacent file-level comments still stay tight as a group (so a
689 // `; ====\n; HEADER\n; ====` section header keeps its visual
690 // grouping), and a comment group sitting against a directive on
691 // either side stays flush.
692 let mut prev_was_directive = false;
693 for el in node.children_with_tokens() {
694 match el {
695 rowan::NodeOrToken::Node(n) => {
696 if let Some(directive) = ast::Directive::cast(n.clone()) {
697 if prev_was_directive {
698 for _ in 0..leading_blank_lines(directive.syntax()) {
699 out.push('\n');
700 }
701 }
702 emit_directive(&directive, alignment, &mut out);
703 prev_was_directive = true;
704 } else if n.kind() == crate::SyntaxKind::ERROR_NODE {
705 // Preserve unparsable content verbatim (#1335): `format`
706 // must never delete the author's text. Org-mode `*`
707 // section headers (and any comments grouped with them)
708 // parse into ERROR_NODEs; emit them as-is rather than
709 // dropping them. Treated like a directive for spacing — an
710 // ERROR_NODE is a top-level content block, so the author's
711 // blank lines around it (before it, and before the next
712 // directive) are preserved, not flushed.
713 if prev_was_directive {
714 for _ in 0..leading_blank_lines(&n) {
715 out.push('\n');
716 }
717 }
718 emit_error_node(&n, &mut out);
719 prev_was_directive = true;
720 }
721 // Any other non-directive node: nothing to emit.
722 }
723 rowan::NodeOrToken::Token(t) => {
724 if matches!(
725 t.kind(),
726 crate::SyntaxKind::COMMENT
727 | crate::SyntaxKind::PERCENT_COMMENT
728 | crate::SyntaxKind::SHEBANG
729 | crate::SyntaxKind::EMACS_DIRECTIVE
730 ) {
731 out.push_str(t.text().trim_end_matches(['\n', '\r']));
732 out.push('\n');
733 prev_was_directive = false;
734 }
735 }
736 }
737 }
738 if !out.ends_with('\n') {
739 out.push('\n');
740 }
741 out
742}
743
744/// Format the subset of `node`'s top-level children that intersect
745/// `range`, returning the snapped byte range and the canonical-form
746/// replacement text.
747///
748/// This is the building block for the LSP `textDocument/rangeFormatting`
749/// provider: the client sends a `Range`, the server snaps it up to
750/// the smallest set of top-level structural nodes (directives or
751/// standalone comments) that intersect the selection, formats those
752/// nodes the same way [`format_node`] formats the whole file, and
753/// returns a single `TextEdit` replacing the snapped range. The
754/// alternative — formatting a substring of the source — would have
755/// to either invent a partial canonical form (creating a second
756/// truth alongside the whole-file canonical form, the failure mode
757/// that bit #1252) or refuse to format anything that crosses a
758/// structural boundary. Snapping up to top-level boundaries is the
759/// only choice that lets the same canonical-form rules apply.
760///
761/// **Frame.** `range` is in the *CST* byte frame — the same frame
762/// the syntax node's `TextRange`s use. The LSP handler is
763/// responsible for shifting `bom_offset` at the input/output
764/// boundary (mirrors the [`super::super::SyntaxNode`] /
765/// `selection_range` handler convention; see
766/// `ParseResult::syntax_root` rustdoc for the rationale).
767///
768/// **Behavior.**
769///
770/// - If `range` intersects no top-level Directive or standalone
771/// COMMENT/SHEBANG/EMACS token, returns `None`. The LSP handler
772/// surfaces `None` directly (serialized as `null` per LSP, not
773/// as `[]`); the client treats it as "nothing to format".
774/// - If the computed snap range would cover any top-level
775/// `ERROR_NODE` byte, returns `None`. **Range formatting refuses
776/// to delete user content the parser couldn't classify.** This
777/// diverges from [`format_node`], which silently drops
778/// `ERROR_NODE` children on the whole-file path; the rationale
779/// is the per-handler asymmetry the LSP exposes — the user
780/// pressing "Format Selection" expects either a clean
781/// reformat or a no-op, never a silent partial delete of an
782/// in-progress directive. Tooling that genuinely wants to drop
783/// broken regions can still call [`format_node`] on the same
784/// node.
785/// - Otherwise returns `Some((snap, text))` where `snap` is the
786/// union of the included children's text ranges (so it begins at
787/// the first included child's start and ends at the last
788/// included child's end, including each child's leading-trivia
789/// prefix per the phase-2.0 Directive-Terminator Rule) and
790/// `text` is the canonical-form replacement.
791/// - Cursor-only selection (`range.is_empty()`): the child at the
792/// cursor is included if the cursor is strictly inside it OR is
793/// exactly at the child's start. Boundary at the child's end
794/// belongs to the next child, not the previous one — matches
795/// the standard "end-of-line cursor is start-of-next-line"
796/// convention.
797///
798/// **Posting alignment.** The pre-pass uses the FULL `SourceFile`, not
799/// the selected subset. A selection that formats one transaction
800/// in a file with many other transactions inherits the file's
801/// alignment columns, so the formatted output stays visually
802/// aligned with un-formatted postings elsewhere. The opposite
803/// policy (per-selection alignment) would create a jarring
804/// visual jump every time the user re-formats a sub-range.
805///
806/// **Round-trip invariant.** For any `range` that contains every
807/// top-level child, the returned text equals the result of
808/// [`format_node`] on the same node. Pinned by
809/// `format_node_range_full_range_matches_format_node` in this
810/// file's test module.
811///
812/// # Panics
813///
814/// Panics if `node`'s kind is not `SOURCE_FILE` — same precondition
815/// as [`format_node`].
816#[must_use]
817pub fn format_node_range(
818 node: &crate::SyntaxNode,
819 range: rowan::TextRange,
820) -> Option<(rowan::TextRange, String)> {
821 let source_file =
822 SourceFile::cast(node.clone()).expect("format_node_range called on non-SOURCE_FILE node");
823 // File-wide alignment pre-pass: see rustdoc above for the
824 // rationale. The selected subset always uses the full file's
825 // alignment columns. Hot paths with a precomputed `PostingAlignment`
826 // should call `format_node_range_with_alignment` instead.
827 let alignment = compute_alignment(&source_file);
828 format_node_range_with_alignment(node, range, alignment)
829}
830
831/// Like [`format_node_range`] but skips the per-call
832/// [`compute_alignment`] walk by accepting a precomputed
833/// `PostingAlignment`.
834///
835/// The cache pattern is identical to
836/// [`format_node_with_alignment`]: parse → take
837/// `ParseResult::alignment` → call this function. The hot path the
838/// cache addresses is the LSP `textDocument/rangeFormatting`
839/// fallback (CST-snap path that fires on parse-error files), which
840/// can be invoked per-keystroke through format-on-type clients.
841/// Without the cache the per-call cost is
842/// `O(N_postings_in_file)`; with the cache it's
843/// `O(N_cst_nodes covered by range)`.
844///
845/// `alignment` MUST match what `compute_alignment(&SourceFile::cast(node).unwrap())` would
846/// return for the given `node`; pinned by
847/// `format_node_range_matches_format_node_range_with_alignment`. Same
848/// `range` semantics, `ERROR_NODE` policy, snap rules, and
849/// `# Panics` precondition as [`format_node_range`].
850#[must_use]
851pub fn format_node_range_with_alignment(
852 node: &crate::SyntaxNode,
853 range: rowan::TextRange,
854 alignment: PostingAlignment,
855) -> Option<(rowan::TextRange, String)> {
856 // Precondition check (debug-only). Same rationale as
857 // `format_node_with_alignment`: the bare delegate already
858 // validated the kind, so the most common call path (bare →
859 // with_alignment) gets no release-build cost from this
860 // assert. External direct callers — the LSP range_formatting
861 // fallback, FFI, future format-on-type — get a debug-build
862 // panic; release-build wrong-kind input produces no output
863 // (rather than panicking).
864 debug_assert_eq!(
865 node.kind(),
866 crate::SyntaxKind::SOURCE_FILE,
867 "format_node_range_with_alignment called on non-SOURCE_FILE node (got {:?})",
868 node.kind(),
869 );
870
871 // First pass: identify the included children and the snap range.
872 // We pick:
873 // - Directive nodes whose `text_range` intersects `range`
874 // - top-level COMMENT/PERCENT_COMMENT/SHEBANG/EMACS_DIRECTIVE
875 // tokens whose range intersects `range`
876 // ERROR_NODE and other non-Directive nodes are skipped (matches
877 // `format_node`); a selection that lands only on them returns
878 // None below.
879 let mut snap_start: Option<rowan::TextSize> = None;
880 let mut snap_end: Option<rowan::TextSize> = None;
881 let mut any_included = false;
882 for el in node.children_with_tokens() {
883 let (kind, child_range) = (el.kind(), el.text_range());
884 let is_formattable = match &el {
885 rowan::NodeOrToken::Node(n) => ast::Directive::cast(n.clone()).is_some(),
886 rowan::NodeOrToken::Token(_) => matches!(
887 kind,
888 crate::SyntaxKind::COMMENT
889 | crate::SyntaxKind::PERCENT_COMMENT
890 | crate::SyntaxKind::SHEBANG
891 | crate::SyntaxKind::EMACS_DIRECTIVE
892 ),
893 };
894 if !is_formattable {
895 continue;
896 }
897 if !range_intersects(child_range, range) {
898 continue;
899 }
900 any_included = true;
901 snap_start = Some(snap_start.map_or(child_range.start(), |s| s.min(child_range.start())));
902 snap_end = Some(snap_end.map_or(child_range.end(), |e| e.max(child_range.end())));
903 }
904 if !any_included {
905 return None;
906 }
907 let snap = rowan::TextRange::new(snap_start.unwrap(), snap_end.unwrap());
908
909 // ERROR_NODE intersection bail: if the snap range covers any
910 // top-level ERROR_NODE byte, refuse to format and return None.
911 // Range formatting must not silently delete content the parser
912 // could not classify — without this guard, a selection
913 // spanning two valid directives with an ERROR_NODE between
914 // them would emit a TextEdit that replaces all three with
915 // just the two formatted directives, deleting the user's
916 // in-progress source bytes.
917 //
918 // This is the deliberate divergence from `format_node`'s
919 // whole-file policy: the whole-file path runs on the
920 // assumption that the caller (CLI / FFI / `try_format_source`)
921 // has already decided to accept content loss; the per-handler
922 // LSP path has no such opt-in. The cost is occasional
923 // "format-selection did nothing" UX while a parse error sits
924 // inside the snap; the benefit is no data loss.
925 for el in node.children_with_tokens() {
926 if !matches!(el.kind(), crate::SyntaxKind::ERROR_NODE) {
927 continue;
928 }
929 let er = el.text_range();
930 // Strict-overlap check: an ERROR_NODE whose end touches
931 // snap.start (or start touches snap.end) is adjacent, not
932 // overlapping — those are safe to emit alongside.
933 if er.end() > snap.start() && er.start() < snap.end() {
934 return None;
935 }
936 }
937
938 // Second pass: emit only the children whose range falls
939 // inside `snap`. We re-walk rather than caching the first
940 // pass because the second pass needs to maintain the
941 // `prev_was_directive` blank-line state in source order, and
942 // the child set is small enough that the second walk is
943 // cheap. (Re-walking also keeps the data-flow obvious: snap
944 // computation and emission are two distinct concerns.)
945 let mut out = String::new();
946 let mut prev_was_directive = false;
947 for el in node.children_with_tokens() {
948 let child_range = el.text_range();
949 // Use the snap range (not the input `range`) so we emit
950 // every child WITHIN the snap, even those that the
951 // original selection didn't directly intersect but that
952 // sit between two intersecting children. Without this,
953 // ERROR_NODE-free trivia between two selected directives
954 // would be re-formatted into our output (the comment
955 // pass picks them up), which matches `format_node`.
956 if child_range.end() <= snap.start() || child_range.start() >= snap.end() {
957 continue;
958 }
959 match el {
960 rowan::NodeOrToken::Node(n) => {
961 // ERROR_NODEs never reach here: the range path bails out
962 // above (returns None) when the snap covers one, so it
963 // refuses to format rather than risk touching unparsable
964 // content. Only the whole-file path preserves them verbatim.
965 let Some(directive) = ast::Directive::cast(n) else {
966 continue;
967 };
968 // Preserve the author's inter-directive blank lines
969 // (#1325), identically to `format_node_with_alignment`,
970 // so range formatting and whole-file formatting agree.
971 //
972 // The FIRST directive emitted from the snap needs care:
973 // its predecessor may sit OUTSIDE the selection, but the
974 // blank lines between them are this directive's leading
975 // trivia (the Directive-Terminator Rule), so they fall
976 // INSIDE the snapped range. Dropping them would delete
977 // the blank line above the selection. Emit them whenever
978 // a directive precedes this one in the file — the same
979 // condition the whole-file path expresses as
980 // `prev_was_directive`. For the file's first directive
981 // (no predecessor) there is nothing to preserve.
982 let preceded_by_directive = prev_was_directive
983 || directive
984 .syntax()
985 .prev_sibling()
986 .and_then(ast::Directive::cast)
987 .is_some();
988 if preceded_by_directive {
989 for _ in 0..leading_blank_lines(directive.syntax()) {
990 out.push('\n');
991 }
992 }
993 emit_directive(&directive, alignment, &mut out);
994 prev_was_directive = true;
995 }
996 rowan::NodeOrToken::Token(t) => {
997 if matches!(
998 t.kind(),
999 crate::SyntaxKind::COMMENT
1000 | crate::SyntaxKind::PERCENT_COMMENT
1001 | crate::SyntaxKind::SHEBANG
1002 | crate::SyntaxKind::EMACS_DIRECTIVE
1003 ) {
1004 out.push_str(t.text().trim_end_matches(['\n', '\r']));
1005 out.push('\n');
1006 prev_was_directive = false;
1007 }
1008 }
1009 }
1010 }
1011 if !out.ends_with('\n') {
1012 out.push('\n');
1013 }
1014 Some((snap, out))
1015}
1016
1017/// Whether `child` (a CST node's text range) intersects the
1018/// caller's selection. Zero-width selections (a cursor with no
1019/// extent) are handled specially: the cursor counts as "inside"
1020/// a child if the cursor is strictly inside the child's range or
1021/// is exactly at the child's start. Boundary at the child's end
1022/// is NOT a match — it belongs to the next child, matching
1023/// editors' "end-of-line cursor = start of next line" convention.
1024fn range_intersects(child: rowan::TextRange, sel: rowan::TextRange) -> bool {
1025 if sel.is_empty() {
1026 child.contains(sel.start()) || sel.start() == child.start()
1027 } else {
1028 child.start() < sel.end() && sel.start() < child.end()
1029 }
1030}
1031
1032/// Compute the file-wide alignment columns for a parsed `SourceFile`.
1033///
1034/// Walks every Transaction's postings once, takes the max LHS
1035/// width (account + optional `flag `) and max number-text width,
1036/// and derives the column targets from them.
1037///
1038/// **`O(N_postings)`.** Public so consumers can pre-compute the
1039/// alignment once (typically at parse time) and pass the cached
1040/// `PostingAlignment` into [`format_node_with_alignment`] or
1041/// [`format_node_range_with_alignment`] — eliminates the per-call
1042/// walk in hot formatting paths (LSP format-on-type through a
1043/// parse error, repeat-format scripts, etc.).
1044///
1045/// **Tree-shape precondition.** `sf` must be a `SourceFile` whose
1046/// CST was produced by `parse_structured` (directly or transitively
1047/// via `parse_via_cst` / `parse`). Hand-built partial trees (e.g.,
1048/// a `GreenNodeBuilder` invocation for snippet formatting) silently
1049/// return `PostingAlignment::default()` because their wrapping
1050/// nodes fail the `ast::Directive::Transaction::cast` check.
1051/// Likewise, transactions wrapped in `ERROR_NODE` by mid-edit
1052/// error recovery are excluded — see
1053/// `parse_result_alignment_cache::mid_transaction_error_node` for
1054/// the pinned behavior. The function never panics on a partial
1055/// tree; it just returns the all-zero alignment for the no-postings
1056/// case.
1057///
1058/// **Pinning the contract.** `ParseResult::alignment` is populated
1059/// by calling this function during `parse_via_cst`; the equivalence
1060/// between the cached value and a fresh call is guaranteed by the
1061/// `parse_result_alignment_cache::*` regression tests (7 fixtures) in
1062/// this module.
1063#[must_use]
1064pub fn compute_alignment(sf: &SourceFile) -> PostingAlignment {
1065 let mut max_lhs: usize = 0;
1066 let mut max_num: usize = 0;
1067 // Tracks postings that actually render a number — the only ones that
1068 // participate in alignment. A file whose postings render no numbers
1069 // gets `PostingAlignment::default()`, matching the type docs.
1070 let mut any_aligned_posting = false;
1071 for directive in sf.directives() {
1072 let ast::Directive::Transaction(t) = directive else {
1073 continue;
1074 };
1075 for child in t.syntax().children() {
1076 let Some(p) = ast::Posting::cast(child) else {
1077 continue;
1078 };
1079 let mut lhs = 0usize;
1080 if let Some(flag) = p.flag() {
1081 lhs += flag.text().chars().count() + 1; // `! ` etc.
1082 }
1083 if let Some(account) = p.account() {
1084 lhs += account.text().chars().count();
1085 }
1086
1087 // Only postings that render a number drive the alignment
1088 // column. `bean-format` computes the number column from the
1089 // prefixes of number-bearing lines only, so two kinds of
1090 // posting must NOT push the column right:
1091 // - amount-less postings (the elided balancing leg, or a
1092 // long account with no amount), and
1093 // - currency-only amounts (`Assets:Cash USD`), which
1094 // `emit_posting` prints with no number at all.
1095 // Counting either is why `rledger format` and `bean-format`
1096 // disagreed and round-tripping never converged (issue #1290).
1097 // `amount_number_text` is the shared predicate that keeps
1098 // this pre-pass in lockstep with `emit_posting`.
1099 if let Some(amt) = p.amount()
1100 && let Some(text) = amount_number_text(&amt)
1101 {
1102 any_aligned_posting = true;
1103 max_lhs = max_lhs.max(lhs);
1104 max_num = max_num.max(text.chars().count());
1105 }
1106 }
1107 }
1108 if !any_aligned_posting {
1109 return PostingAlignment::default();
1110 }
1111 // 2 spaces between the longest account end and the number field,
1112 // matching the conventional Beancount layout.
1113 PostingAlignment {
1114 number_col: INDENT.len() + max_lhs + 2,
1115 number_width: max_num,
1116 }
1117}
1118
1119/// The rendered number / arithmetic-expression text of an amount *if it
1120/// renders a number*, or `None` when it renders nothing (a currency-only
1121/// amount like `USD`, whose value text is empty). EXCLUDES the trailing
1122/// currency; sign (if any) is included.
1123///
1124/// This is the single source of truth for "does this posting line have a
1125/// number?". Both the file-wide alignment pre-pass ([`compute_alignment`])
1126/// and the emitter ([`emit_posting`]) consult it, so they can never
1127/// disagree about which postings participate in alignment — the bug
1128/// class behind #1290 (amount-less postings) and its currency-only
1129/// sibling.
1130fn amount_number_text(amt: &ast::Amount) -> Option<String> {
1131 let text = amount_value_text(amt);
1132 (!text.is_empty()).then_some(text)
1133}
1134
1135/// Render an amount's value portion (number or arithmetic
1136/// expression) as a string, EXCLUDING the trailing currency.
1137/// Mirrors the value half of [`format_amount`].
1138fn amount_value_text(amt: &ast::Amount) -> String {
1139 let mut buf = String::new();
1140 if amt.is_arithmetic() {
1141 emit_amount_subnode_expression(amt.syntax(), &mut buf);
1142 return buf;
1143 }
1144 if let Some(sign) = amt.sign()
1145 && sign.is_minus()
1146 {
1147 buf.push('-');
1148 }
1149 if let Some(n) = amt.number() {
1150 buf.push_str(&canonical_number(n.text()));
1151 }
1152 buf
1153}
1154
1155fn emit_directive(d: &ast::Directive, align: PostingAlignment, out: &mut String) {
1156 // Leading inter-directive trivia: COMMENT tokens that sit
1157 // BEFORE the directive's first content token. Per phase-2.0
1158 // trivia attachment, these live inside the directive's syntax
1159 // node — emit them as their own lines BEFORE the canonical
1160 // content.
1161 emit_leading_comments(d.syntax(), out);
1162
1163 // Capture an optional same-line trailing comment so we can
1164 // splice it back in immediately before the directive's
1165 // terminating NEWLINE — see the comment-aware emit loop at
1166 // the bottom of this function.
1167 let trailing = collect_trailing_comment(d.syntax());
1168
1169 let len_before = out.len();
1170 match d {
1171 ast::Directive::Open(d) => emit_open(d, out),
1172 ast::Directive::Close(d) => emit_close(d, out),
1173 ast::Directive::Commodity(d) => emit_commodity(d, out),
1174 ast::Directive::Note(d) => emit_note(d, out),
1175 ast::Directive::Event(d) => emit_event(d, out),
1176 ast::Directive::Query(d) => emit_query(d, out),
1177 ast::Directive::Pad(d) => emit_pad(d, out),
1178 ast::Directive::Document(d) => emit_document(d, out),
1179 ast::Directive::Price(d) => emit_price(d, out),
1180 ast::Directive::Balance(d) => emit_balance(d, out),
1181 ast::Directive::Custom(d) => emit_custom(d, out),
1182 ast::Directive::Option(d) => emit_option(d, out),
1183 ast::Directive::Include(d) => emit_include(d, out),
1184 ast::Directive::Plugin(d) => emit_plugin(d, out),
1185 ast::Directive::Pushtag(d) => emit_pushtag(d, out),
1186 ast::Directive::Poptag(d) => emit_poptag(d, out),
1187 ast::Directive::Pushmeta(d) => emit_pushmeta(d, out),
1188 ast::Directive::Popmeta(d) => emit_popmeta(d, out),
1189 ast::Directive::Transaction(d) => emit_transaction(d, align, out),
1190 }
1191 // Splice the same-line trailing comment in: find the FIRST '\n'
1192 // after `len_before` (= end of the directive's header line in
1193 // the emitted bytes) and insert `" ; comment"` before it. For
1194 // single-line directives the first '\n' is also the only one
1195 // and this lands the comment on the directive line. For multi-
1196 // line transactions it lands the comment on the header line
1197 // (where the source had it), not after the body.
1198 if let Some(c) = trailing
1199 && let Some(newline_rel) = out[len_before..].find('\n')
1200 {
1201 let insert_at = len_before + newline_rel;
1202 let mut splice = String::with_capacity(c.len() + 1);
1203 splice.push(' ');
1204 splice.push_str(&c);
1205 out.insert_str(insert_at, &splice);
1206 }
1207}
1208
1209/// Emit an `ERROR_NODE`'s text verbatim, so `format` never deletes content it
1210/// could not parse (#1335) — chiefly org-mode `*` section headers and the
1211/// comments grouped with them. Only trailing whitespace per line is stripped
1212/// (the formatter's no-trailing-space policy) and the node's trailing newlines
1213/// are collapsed to one; everything else — including blank lines, comments and
1214/// the unparsable lines themselves — is preserved exactly as written.
1215fn emit_error_node(node: &crate::SyntaxNode, out: &mut String) {
1216 let text = node.text().to_string();
1217 // Trim leading AND trailing blank lines: the caller emits the leading
1218 // blank lines (via `leading_blank_lines`) so emitting them here too would
1219 // double-count them and break idempotence. Internal blank lines and the
1220 // content (org headers, grouped comments) are preserved.
1221 for line in text.trim_matches(['\n', '\r']).split('\n') {
1222 out.push_str(line.trim_end());
1223 out.push('\n');
1224 }
1225}
1226
1227/// Number of blank lines the author left immediately before this
1228/// directive's first visible line (its leading comment, if any, else
1229/// its content). Each NEWLINE in the leading trivia that precedes the
1230/// first comment / content token is exactly one blank line: the
1231/// previous directive owns its own terminator NEWLINE (the Directive-
1232/// Terminator Rule), so this node's leading NEWLINEs are purely the
1233/// blank gap, with no off-by-one. WHITESPACE-only "blank" lines count
1234/// too (the NEWLINE that ends them is included). Scanning stops at the
1235/// first comment or content token, so a blank line sitting *between* a
1236/// leading comment and the directive's content is not counted here
1237/// (that gap is collapsed by `emit_leading_comments`, as before).
1238fn leading_blank_lines(node: &crate::SyntaxNode) -> usize {
1239 let mut blanks = 0;
1240 for el in node.children_with_tokens() {
1241 let rowan::NodeOrToken::Token(t) = el else {
1242 break;
1243 };
1244 match t.kind() {
1245 crate::SyntaxKind::NEWLINE => blanks += 1,
1246 crate::SyntaxKind::WHITESPACE => {}
1247 // First comment or content token — past the leading gap.
1248 _ => break,
1249 }
1250 }
1251 blanks
1252}
1253
1254/// Walk the directive's direct-child tokens until the first
1255/// non-trivia token, emitting each `COMMENT` (and `PERCENT_COMMENT`)
1256/// on its own line. Whitespace and newlines in the leading region
1257/// are ignored — the canonical form controls inter-directive
1258/// blank-line spacing separately.
1259fn emit_leading_comments(node: &crate::SyntaxNode, out: &mut String) {
1260 for el in node.children_with_tokens() {
1261 let rowan::NodeOrToken::Token(t) = el else {
1262 break;
1263 };
1264 match t.kind() {
1265 crate::SyntaxKind::COMMENT | crate::SyntaxKind::PERCENT_COMMENT => {
1266 out.push_str(t.text().trim_end_matches(['\n', '\r']));
1267 out.push('\n');
1268 }
1269 crate::SyntaxKind::WHITESPACE | crate::SyntaxKind::NEWLINE => {}
1270 _ => break,
1271 }
1272 }
1273}
1274
1275/// Return the directive's same-line trailing comment (if any) —
1276/// the COMMENT token that appears between the LAST non-trivia
1277/// content token and the directive-terminating NEWLINE on the
1278/// header line. Returns the verbatim comment text (no trailing
1279/// newline).
1280fn collect_trailing_comment(node: &crate::SyntaxNode) -> Option<String> {
1281 // Find the directive-header terminating NEWLINE: the FIRST
1282 // direct-child NEWLINE that follows at least one non-trivia
1283 // content token. (For single-line directives there's only one
1284 // NEWLINE; for transactions the header line is the first
1285 // NEWLINE, after which postings/metadata follow.)
1286 let mut header_nl_idx: Option<usize> = None;
1287 let mut saw_content = false;
1288 let tokens: Vec<crate::SyntaxToken> = node
1289 .children_with_tokens()
1290 .filter_map(rowan::NodeOrToken::into_token)
1291 .collect();
1292 for (i, t) in tokens.iter().enumerate() {
1293 let k = t.kind();
1294 if k == crate::SyntaxKind::NEWLINE && saw_content {
1295 header_nl_idx = Some(i);
1296 break;
1297 }
1298 if !matches!(
1299 k,
1300 crate::SyntaxKind::WHITESPACE
1301 | crate::SyntaxKind::NEWLINE
1302 | crate::SyntaxKind::COMMENT
1303 | crate::SyntaxKind::PERCENT_COMMENT
1304 ) {
1305 saw_content = true;
1306 }
1307 }
1308 // EOF-without-newline fallback: if there is no header-
1309 // terminating NEWLINE, the directive runs to the end of the
1310 // file. Scan from the LAST token instead. A `?` early-return
1311 // here previously dropped same-line trailing comments at the
1312 // final line of a file that lacked a trailing newline, e.g.
1313 // `2024-01-15 open Assets:A ; trailing` (no `\n`). The
1314 // canonical formatter restores the trailing newline, but the
1315 // comment was already gone.
1316 let nl_idx = header_nl_idx.unwrap_or(tokens.len());
1317 // Scan backwards from the header NEWLINE (or EOF): the
1318 // trailing comment is the last COMMENT before the NEWLINE
1319 // separated only by WHITESPACE.
1320 for i in (0..nl_idx).rev() {
1321 let k = tokens[i].kind();
1322 if matches!(
1323 k,
1324 crate::SyntaxKind::COMMENT | crate::SyntaxKind::PERCENT_COMMENT
1325 ) {
1326 return Some(tokens[i].text().to_string());
1327 }
1328 if k != crate::SyntaxKind::WHITESPACE {
1329 return None;
1330 }
1331 }
1332 None
1333}
1334
1335// ---- Single-line directives ------------------------------------
1336
1337fn emit_open(d: &ast::OpenDirective, out: &mut String) {
1338 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1339 let account = d
1340 .account()
1341 .map(|t| t.text().to_string())
1342 .unwrap_or_default();
1343 out.push_str(&date);
1344 out.push_str(" open ");
1345 out.push_str(&account);
1346 for currency in d.currencies() {
1347 out.push(' ');
1348 out.push_str(currency.text());
1349 }
1350 if let Some(booking) = d.booking_method() {
1351 // `booking.text()` includes the surrounding quotes.
1352 out.push(' ');
1353 out.push_str(booking.text());
1354 }
1355 out.push('\n');
1356 emit_meta_entries_of(d.syntax(), out);
1357}
1358
1359fn emit_close(d: &ast::CloseDirective, out: &mut String) {
1360 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1361 let account = d
1362 .account()
1363 .map(|t| t.text().to_string())
1364 .unwrap_or_default();
1365 out.push_str(&date);
1366 out.push_str(" close ");
1367 out.push_str(&account);
1368 out.push('\n');
1369 emit_meta_entries_of(d.syntax(), out);
1370}
1371
1372fn emit_commodity(d: &ast::CommodityDirective, out: &mut String) {
1373 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1374 let currency = d
1375 .currency()
1376 .map(|t| t.text().to_string())
1377 .unwrap_or_default();
1378 out.push_str(&date);
1379 out.push_str(" commodity ");
1380 out.push_str(¤cy);
1381 out.push('\n');
1382 emit_meta_entries_of(d.syntax(), out);
1383}
1384
1385fn emit_note(d: &ast::NoteDirective, out: &mut String) {
1386 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1387 let account = d
1388 .account()
1389 .map(|t| t.text().to_string())
1390 .unwrap_or_default();
1391 let text = d.text().map(|s| s.text().to_string()).unwrap_or_default();
1392 out.push_str(&date);
1393 out.push_str(" note ");
1394 out.push_str(&account);
1395 out.push(' ');
1396 out.push_str(&text);
1397 out.push('\n');
1398 emit_meta_entries_of(d.syntax(), out);
1399}
1400
1401fn emit_event(d: &ast::EventDirective, out: &mut String) {
1402 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1403 let event_type = d
1404 .event_type()
1405 .map(|s| s.text().to_string())
1406 .unwrap_or_default();
1407 let value = d.value().map(|s| s.text().to_string()).unwrap_or_default();
1408 out.push_str(&date);
1409 out.push_str(" event ");
1410 out.push_str(&event_type);
1411 out.push(' ');
1412 out.push_str(&value);
1413 out.push('\n');
1414 emit_meta_entries_of(d.syntax(), out);
1415}
1416
1417fn emit_query(d: &ast::QueryDirective, out: &mut String) {
1418 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1419 let name = d.name().map(|s| s.text().to_string()).unwrap_or_default();
1420 let query = d.query().map(|s| s.text().to_string()).unwrap_or_default();
1421 out.push_str(&date);
1422 out.push_str(" query ");
1423 out.push_str(&name);
1424 out.push(' ');
1425 out.push_str(&query);
1426 out.push('\n');
1427 emit_meta_entries_of(d.syntax(), out);
1428}
1429
1430fn emit_pad(d: &ast::PadDirective, out: &mut String) {
1431 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1432 let target = d
1433 .target_account()
1434 .map(|t| t.text().to_string())
1435 .unwrap_or_default();
1436 let source = d
1437 .source_account()
1438 .map(|t| t.text().to_string())
1439 .unwrap_or_default();
1440 out.push_str(&date);
1441 out.push_str(" pad ");
1442 out.push_str(&target);
1443 out.push(' ');
1444 out.push_str(&source);
1445 out.push('\n');
1446 emit_meta_entries_of(d.syntax(), out);
1447}
1448
1449fn emit_document(d: &ast::DocumentDirective, out: &mut String) {
1450 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1451 let account = d
1452 .account()
1453 .map(|t| t.text().to_string())
1454 .unwrap_or_default();
1455 let path = d.path().map(|s| s.text().to_string()).unwrap_or_default();
1456 out.push_str(&date);
1457 out.push_str(" document ");
1458 out.push_str(&account);
1459 out.push(' ');
1460 out.push_str(&path);
1461 // Trailing TAG / LINK tokens — typed AST has no accessor, so
1462 // walk direct-child tokens. Skip LEADING trivia (a blank line
1463 // before a non-first directive attaches its NEWLINE inside the
1464 // node) and stop at the first NEWLINE *after* the header content
1465 // begins; otherwise the tags/links are dropped when reformatting
1466 // any document past the first — the same bug as #1321 in the
1467 // transaction path.
1468 let mut seen_content = false;
1469 for el in d.syntax().children_with_tokens() {
1470 let rowan::NodeOrToken::Token(t) = el else {
1471 break;
1472 };
1473 match t.kind() {
1474 crate::SyntaxKind::TAG | crate::SyntaxKind::LINK => {
1475 out.push(' ');
1476 out.push_str(t.text());
1477 seen_content = true;
1478 }
1479 crate::SyntaxKind::NEWLINE if seen_content => break,
1480 // Leading trivia before the date: whitespace, blank-line
1481 // NEWLINEs, AND comment lines. A comment before a non-first
1482 // directive attaches inside this node (Directive-Terminator
1483 // Rule); skipping only WHITESPACE/NEWLINE would let it flip
1484 // `seen_content`, break at the comment's NEWLINE, and drop
1485 // the real header tags/links.
1486 k if k.is_trivia() => {}
1487 _ => seen_content = true,
1488 }
1489 }
1490 out.push('\n');
1491 emit_meta_entries_of(d.syntax(), out);
1492}
1493
1494fn emit_price(d: &ast::PriceDirective, out: &mut String) {
1495 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1496 let base = d
1497 .base_currency()
1498 .map(|t| t.text().to_string())
1499 .unwrap_or_default();
1500 let quote = d
1501 .quote_currency()
1502 .map(|t| t.text().to_string())
1503 .unwrap_or_default();
1504 out.push_str(&date);
1505 out.push_str(" price ");
1506 out.push_str(&base);
1507 out.push(' ');
1508 emit_amount_expression(d.syntax(), out);
1509 out.push(' ');
1510 out.push_str("e);
1511 out.push('\n');
1512 emit_meta_entries_of(d.syntax(), out);
1513}
1514
1515fn emit_balance(d: &ast::BalanceDirective, out: &mut String) {
1516 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1517 let account = d
1518 .account()
1519 .map(|t| t.text().to_string())
1520 .unwrap_or_default();
1521 let currency = d
1522 .currency()
1523 .map(|t| t.text().to_string())
1524 .unwrap_or_default();
1525 out.push_str(&date);
1526 out.push_str(" balance ");
1527 out.push_str(&account);
1528 out.push(' ');
1529 emit_amount_expression(d.syntax(), out);
1530 out.push(' ');
1531 out.push_str(¤cy);
1532 // Optional `~ tolerance [CCY]` — walk raw tokens.
1533 if let Some((tolerance, tol_currency)) = balance_tolerance(d.syntax()) {
1534 out.push_str(" ~ ");
1535 out.push_str(&tolerance);
1536 if let Some(c) = tol_currency {
1537 out.push(' ');
1538 out.push_str(&c);
1539 }
1540 }
1541 out.push('\n');
1542 emit_meta_entries_of(d.syntax(), out);
1543}
1544
1545fn emit_custom(d: &ast::CustomDirective, out: &mut String) {
1546 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1547 let custom_type = d
1548 .custom_type()
1549 .map(|s| s.text().to_string())
1550 .unwrap_or_default();
1551 out.push_str(&date);
1552 out.push_str(" custom ");
1553 out.push_str(&custom_type);
1554 // Walk raw tokens after the type STRING and emit each value
1555 // with single-space separation. NUMBER + CURRENCY adjacent
1556 // counts as an Amount; emitted together with one space.
1557 let tokens: Vec<crate::SyntaxToken> = d
1558 .syntax()
1559 .children_with_tokens()
1560 .filter_map(rowan::NodeOrToken::into_token)
1561 .filter(|t| !is_trivia_kind(t.kind()))
1562 .collect();
1563 // `seen_type` skips the leading DATE + CUSTOM_KW + type-STRING
1564 // tokens (already emitted above as the directive header); once
1565 // it flips true, every subsequent non-trivia token is a value
1566 // argument and gets emitted with single-space separation. An
1567 // adjacent NUMBER + CURRENCY pair is glued with a single space
1568 // (canonical Amount shape); the CURRENCY is NOT eaten as a
1569 // standalone arg next iteration.
1570 //
1571 // Beancount custom directives accept any mix of value kinds
1572 // including DATE — a `custom "type" 2024-06-15 100.00 USD`
1573 // shape has a DATE in value position. The previous version
1574 // skipped every DATE after seen_type, silently dropping such
1575 // user-provided date arguments.
1576 let mut seen_type = false;
1577 let mut i = 0;
1578 while i < tokens.len() {
1579 let t = &tokens[i];
1580 if !seen_type {
1581 if t.kind() == crate::SyntaxKind::STRING {
1582 seen_type = true;
1583 }
1584 i += 1;
1585 continue;
1586 }
1587 out.push(' ');
1588 if t.kind() == crate::SyntaxKind::NUMBER {
1589 out.push_str(&canonical_number(t.text()));
1590 if matches!(
1591 tokens.get(i + 1).map(rowan::SyntaxToken::kind),
1592 Some(crate::SyntaxKind::CURRENCY)
1593 ) {
1594 out.push(' ');
1595 out.push_str(tokens[i + 1].text());
1596 i += 2;
1597 continue;
1598 }
1599 } else {
1600 out.push_str(t.text());
1601 }
1602 i += 1;
1603 }
1604 out.push('\n');
1605 emit_meta_entries_of(d.syntax(), out);
1606}
1607
1608// ---- Top-level non-dated directives -----------------------------
1609
1610fn emit_option(d: &ast::OptionDirective, out: &mut String) {
1611 let key = d.key().map(|s| s.text().to_string()).unwrap_or_default();
1612 let value = d.value().map(|s| s.text().to_string()).unwrap_or_default();
1613 out.push_str("option ");
1614 out.push_str(&key);
1615 out.push(' ');
1616 out.push_str(&value);
1617 out.push('\n');
1618}
1619
1620fn emit_include(d: &ast::IncludeDirective, out: &mut String) {
1621 let path = d.path().map(|s| s.text().to_string()).unwrap_or_default();
1622 out.push_str("include ");
1623 out.push_str(&path);
1624 out.push('\n');
1625}
1626
1627fn emit_plugin(d: &ast::PluginDirective, out: &mut String) {
1628 let module = d.module().map(|s| s.text().to_string()).unwrap_or_default();
1629 out.push_str("plugin ");
1630 out.push_str(&module);
1631 if let Some(config) = d.config() {
1632 out.push(' ');
1633 out.push_str(config.text());
1634 }
1635 out.push('\n');
1636}
1637
1638// ---- State directives (no metadata) -----------------------------
1639
1640fn emit_pushtag(d: &ast::PushtagDirective, out: &mut String) {
1641 let tag = d.tag().map(|t| t.text().to_string()).unwrap_or_default();
1642 out.push_str("pushtag ");
1643 out.push_str(&tag);
1644 out.push('\n');
1645}
1646
1647fn emit_poptag(d: &ast::PoptagDirective, out: &mut String) {
1648 let tag = d.tag().map(|t| t.text().to_string()).unwrap_or_default();
1649 out.push_str("poptag ");
1650 out.push_str(&tag);
1651 out.push('\n');
1652}
1653
1654fn emit_pushmeta(d: &ast::PushmetaDirective, out: &mut String) {
1655 let key = d.key().map(|t| t.text().to_string()).unwrap_or_default();
1656 out.push_str("pushmeta ");
1657 out.push_str(&key);
1658 // Walk the value tokens after META_KEY, single-space separated.
1659 let mut past_key = false;
1660 for el in d.syntax().children_with_tokens() {
1661 let rowan::NodeOrToken::Token(t) = el else {
1662 continue;
1663 };
1664 if !past_key {
1665 if t.kind() == crate::SyntaxKind::META_KEY {
1666 past_key = true;
1667 }
1668 continue;
1669 }
1670 if is_trivia_kind(t.kind()) {
1671 continue;
1672 }
1673 out.push(' ');
1674 if t.kind() == crate::SyntaxKind::NUMBER {
1675 out.push_str(&canonical_number(t.text()));
1676 } else {
1677 out.push_str(t.text());
1678 }
1679 }
1680 out.push('\n');
1681}
1682
1683fn emit_popmeta(d: &ast::PopmetaDirective, out: &mut String) {
1684 let key = d.key().map(|t| t.text().to_string()).unwrap_or_default();
1685 out.push_str("popmeta ");
1686 out.push_str(&key);
1687 out.push('\n');
1688}
1689
1690// ---- Transaction + Posting --------------------------------------
1691
1692fn emit_transaction(d: &ast::Transaction, align: PostingAlignment, out: &mut String) {
1693 let date = d.date().map(|t| t.text().to_string()).unwrap_or_default();
1694 out.push_str(&date);
1695 out.push(' ');
1696 out.push_str(&transaction_flag_string(d));
1697 if let Some(payee) = d.payee() {
1698 out.push(' ');
1699 out.push_str(payee.text());
1700 }
1701 if let Some(narration) = d.narration() {
1702 out.push(' ');
1703 out.push_str(narration.text());
1704 }
1705 // Header-region tags/links — emitted in source order
1706 // (typed `.tags()` / `.links()` accessors return each kind
1707 // grouped, which loses interleaving like `#a ^l #b`). Walk
1708 // direct-child tokens, stopping at the header-terminating
1709 // NEWLINE.
1710 //
1711 // `seen_content` guards against LEADING trivia: for any directive
1712 // after the first, the preceding blank line's NEWLINE attaches
1713 // inside this node before the date (the Directive-Terminator Rule).
1714 // The header terminator is the first NEWLINE *after* the date, not
1715 // a leading one — otherwise this loop would break immediately and
1716 // emit no header tags (#1321).
1717 let mut seen_content = false;
1718 for el in d.syntax().children_with_tokens() {
1719 let rowan::NodeOrToken::Token(t) = el else {
1720 break;
1721 };
1722 match t.kind() {
1723 crate::SyntaxKind::TAG | crate::SyntaxKind::LINK => {
1724 out.push(' ');
1725 out.push_str(t.text());
1726 seen_content = true;
1727 }
1728 crate::SyntaxKind::NEWLINE if seen_content => break,
1729 // Leading trivia before the date: whitespace, blank-line
1730 // NEWLINEs, AND comment lines (a comment before a non-first
1731 // directive attaches inside this node per the Directive-
1732 // Terminator Rule). Skipping only WHITESPACE/NEWLINE would
1733 // let a leading comment flip `seen_content`, break at the
1734 // comment's NEWLINE, and drop the real header tags/links.
1735 k if k.is_trivia() => {}
1736 // DATE / flag / STRING etc. — header content has begun.
1737 _ => seen_content = true,
1738 }
1739 }
1740 out.push('\n');
1741 // Body: a single source-order walk over the transaction's children,
1742 // emitting — in the order they appear — POSTING / META_ENTRY nodes, any
1743 // body-internal COMMENT lines (#1332: the formatter must not delete the
1744 // author's comments), and trailing body-line TAG / LINK continuation
1745 // tokens (valid Beancount per the body-line exemption).
1746 //
1747 // `seen_content` / `past_header` skip the header region exactly as the
1748 // header loop above does, so the header-trailing comment (spliced onto
1749 // the header line by `emit_directive`) and the header tags/links (already
1750 // emitted inline above) are not duplicated here. A leading blank-line
1751 // NEWLINE for any directive past the first is trivia and must not flip
1752 // `past_header` early (#1321).
1753 let mut past_header = false;
1754 let mut seen_content = false;
1755 for el in d.syntax().children_with_tokens() {
1756 match el {
1757 rowan::NodeOrToken::Node(n) => {
1758 // A POSTING / META_ENTRY node is definitively past the header.
1759 past_header = true;
1760 if let Some(p) = ast::Posting::cast(n.clone()) {
1761 emit_posting(&p, align, out);
1762 } else if let Some(m) = ast::MetaEntry::cast(n) {
1763 emit_meta_entry(&m, INDENT, out);
1764 }
1765 }
1766 rowan::NodeOrToken::Token(t) => {
1767 if !past_header {
1768 match t.kind() {
1769 crate::SyntaxKind::NEWLINE if seen_content => past_header = true,
1770 k if k.is_trivia() => {}
1771 // DATE / flag / STRING / header TAG / LINK: still header.
1772 _ => seen_content = true,
1773 }
1774 continue;
1775 }
1776 // Body tokens: preserve comment-only lines and emit
1777 // continuation tags/links, each on its own indented line.
1778 match t.kind() {
1779 crate::SyntaxKind::COMMENT | crate::SyntaxKind::PERCENT_COMMENT => {
1780 out.push_str(INDENT);
1781 out.push_str(t.text().trim_end_matches(['\n', '\r']));
1782 out.push('\n');
1783 }
1784 crate::SyntaxKind::TAG | crate::SyntaxKind::LINK => {
1785 out.push_str(INDENT);
1786 out.push_str(t.text());
1787 out.push('\n');
1788 }
1789 _ => {}
1790 }
1791 }
1792 }
1793 }
1794}
1795
1796fn transaction_flag_string(d: &ast::Transaction) -> String {
1797 use crate::cst::ast::TransactionFlagKind;
1798 match d.flag() {
1799 None => "*".to_string(),
1800 Some(f) => match f.classify() {
1801 TransactionFlagKind::Star | TransactionFlagKind::Txn => "*".to_string(),
1802 TransactionFlagKind::Pending => "!".to_string(),
1803 TransactionFlagKind::Hash => "#".to_string(),
1804 TransactionFlagKind::Letter | TransactionFlagKind::CurrencyLetter => {
1805 f.text().to_string()
1806 }
1807 },
1808 }
1809}
1810
1811fn emit_posting(p: &ast::Posting, align: PostingAlignment, out: &mut String) {
1812 // Posting-trailing comment (same-line, before the posting-line
1813 // NEWLINE) — capture upfront so we can splice it back in just
1814 // before that NEWLINE, preserving the user's attachment intent.
1815 let trailing = collect_trailing_comment(p.syntax());
1816 let posting_start = out.len();
1817
1818 out.push_str(INDENT);
1819 let mut col = INDENT.len();
1820 if let Some(flag) = p.flag() {
1821 out.push_str(flag.text());
1822 out.push(' ');
1823 col += flag.text().chars().count() + 1;
1824 }
1825 let account_text = p
1826 .account()
1827 .map(|a| a.text().to_string())
1828 .unwrap_or_default();
1829 out.push_str(&account_text);
1830 col += account_text.chars().count();
1831
1832 if let Some(amt) = p.amount() {
1833 // `amount_number_text` is the shared "does this render a number?"
1834 // predicate (see `compute_alignment`); a currency-only amount
1835 // returns `None` and prints no number.
1836 if let Some(value) = amount_number_text(&amt) {
1837 // Two stages of padding:
1838 // 1) Account end → start of number field (`number_col`).
1839 // Fall back to 2 spaces when the LHS already exceeds
1840 // the file-wide max (over-long account name).
1841 // 2) Inside the number field, left-pad to right-justify
1842 // to `number_width`. Effect: the currency column
1843 // lands at a single uniform position file-wide even
1844 // when numbers have different widths or signs.
1845 let field_pad = align.number_col.saturating_sub(col).max(2);
1846 let justify_pad = align.number_width.saturating_sub(value.chars().count());
1847 for _ in 0..(field_pad + justify_pad) {
1848 out.push(' ');
1849 }
1850 out.push_str(&value);
1851 if let Some(c) = amt.currency() {
1852 out.push(' ');
1853 out.push_str(c.text());
1854 }
1855 if let Some(cs) = p.cost_spec() {
1856 out.push(' ');
1857 out.push_str(&format_cost_spec(&cs));
1858 }
1859 if let Some(pa) = p.price_annotation() {
1860 out.push(' ');
1861 out.push_str(&format_price_annotation(&pa));
1862 }
1863 }
1864 }
1865 out.push('\n');
1866 // Splice the trailing comment in BEFORE the posting-line
1867 // NEWLINE (the first '\n' in the emitted posting region).
1868 if let Some(c) = trailing
1869 && let Some(rel) = out[posting_start..].find('\n')
1870 {
1871 let mut splice = String::with_capacity(c.len() + 1);
1872 splice.push(' ');
1873 splice.push_str(&c);
1874 out.insert_str(posting_start + rel, &splice);
1875 }
1876 // Posting body: emit attached metadata AND posting-internal comment
1877 // lines in source order, indented 4 (deeper than the posting's 2).
1878 // Comment-only lines inside a posting attach as COMMENT tokens of the
1879 // POSTING node; walking children-with-tokens preserves them (#1337)
1880 // instead of dropping them. The posting's own header line is skipped via
1881 // the seen_content/past_header guard, so the same-line trailing comment
1882 // (spliced above) is not duplicated here.
1883 let mut past_header = false;
1884 let mut seen_content = false;
1885 for el in p.syntax().children_with_tokens() {
1886 match el {
1887 rowan::NodeOrToken::Node(n) => {
1888 // Header child nodes (AMOUNT / COST_SPEC / PRICE_ANNOTATION)
1889 // are emitted inline above and must NOT flip `past_header` —
1890 // only the posting-line NEWLINE does. Otherwise the same-line
1891 // trailing comment, which follows the AMOUNT node, would be
1892 // re-emitted here as a body comment. META_ENTRY nodes only
1893 // appear in the body, after `past_header` is already set.
1894 if let Some(m) = ast::MetaEntry::cast(n) {
1895 emit_meta_entry(&m, " ", out);
1896 }
1897 }
1898 rowan::NodeOrToken::Token(t) => {
1899 if !past_header {
1900 match t.kind() {
1901 crate::SyntaxKind::NEWLINE if seen_content => past_header = true,
1902 k if k.is_trivia() => {}
1903 _ => seen_content = true,
1904 }
1905 continue;
1906 }
1907 if matches!(
1908 t.kind(),
1909 crate::SyntaxKind::COMMENT | crate::SyntaxKind::PERCENT_COMMENT
1910 ) {
1911 out.push_str(" ");
1912 out.push_str(t.text().trim_end_matches(['\n', '\r']));
1913 out.push('\n');
1914 }
1915 }
1916 }
1917 }
1918}
1919
1920/// Format an `AMOUNT` (units + currency) in canonical form. For
1921/// arithmetic shapes, emits the expression with single-space
1922/// separators (parens tight); for plain shapes, emits
1923/// `NUMBER CURRENCY` with thousands separators stripped.
1924fn format_amount(amt: &ast::Amount) -> String {
1925 let mut out = String::new();
1926 if amt.is_arithmetic() {
1927 emit_amount_subnode_expression(amt.syntax(), &mut out);
1928 if let Some(c) = amt.currency() {
1929 if !out.is_empty() {
1930 out.push(' ');
1931 }
1932 out.push_str(c.text());
1933 }
1934 return out;
1935 }
1936 if let Some(sign) = amt.sign()
1937 && sign.is_minus()
1938 {
1939 out.push('-');
1940 }
1941 if let Some(n) = amt.number() {
1942 out.push_str(&canonical_number(n.text()));
1943 }
1944 if let Some(c) = amt.currency() {
1945 if !out.is_empty() && !out.ends_with('-') {
1946 out.push(' ');
1947 }
1948 out.push_str(c.text());
1949 }
1950 out
1951}
1952
1953/// Canonical form for cost specs: `{cost CCY}` (single-brace
1954/// per-unit), `{{cost CCY}}` (double-brace total), `{# cost CCY}`
1955/// (per-unit + total via opener), or the in-brace `{N # T CCY}`
1956/// shape preserved as-is with single-space normalization.
1957///
1958/// Commas separating cost components (`{N CCY, DATE, "label"}`)
1959/// stay tight against the preceding token; every other adjacent
1960/// token pair is joined with a single space.
1961fn format_cost_spec(cs: &ast::CostSpec) -> String {
1962 let (open, close) = if cs.is_total() {
1963 ("{{", "}}")
1964 } else if cs.is_per_unit_plus_total() {
1965 ("{#", "}")
1966 } else {
1967 ("{", "}")
1968 };
1969 // Collect inner content tokens (skip opener/closer/whitespace),
1970 // then route through write_canonical_token_sequence so the spacing rule
1971 // is identical to balance/price/AMOUNT-subnode arithmetic — most
1972 // importantly, unary `+`/`-` stays tight (`{-500 USD}`, not
1973 // `{- 500 USD}`) and COMMA stays tight.
1974 let inner_tokens: Vec<crate::SyntaxToken> = cs
1975 .syntax()
1976 .children_with_tokens()
1977 .filter_map(rowan::NodeOrToken::into_token)
1978 .filter(|t| {
1979 !matches!(
1980 t.kind(),
1981 crate::SyntaxKind::L_BRACE
1982 | crate::SyntaxKind::R_BRACE
1983 | crate::SyntaxKind::L_DOUBLE_BRACE
1984 | crate::SyntaxKind::R_DOUBLE_BRACE
1985 | crate::SyntaxKind::L_BRACE_HASH
1986 | crate::SyntaxKind::WHITESPACE
1987 | crate::SyntaxKind::NEWLINE
1988 )
1989 })
1990 .collect();
1991 let mut inner = String::new();
1992 write_canonical_token_sequence(&inner_tokens, &mut inner);
1993 // The `{#` opener is a two-character marker; canonical form
1994 // separates it from the first inner token with a single space
1995 // (matching the rendering in this function's rustdoc). `{` and
1996 // `{{` don't get inner padding per the canonical-form spec.
1997 if cs.is_per_unit_plus_total() && !inner.is_empty() {
1998 format!("{open} {inner}{close}")
1999 } else {
2000 format!("{open}{inner}{close}")
2001 }
2002}
2003
2004/// Canonical price annotation: `@ amount` (per-unit) or
2005/// `@@ amount` (total).
2006fn format_price_annotation(pa: &ast::PriceAnnotation) -> String {
2007 let op = if pa.is_total() { "@@" } else { "@" };
2008 match pa.amount() {
2009 Some(a) => format!("{op} {}", format_amount(&a)),
2010 None => op.to_string(),
2011 }
2012}
2013
2014// ---- Helpers ---------------------------------------------------
2015
2016/// True for tokens that don't contribute content to the canonical
2017/// form: whitespace, newlines, every comment kind, and the
2018/// leading-file `BOM` token.
2019const fn is_trivia_kind(kind: crate::SyntaxKind) -> bool {
2020 matches!(
2021 kind,
2022 crate::SyntaxKind::WHITESPACE
2023 | crate::SyntaxKind::NEWLINE
2024 | crate::SyntaxKind::COMMENT
2025 | crate::SyntaxKind::PERCENT_COMMENT
2026 | crate::SyntaxKind::SHEBANG
2027 | crate::SyntaxKind::EMACS_DIRECTIVE
2028 | crate::SyntaxKind::BOM
2029 )
2030}
2031
2032/// Strip thousands-separator commas from a NUMBER token's text;
2033/// preserve the user's decimal-place count. Per the locked
2034/// canonical-form decision: `1,000.00` → `1000.00`, `1.0` → `1.0`.
2035fn canonical_number(text: &str) -> String {
2036 if text.contains(',') {
2037 text.replace(',', "")
2038 } else {
2039 text.to_string()
2040 }
2041}
2042
2043/// Emit the arithmetic expression of a `PRICE` / `BALANCE`
2044/// directive: tokens from the first expression-starting token
2045/// (`NUMBER`, unary `+`/`-`, or `(`) up to (but not including) the
2046/// first `CURRENCY` at paren-depth 0. Spacing rules per
2047/// [`write_canonical_token_sequence`].
2048///
2049/// **Why the predicate must allow `PLUS` / `MINUS` / `L_PAREN`,
2050/// not just `NUMBER`.** A previous version skipped tokens until
2051/// it hit a `NUMBER`, which silently dropped leading unary signs
2052/// and opening parens — flipping the sign on inputs like
2053/// `2024-01-15 price USD -1.00 EUR` (formatted to `1.00 EUR`) and
2054/// corrupting parenthesized expressions like
2055/// `2024-01-15 balance Assets:A (1 + 2) USD` (formatted to
2056/// `1 + 2) USD USD`). Sign drift in BALANCE / PRICE is silent data
2057/// corruption — a balance assertion that previously asserted a
2058/// debit would assert a credit after a round-trip.
2059fn emit_amount_expression(node: &crate::SyntaxNode, out: &mut String) {
2060 let raw: Vec<crate::SyntaxToken> = node
2061 .children_with_tokens()
2062 .filter_map(rowan::NodeOrToken::into_token)
2063 .filter(|t| !is_trivia_kind(t.kind()))
2064 .skip_while(|t| {
2065 !matches!(
2066 t.kind(),
2067 crate::SyntaxKind::NUMBER
2068 | crate::SyntaxKind::PLUS
2069 | crate::SyntaxKind::MINUS
2070 | crate::SyntaxKind::L_PAREN
2071 )
2072 })
2073 .collect();
2074 let mut depth: i32 = 0;
2075 let mut first_currency_idx: Option<usize> = None;
2076 for (i, t) in raw.iter().enumerate() {
2077 match t.kind() {
2078 crate::SyntaxKind::L_PAREN => depth += 1,
2079 crate::SyntaxKind::R_PAREN => depth -= 1,
2080 crate::SyntaxKind::CURRENCY if depth == 0 && first_currency_idx.is_none() => {
2081 first_currency_idx = Some(i);
2082 }
2083 _ => {}
2084 }
2085 }
2086 let end = first_currency_idx.unwrap_or(raw.len());
2087 write_canonical_token_sequence(&raw[..end], out);
2088}
2089
2090/// Emit an `AMOUNT` subnode's expression region: every non-trivia
2091/// token minus the trailing `CURRENCY` (caller re-emits the
2092/// currency itself). Used by [`format_amount`] for arithmetic
2093/// posting amounts like `-(1.00 + 2.00) USD`.
2094fn emit_amount_subnode_expression(node: &crate::SyntaxNode, out: &mut String) {
2095 let mut tokens: Vec<crate::SyntaxToken> = node
2096 .children_with_tokens()
2097 .filter_map(rowan::NodeOrToken::into_token)
2098 .filter(|t| !is_trivia_kind(t.kind()))
2099 .collect();
2100 if let Some(last) = tokens.last()
2101 && last.kind() == crate::SyntaxKind::CURRENCY
2102 {
2103 tokens.pop();
2104 }
2105 write_canonical_token_sequence(&tokens, out);
2106}
2107
2108/// Single dispatcher for the canonical spacing rules used by EVERY
2109/// token-sequence emit path: balance / price arithmetic, AMOUNT
2110/// subnodes, cost-spec interiors, and metadata values. There is no
2111/// separate path; each call site collects the relevant non-trivia
2112/// tokens and routes them through here so the rules cannot drift
2113/// between contexts.
2114///
2115/// Rules:
2116///
2117/// - single space between adjacent operands / binary operators
2118/// - no space after `(` or before `)` (parens stay tight)
2119/// - no space after a unary `+` / `-` (one that opens the run
2120/// or follows `(` or another operator)
2121/// - no space before `,` (commas in cost-spec component lists
2122/// stay tight against the preceding token)
2123///
2124/// **Adding a new `SyntaxKind` to the formatter implies thinking
2125/// about its effect on every call site of this function.** A new
2126/// operator-like kind added to `is_op` will silently change cost-
2127/// spec and metadata spacing too; a new bracket-like kind needs
2128/// its own rule. The corpus-level idempotence test
2129/// (`idempotence_corpus_sweep`) is the safety net that catches
2130/// drifts.
2131fn write_canonical_token_sequence(tokens: &[crate::SyntaxToken], out: &mut String) {
2132 let is_op = |k: crate::SyntaxKind| {
2133 matches!(
2134 k,
2135 crate::SyntaxKind::PLUS
2136 | crate::SyntaxKind::MINUS
2137 | crate::SyntaxKind::STAR
2138 | crate::SyntaxKind::SLASH
2139 )
2140 };
2141 let mut prev_kind: Option<crate::SyntaxKind> = None;
2142 let mut prev_was_unary = false;
2143 for t in tokens {
2144 let kind = t.kind();
2145 let is_unary = is_op(kind)
2146 && match prev_kind {
2147 None => true,
2148 Some(p) => p == crate::SyntaxKind::L_PAREN || is_op(p),
2149 };
2150 let need_space = match prev_kind {
2151 None => false,
2152 Some(prev) => {
2153 prev != crate::SyntaxKind::L_PAREN
2154 && kind != crate::SyntaxKind::R_PAREN
2155 && kind != crate::SyntaxKind::COMMA
2156 && !prev_was_unary
2157 }
2158 };
2159 if need_space {
2160 out.push(' ');
2161 }
2162 if kind == crate::SyntaxKind::NUMBER {
2163 out.push_str(&canonical_number(t.text()));
2164 } else {
2165 out.push_str(t.text());
2166 }
2167 prev_kind = Some(kind);
2168 prev_was_unary = is_unary;
2169 }
2170}
2171
2172/// Extract a balance directive's optional tolerance — the
2173/// `NUMBER` after the first `TILDE`, plus an optional trailing
2174/// `CURRENCY` at paren-depth 0.
2175fn balance_tolerance(node: &crate::SyntaxNode) -> Option<(String, Option<String>)> {
2176 let mut past_tilde = false;
2177 let mut number: Option<String> = None;
2178 let mut currency: Option<String> = None;
2179 for el in node.children_with_tokens() {
2180 let rowan::NodeOrToken::Token(t) = el else {
2181 continue;
2182 };
2183 if !past_tilde {
2184 if t.kind() == crate::SyntaxKind::TILDE {
2185 past_tilde = true;
2186 }
2187 continue;
2188 }
2189 match t.kind() {
2190 crate::SyntaxKind::NUMBER if number.is_none() => {
2191 number = Some(canonical_number(t.text()));
2192 }
2193 crate::SyntaxKind::CURRENCY if number.is_some() && currency.is_none() => {
2194 currency = Some(t.text().to_string());
2195 }
2196 _ => {}
2197 }
2198 }
2199 number.map(|n| (n, currency))
2200}
2201
2202// ---- Metadata --------------------------------------------------
2203
2204/// Walk a directive's direct-child `META_ENTRY` nodes and emit
2205/// each on its own indented line in canonical form (`indent + KEY:
2206/// value\n`). Most directive types don't have a `.meta_entries()`
2207/// accessor on their typed wrapper; we walk the syntax node
2208/// directly to stay uniform.
2209fn emit_meta_entries_of(node: &crate::SyntaxNode, out: &mut String) {
2210 // Source-order walk so body-internal COMMENT lines are preserved
2211 // alongside the metadata entries (#1332). The header region (up to and
2212 // including the header-terminating NEWLINE) is skipped so the
2213 // header-trailing comment — spliced onto the header line by
2214 // `emit_directive` — is not duplicated here.
2215 let mut past_header = false;
2216 let mut seen_content = false;
2217 for el in node.children_with_tokens() {
2218 match el {
2219 rowan::NodeOrToken::Node(n) => {
2220 past_header = true;
2221 if let Some(entry) = MetaEntry::cast(n) {
2222 emit_meta_entry(&entry, INDENT, out);
2223 }
2224 }
2225 rowan::NodeOrToken::Token(t) => {
2226 if !past_header {
2227 match t.kind() {
2228 crate::SyntaxKind::NEWLINE if seen_content => past_header = true,
2229 k if k.is_trivia() => {}
2230 _ => seen_content = true,
2231 }
2232 continue;
2233 }
2234 if matches!(
2235 t.kind(),
2236 crate::SyntaxKind::COMMENT | crate::SyntaxKind::PERCENT_COMMENT
2237 ) {
2238 out.push_str(INDENT);
2239 out.push_str(t.text().trim_end_matches(['\n', '\r']));
2240 out.push('\n');
2241 }
2242 }
2243 }
2244 }
2245}
2246
2247/// Canonical emit for a single `META_ENTRY`. Walks non-trivia
2248/// tokens, prints them with single-space separation, and
2249/// normalizes numbers via [`canonical_number`]. The `META_KEY`
2250/// token already includes the trailing colon (e.g. `note:`); the
2251/// value side gets the same NUMBER + CURRENCY gluing rule the
2252/// rest of the formatter uses elsewhere.
2253///
2254/// Two semantically-equivalent inputs (e.g. `foo: "bar"` and
2255/// `foo: "bar"`) produce byte-identical output — the
2256/// gofmt-style invariant the file rustdoc promises.
2257fn emit_meta_entry(m: &MetaEntry, indent: &str, out: &mut String) {
2258 out.push_str(indent);
2259 // Split the META_ENTRY's non-trivia tokens into [META_KEY,
2260 // value*]. The META_KEY token already includes the trailing
2261 // colon (e.g. `note:`); the value tokens go through
2262 // write_canonical_token_sequence so the spacing rules — unary +/-
2263 // tight, COMMA tight, paren-tight, NUMBER canonicalized — are
2264 // shared with the balance/price/cost-spec/posting-amount paths.
2265 let content: Vec<crate::SyntaxToken> = m
2266 .syntax()
2267 .children_with_tokens()
2268 .filter_map(rowan::NodeOrToken::into_token)
2269 .filter(|t| {
2270 !matches!(
2271 t.kind(),
2272 crate::SyntaxKind::WHITESPACE | crate::SyntaxKind::NEWLINE
2273 )
2274 })
2275 .collect();
2276 let mut iter = content.iter();
2277 if let Some(key) = iter.next() {
2278 out.push_str(key.text());
2279 }
2280 let value_tokens: Vec<crate::SyntaxToken> = iter.cloned().collect();
2281 if !value_tokens.is_empty() {
2282 out.push(' ');
2283 write_canonical_token_sequence(&value_tokens, out);
2284 }
2285 out.push('\n');
2286}
2287
2288#[cfg(test)]
2289mod tests {
2290 use super::*;
2291
2292 #[test]
2293 fn empty_input_yields_single_newline() {
2294 assert_eq!(format_source(""), "\n");
2295 }
2296
2297 #[test]
2298 fn open_directive_canonical() {
2299 let src = "2024-01-15 open Assets:Cash\n";
2300 assert_eq!(format_source(src), "2024-01-15 open Assets:Cash\n");
2301 }
2302
2303 #[test]
2304 fn open_with_currencies_and_booking_canonical() {
2305 let src = "2024-01-15 open Assets:Brokerage USD,EUR \"STRICT\"\n";
2306 assert_eq!(
2307 format_source(src),
2308 "2024-01-15 open Assets:Brokerage USD EUR \"STRICT\"\n"
2309 );
2310 }
2311
2312 #[test]
2313 fn close_directive_canonical() {
2314 let src = "2024-12-31 close Assets:Cash\n";
2315 assert_eq!(format_source(src), "2024-12-31 close Assets:Cash\n");
2316 }
2317
2318 #[test]
2319 fn commodity_directive_canonical() {
2320 let src = "2024-01-01 commodity HOOL\n";
2321 assert_eq!(format_source(src), "2024-01-01 commodity HOOL\n");
2322 }
2323
2324 #[test]
2325 fn blank_lines_between_directives_preserved() {
2326 // #1325: the formatter preserves the author's inter-directive
2327 // blank lines rather than normalizing to exactly one (matching
2328 // Python bean-format and the rest of the beancount lineage).
2329
2330 // Grouped (no blank in source) stays grouped — not double-spaced.
2331 let grouped = "2024-01-01 open Assets:A\n2024-01-02 open Assets:B\n";
2332 assert_eq!(format_source(grouped), grouped);
2333
2334 // One blank is preserved as one.
2335 let one = "2024-01-01 open Assets:A\n\n2024-01-02 open Assets:B\n";
2336 assert_eq!(format_source(one), one);
2337
2338 // Two blanks are preserved as two (not collapsed).
2339 let two = "2024-01-01 open Assets:A\n\n\n2024-01-02 open Assets:B\n";
2340 assert_eq!(format_source(two), two);
2341
2342 // A whitespace-only "blank" line still counts as one blank line
2343 // (its trailing whitespace is stripped, leaving an empty line).
2344 let ws_blank = "2024-01-01 open Assets:A\n \n2024-01-02 open Assets:B\n";
2345 assert_eq!(
2346 format_source(ws_blank),
2347 "2024-01-01 open Assets:A\n\n2024-01-02 open Assets:B\n"
2348 );
2349 }
2350
2351 #[test]
2352 fn trailing_newline_always_present() {
2353 let src = "2024-01-01 open Assets:A";
2354 let formatted = format_source(src);
2355 assert!(formatted.ends_with('\n'));
2356 assert!(!formatted.ends_with("\n\n"));
2357 }
2358
2359 #[test]
2360 fn idempotent_on_canonical_input() {
2361 let src = "2024-01-01 open Assets:A\n\n2024-01-02 close Assets:A\n";
2362 let once = format_source(src);
2363 let twice = format_source(&once);
2364 assert_eq!(once, twice);
2365 }
2366
2367 #[test]
2368 fn note_canonical() {
2369 let src = "2024-01-15 note Assets:Cash \"a note\"\n";
2370 assert_eq!(
2371 format_source(src),
2372 "2024-01-15 note Assets:Cash \"a note\"\n"
2373 );
2374 }
2375
2376 #[test]
2377 fn event_canonical() {
2378 let src = "2024-01-15 event \"location\" \"NYC\"\n";
2379 assert_eq!(
2380 format_source(src),
2381 "2024-01-15 event \"location\" \"NYC\"\n"
2382 );
2383 }
2384
2385 #[test]
2386 fn query_canonical() {
2387 let src = "2024-01-15 query \"q1\" \"SELECT account\"\n";
2388 assert_eq!(
2389 format_source(src),
2390 "2024-01-15 query \"q1\" \"SELECT account\"\n"
2391 );
2392 }
2393
2394 #[test]
2395 fn pad_canonical() {
2396 let src = "2024-01-15 pad Assets:A Equity:Opening\n";
2397 assert_eq!(
2398 format_source(src),
2399 "2024-01-15 pad Assets:A Equity:Opening\n"
2400 );
2401 }
2402
2403 #[test]
2404 fn document_with_tags_and_links_canonical() {
2405 let src = "2024-06-01 document Assets:Bank \"stmt.pdf\" #q1 ^scan42 #urgent\n";
2406 assert_eq!(
2407 format_source(src),
2408 "2024-06-01 document Assets:Bank \"stmt.pdf\" #q1 ^scan42 #urgent\n"
2409 );
2410 }
2411
2412 #[test]
2413 fn issue_1321_document_tags_links_idempotent_across_directives() {
2414 // Same class as the transaction case, in `document` directives:
2415 // the 2nd+ document's trailing tags/links were dropped on a
2416 // reformat (found by the #1323 corpus idempotence check). Assert
2417 // the fixed-point property: re-formatting must not change (and
2418 // must not drop the tags/links of the second document).
2419 let src = "\
24202013-05-18 document Assets:Bank \"/a.pdf\" #tag1 ^link1
24212013-05-19 document Assets:Bank \"/b.pdf\" #tag2 ^link2
2422";
2423 let once = format_source(src);
2424 assert_eq!(format_source(&once), once, "format must be idempotent");
2425 assert!(
2426 once.contains("#tag2") && once.contains("^link2"),
2427 "the second document's tags/links must survive formatting; got:\n{once}"
2428 );
2429 }
2430
2431 #[test]
2432 fn issue_1321_header_tags_links_idempotent_across_transactions() {
2433 // Header tags/links must stay on the header line for EVERY
2434 // transaction, not just the first. Regression for #1321 where
2435 // the 2nd+ transaction's header tags/links got migrated to
2436 // continuation lines.
2437 let src = "\
24382024-01-15 * \"x\" #tag1 ^link1 #tag2 ^link2
2439 Assets:Cash -1.00 USD
2440 Expenses:Misc 1.00 USD
2441
24422024-01-16 * \"x\" #tag1 ^link1 #tag2 ^link2
2443 Assets:Cash -1.00 USD
2444 Expenses:Misc 1.00 USD
2445";
2446 assert_eq!(
2447 format_source(src),
2448 src,
2449 "format must be a no-op (idempotent)"
2450 );
2451 }
2452
2453 #[test]
2454 fn issue_1321_comment_before_transaction_keeps_header_tags() {
2455 // A comment line before a transaction is leading trivia attached
2456 // inside the transaction node (Directive-Terminator Rule), exactly
2457 // like a blank line. Skipping only WHITESPACE/NEWLINE let the
2458 // comment flip `seen_content`, break at the comment's NEWLINE, and
2459 // migrate the real header tags/links to continuation lines. The
2460 // header tags/links must stay on the header line. (Found by the
2461 // Copilot review of the #1321 fix.)
2462 let src = "\
24632024-01-15 * \"first\" #h1 ^l1
2464 Assets:Cash -1.00 USD
2465 Expenses:Misc 1.00 USD
2466
2467; a comment before the second transaction
24682024-01-16 * \"second\" #tag1 ^link1
2469 Assets:Cash -2.00 USD
2470 Expenses:Misc 2.00 USD
2471";
2472 assert_eq!(
2473 format_source(src),
2474 src,
2475 "a leading comment must not migrate header tags/links to continuation lines"
2476 );
2477 }
2478
2479 #[test]
2480 fn issue_1321_comment_before_document_keeps_tags() {
2481 // Document-directive variant of the comment-trivia case above.
2482 let src = "\
24832013-05-18 document Assets:Bank \"/a.pdf\" #tag1 ^link1
2484; a comment before the second document
24852013-05-19 document Assets:Bank \"/b.pdf\" #tag2 ^link2
2486";
2487 let once = format_source(src);
2488 assert_eq!(format_source(&once), once, "format must be idempotent");
2489 assert!(
2490 once.contains("\"/b.pdf\" #tag2 ^link2"),
2491 "the second document's tags/links must stay on its header line; got:\n{once}"
2492 );
2493 }
2494
2495 #[test]
2496 fn issue_1332_body_comments_in_metadata_preserved() {
2497 // The formatter must NOT delete comment-only lines inside a
2498 // directive body (#1332). Here two commented-out `; price:` lines
2499 // sit between metadata entries in a `commodity` body; they must
2500 // survive, interleaved in source order, and the result is idempotent.
2501 let src = "\
25022023-06-04 commodity EAM-VEUR ; cSpell: word VEUR
2503 name: \"Vanguard FTSE Developed Europe UCITS ETF EUR Dist\"
2504 ; price: \"EUR:alphavantage/price:VEUR.AS:EUR\"
2505 ; price: \"EUR:yahoo/VEUR.AS\"
2506 price: \"EUR:pricehist.beanprice.yahoo/VEUR.AS\"
2507";
2508 assert_eq!(
2509 format_source(src),
2510 src,
2511 "body comments must be preserved verbatim"
2512 );
2513 assert_eq!(format_source(&format_source(src)), format_source(src));
2514 }
2515
2516 #[test]
2517 fn issue_1332_body_comments_between_postings_preserved() {
2518 // Same class, inside a transaction body: a comment-only line between
2519 // postings must survive (in source order, 2-space indent). Asserted
2520 // via preservation + idempotence rather than an exact match, since
2521 // amount alignment is also canonicalized.
2522 let src = "\
25232024-01-15 * \"Cafe\" \"Latte\"
2524 Expenses:Coffee 4.50 USD
2525 ; was 5.00 before the discount
2526 Assets:Checking
2527";
2528 let out = format_source(src);
2529 assert!(
2530 out.contains("\n ; was 5.00 before the discount\n"),
2531 "the body comment must be preserved on its own indented line; got:\n{out}"
2532 );
2533 // Order: the comment stays between the two postings.
2534 let coffee = out.find("Expenses:Coffee").unwrap();
2535 let comment = out.find("; was 5.00").unwrap();
2536 let checking = out.find("Assets:Checking").unwrap();
2537 assert!(
2538 coffee < comment && comment < checking,
2539 "comment must stay between postings:\n{out}"
2540 );
2541 assert_eq!(format_source(&out), out, "format must be idempotent");
2542 }
2543
2544 #[test]
2545 fn issue_1335_org_headers_and_grouped_comments_preserved() {
2546 // The formatter must not delete unparsable content (#1335).
2547 // Org-mode `*` section headers parse into ERROR_NODEs, and comments
2548 // grouped with them get swallowed into the same node — previously all
2549 // dropped. They must survive, and the result must be idempotent.
2550 let src = "\
2551* Section A
2552;; comment between headers
2553;; second line
2554* Section B
25552013-01-01 open Assets:X
2556";
2557 let out = format_source(src);
2558 // Use the exact `;;` needles: a single-`;` substring would still match
2559 // `;; ...` even if one `;` were dropped, weakening the regression.
2560 for needle in [
2561 "* Section A",
2562 ";; comment between headers",
2563 ";; second line",
2564 "* Section B",
2565 "2013-01-01 open Assets:X",
2566 ] {
2567 assert!(
2568 out.contains(needle),
2569 "lost {needle:?} on format; got:\n{out}"
2570 );
2571 }
2572 assert_eq!(format_source(&out), out, "format must be idempotent");
2573 }
2574
2575 #[test]
2576 fn issue_1335_org_header_then_directive_keeps_header() {
2577 // A lone org header before a directive: the header is an ERROR_NODE
2578 // and must be kept (the comment here attaches to the directive and
2579 // was already preserved).
2580 let src = "* Accounts\n2013-01-01 open Assets:X\n";
2581 let out = format_source(src);
2582 assert!(
2583 out.contains("* Accounts"),
2584 "org header dropped; got:\n{out}"
2585 );
2586 assert_eq!(format_source(&out), out);
2587 }
2588
2589 #[test]
2590 fn issue_1335_blank_lines_around_org_header_preserved() {
2591 // An ERROR_NODE is a top-level content block: the author's blank line
2592 // between an org header and the following directive is preserved (it
2593 // is not flushed), and the result is idempotent.
2594 let src = "* Accounts\n\n2013-01-01 open Assets:X\n";
2595 assert_eq!(
2596 format_source(src),
2597 src,
2598 "blank around org header must be kept"
2599 );
2600 assert_eq!(format_source(&format_source(src)), format_source(src));
2601 }
2602
2603 #[test]
2604 fn issue_1337_posting_internal_comments_preserved() {
2605 // A comment on its own line inside a posting attaches as a COMMENT
2606 // token of the POSTING node; it must be preserved (#1337), not
2607 // dropped, and stay between its posting and the next.
2608 let src = "\
26092024-01-15 * \"x\"
2610 Assets:A 1.00 USD
2611 ; posting-internal note
2612 Assets:B
2613";
2614 let out = format_source(src);
2615 assert!(
2616 out.contains("; posting-internal note"),
2617 "posting-internal comment dropped; got:\n{out}"
2618 );
2619 let a = out.find("Assets:A").unwrap();
2620 let c = out.find("; posting-internal note").unwrap();
2621 let b = out.find("Assets:B").unwrap();
2622 assert!(a < c && c < b, "comment must stay between postings:\n{out}");
2623 assert_eq!(format_source(&out), out, "format must be idempotent");
2624 }
2625
2626 #[test]
2627 fn price_canonical_strips_thousands_separators() {
2628 let src = "2024-01-15 price USD 1,234.56 EUR\n";
2629 assert_eq!(format_source(src), "2024-01-15 price USD 1234.56 EUR\n");
2630 }
2631
2632 #[test]
2633 fn price_arithmetic_canonicalizes_spacing() {
2634 let src = "2024-01-15 price USD 1/2 EUR\n";
2635 assert_eq!(format_source(src), "2024-01-15 price USD 1 / 2 EUR\n");
2636 }
2637
2638 #[test]
2639 fn balance_canonical() {
2640 let src = "2024-01-15 balance Assets:Cash 100.00 USD\n";
2641 assert_eq!(
2642 format_source(src),
2643 "2024-01-15 balance Assets:Cash 100.00 USD\n"
2644 );
2645 }
2646
2647 #[test]
2648 fn balance_with_tolerance_canonical() {
2649 let src = "2024-01-15 balance Assets:Cash 100.00 USD ~ 0.01 USD\n";
2650 assert_eq!(
2651 format_source(src),
2652 "2024-01-15 balance Assets:Cash 100.00 USD ~ 0.01 USD\n"
2653 );
2654 }
2655
2656 #[test]
2657 fn balance_arithmetic_canonical() {
2658 let src = "2024-01-15 balance Assets:Cash 0.25 + 0.75 USD\n";
2659 assert_eq!(
2660 format_source(src),
2661 "2024-01-15 balance Assets:Cash 0.25 + 0.75 USD\n"
2662 );
2663 }
2664
2665 #[test]
2666 fn custom_canonical() {
2667 let src = "2024-01-01 custom \"budget\" Expenses:Food 500.00 USD\n";
2668 assert_eq!(
2669 format_source(src),
2670 "2024-01-01 custom \"budget\" Expenses:Food 500.00 USD\n"
2671 );
2672 }
2673
2674 #[test]
2675 fn option_canonical() {
2676 let src = "option \"title\" \"My Ledger\"\n";
2677 assert_eq!(format_source(src), "option \"title\" \"My Ledger\"\n");
2678 }
2679
2680 #[test]
2681 fn include_canonical() {
2682 let src = "include \"other.beancount\"\n";
2683 assert_eq!(format_source(src), "include \"other.beancount\"\n");
2684 }
2685
2686 #[test]
2687 fn plugin_canonical_with_config() {
2688 let src = "plugin \"beancount.plugins.unrealized\" \"Unrealized\"\n";
2689 assert_eq!(
2690 format_source(src),
2691 "plugin \"beancount.plugins.unrealized\" \"Unrealized\"\n"
2692 );
2693 }
2694
2695 #[test]
2696 fn plugin_canonical_without_config() {
2697 let src = "plugin \"my.plugin\"\n";
2698 assert_eq!(format_source(src), "plugin \"my.plugin\"\n");
2699 }
2700
2701 #[test]
2702 fn pushtag_poptag_canonical() {
2703 // No blank line in the source — preserved as grouped (#1325).
2704 let src = "pushtag #active\npoptag #active\n";
2705 assert_eq!(format_source(src), "pushtag #active\npoptag #active\n");
2706 }
2707
2708 #[test]
2709 fn pushmeta_popmeta_canonical() {
2710 // No blank line in the source — preserved as grouped (#1325).
2711 let src = "pushmeta location: \"NYC\"\npopmeta location:\n";
2712 assert_eq!(
2713 format_source(src),
2714 "pushmeta location: \"NYC\"\npopmeta location:\n"
2715 );
2716 }
2717
2718 // ---- Transaction tests ------------------------------------
2719
2720 #[test]
2721 fn transaction_minimal_two_postings_aligns_amounts() {
2722 let src = "\
27232024-01-15 * \"Coffee\"
2724 Assets:Cash -5.00 USD
2725 Expenses:Coffee 5.00 USD
2726";
2727 // max LHS = 15 (Expenses:Coffee); number_col = 17.
2728 // max number width = 6 (`-5.00`); number_width = 6.
2729 // Posting 1: account end at col 13, pad 4 → `-5.00` (width 6,
2730 // no left-pad) → currency at col 24.
2731 // Posting 2: account end at col 17, pad 2 → ` 5.00` (width
2732 // 5 left-padded by 1) → currency at col 24.
2733 let expected = "\
27342024-01-15 * \"Coffee\"
2735 Assets:Cash -5.00 USD
2736 Expenses:Coffee 5.00 USD
2737";
2738 assert_eq!(format_source(src), expected);
2739 }
2740
2741 /// Regression for #1290: an amount-less posting (the common elided
2742 /// balancing leg) must NOT widen the number column, even when its
2743 /// account is longer than every amount-bearing account. `bean-format`
2744 /// computes the column only from number-bearing lines, so counting
2745 /// `Expenses:Food` here would make `rledger format` and `bean-format`
2746 /// disagree and never converge on round-trip.
2747 #[test]
2748 fn transaction_elided_posting_does_not_widen_amount_column() {
2749 let src = "\
27502024-01-15 * \"Coffee\"
2751 Assets:Cash -5.00 USD
2752 Expenses:Food
2753";
2754 // Only Assets:Cash (11) bears an amount; Expenses:Food (13) is
2755 // elided and is ignored for alignment. number_col = 2+11+2 = 15.
2756 let expected = "\
27572024-01-15 * \"Coffee\"
2758 Assets:Cash -5.00 USD
2759 Expenses:Food
2760";
2761 assert_eq!(format_source(src), expected);
2762 // Idempotent: re-formatting the output is a no-op.
2763 assert_eq!(format_source(expected), expected);
2764 }
2765
2766 /// Regression for #1290 using the reporter's exact fixture: a long
2767 /// elided account (`Expenses:Thingamabobs`) alongside a short
2768 /// amount-bearing one (`Assets:Money`). Pre-fix the number was
2769 /// pushed right to clear the long account; `bean-format` keeps it
2770 /// two spaces after `Assets:Money`. Also confirms the thousands
2771 /// separator is stripped.
2772 #[test]
2773 fn transaction_long_elided_account_matches_bean_format() {
2774 let src = "\
27752024-07-20 * \"Commas should stay\"
2776 Assets:Money -1,024 USD
2777 Expenses:Thingamabobs
2778";
2779 let expected = "\
27802024-07-20 * \"Commas should stay\"
2781 Assets:Money -1024 USD
2782 Expenses:Thingamabobs
2783";
2784 assert_eq!(format_source(src), expected);
2785 assert_eq!(format_source(expected), expected);
2786 }
2787
2788 /// Regression for the currency-only gap (#1307, found in review): a
2789 /// currency-only posting (`... USD`, no number) renders no number,
2790 /// so — like an elided posting — it must not widen the alignment
2791 /// column even when its account is the longest. Only `Assets:Bank`
2792 /// bears a number here, so the number stays two spaces after it. The
2793 /// assertion checks the numbered line directly, independent of how
2794 /// the currency-only line itself renders.
2795 #[test]
2796 fn transaction_currency_only_posting_does_not_widen_amount_column() {
2797 let out = format_source(
2798 "2024-01-15 * \"x\"\n Assets:Bank -5.00 USD\n Assets:LongCashReserve USD\n",
2799 );
2800 assert!(
2801 out.contains(" Assets:Bank -5.00 USD"),
2802 "number column must align to the numbered posting, not the longer \
2803 currency-only one; got:\n{out}"
2804 );
2805 }
2806
2807 #[test]
2808 fn transaction_payee_and_narration() {
2809 let src =
2810 "2024-01-15 * \"Starbucks\" \"Coffee\"\n Assets:Cash -5.00 USD\n Expenses:Coffee\n";
2811 let out = format_source(src);
2812 assert!(
2813 out.contains("2024-01-15 * \"Starbucks\" \"Coffee\"\n"),
2814 "got: {out}"
2815 );
2816 }
2817
2818 #[test]
2819 fn transaction_pending_flag() {
2820 let src = "2024-01-15 ! \"Pending\"\n Assets:Cash -5.00 USD\n Expenses:Misc\n";
2821 let out = format_source(src);
2822 assert!(out.starts_with("2024-01-15 ! \"Pending\"\n"), "got: {out}");
2823 }
2824
2825 #[test]
2826 fn transaction_txn_keyword_normalized_to_star() {
2827 // The `txn` keyword form is canonical-form equivalent to `*`.
2828 let src = "2024-01-15 txn \"x\"\n Assets:Cash -1.00 USD\n Expenses:Misc\n";
2829 let out = format_source(src);
2830 assert!(out.starts_with("2024-01-15 * \"x\"\n"), "got: {out}");
2831 }
2832
2833 #[test]
2834 fn transaction_header_tags_and_links() {
2835 let src =
2836 "2024-01-15 * \"x\" #tag1 ^link1 #tag2\n Assets:Cash -1.00 USD\n Expenses:Misc\n";
2837 let out = format_source(src);
2838 assert!(
2839 out.starts_with("2024-01-15 * \"x\" #tag1 ^link1 #tag2\n"),
2840 "got: {out}"
2841 );
2842 }
2843
2844 #[test]
2845 fn transaction_auto_balance_posting_no_amount() {
2846 let src = "2024-01-15 * \"x\"\n Assets:Cash -5.00 USD\n Expenses:Misc\n";
2847 let out = format_source(src);
2848 // The auto-balance posting has no amount; should just be
2849 // the indented account name.
2850 assert!(out.contains("\n Expenses:Misc\n"), "got: {out}");
2851 }
2852
2853 #[test]
2854 fn transaction_posting_with_cost_spec() {
2855 let src = "2024-01-15 * \"buy\"\n Assets:Brokerage 10 HOOL {500.00 USD}\n Assets:Cash -5000.00 USD\n";
2856 let out = format_source(src);
2857 assert!(out.contains("10 HOOL {500.00 USD}"), "got: {out}");
2858 }
2859
2860 #[test]
2861 fn transaction_posting_with_total_cost_spec() {
2862 let src = "2024-01-15 * \"buy\"\n Assets:Brokerage 10 HOOL {{5000.00 USD}}\n Assets:Cash -5000.00 USD\n";
2863 let out = format_source(src);
2864 assert!(out.contains("10 HOOL {{5000.00 USD}}"), "got: {out}");
2865 }
2866
2867 #[test]
2868 fn transaction_posting_with_per_unit_price() {
2869 let src = "2024-01-15 * \"buy\"\n Assets:Brokerage 10 HOOL @ 500.00 USD\n Assets:Cash -5000.00 USD\n";
2870 let out = format_source(src);
2871 assert!(out.contains("10 HOOL @ 500.00 USD"), "got: {out}");
2872 }
2873
2874 #[test]
2875 fn transaction_posting_with_total_price() {
2876 let src = "2024-01-15 * \"buy\"\n Assets:Brokerage 10 HOOL @@ 5000.00 USD\n Assets:Cash -5000.00 USD\n";
2877 let out = format_source(src);
2878 assert!(out.contains("10 HOOL @@ 5000.00 USD"), "got: {out}");
2879 }
2880
2881 #[test]
2882 fn transaction_posting_with_flag() {
2883 let src = "2024-01-15 * \"x\"\n ! Assets:Cash -5.00 USD\n Expenses:Misc 5.00 USD\n";
2884 let out = format_source(src);
2885 assert!(out.contains("\n ! Assets:Cash"), "got: {out}");
2886 }
2887
2888 #[test]
2889 fn transaction_negative_amount() {
2890 let src = "2024-01-15 * \"x\"\n Assets:Cash -5.00 USD\n Expenses:Misc 5.00 USD\n";
2891 let out = format_source(src);
2892 assert!(out.contains("-5.00 USD"), "got: {out}");
2893 assert!(out.contains(" 5.00 USD"), "got: {out}");
2894 }
2895
2896 #[test]
2897 fn transaction_strips_thousands_separators_in_postings() {
2898 let src = "2024-01-15 * \"x\"\n Assets:Cash -1,000.00 USD\n Expenses:Misc 1,000.00 USD\n";
2899 let out = format_source(src);
2900 assert!(out.contains("-1000.00 USD"), "got: {out}");
2901 assert!(!out.contains("1,000"), "got: {out}");
2902 }
2903
2904 #[test]
2905 fn transaction_arithmetic_amount() {
2906 let src =
2907 "2024-01-15 * \"x\"\n Assets:Cash -(1.00 + 2.00) USD\n Expenses:Misc 3.00 USD\n";
2908 let out = format_source(src);
2909 // The arithmetic expression should render with single
2910 // spaces around binary ops and tight parens.
2911 assert!(
2912 out.contains("(1.00 + 2.00) USD") || out.contains("-(1.00 + 2.00) USD"),
2913 "got: {out}"
2914 );
2915 }
2916
2917 #[test]
2918 fn transaction_idempotent() {
2919 let src = "\
29202024-01-15 * \"Coffee\"
2921 Assets:Cash -5.00 USD
2922 Expenses:Coffee 5.00 USD
2923";
2924 let once = format_source(src);
2925 let twice = format_source(&once);
2926 assert_eq!(once, twice);
2927 }
2928
2929 #[test]
2930 fn transaction_file_wide_alignment_across_transactions() {
2931 let src = "\
29322024-01-15 * \"x\"
2933 Assets:Cash -5.00 USD
2934 Expenses:Misc 5.00 USD
2935
29362024-01-16 * \"y\"
2937 Liabilities:CreditCard:Visa -100.00 USD
2938 Expenses:Big 100.00 USD
2939";
2940 let out = format_source(src);
2941 // Cross-posting invariant: the currency column (USD here)
2942 // lands at the same column on every posting line, even when
2943 // individual numbers differ in width or sign. The number
2944 // field is right-justified so the currency column is uniform.
2945 let usd_cols: Vec<usize> = out
2946 .lines()
2947 .filter(|l| l.starts_with(" ") && l.contains(" USD"))
2948 .filter_map(|l| l.find("USD"))
2949 .collect();
2950 assert!(
2951 usd_cols.len() >= 4,
2952 "expected ≥4 posting lines, got {usd_cols:?} in {out}"
2953 );
2954 let first = usd_cols[0];
2955 assert!(
2956 usd_cols.iter().all(|&c| c == first),
2957 "expected USD column uniform at {first}, got {usd_cols:?} in:\n{out}"
2958 );
2959 }
2960
2961 #[test]
2962 fn transaction_posting_metadata_indented_four() {
2963 let src =
2964 "2024-01-15 * \"x\"\n Assets:Cash -5.00 USD\n foo: \"bar\"\n Expenses:Misc\n";
2965 let out = format_source(src);
2966 assert!(out.contains("\n foo: \"bar\"\n"), "got: {out}");
2967 }
2968
2969 // ---- Code-review regression tests -----------------------------
2970 //
2971 // Each test pins a bug surfaced by the high-effort code review of
2972 // PR #1284 and verified at runtime against the unfixed formatter.
2973
2974 #[test]
2975 fn cost_spec_per_unit_plus_total_opener_preserved() {
2976 // Bug: format_cost_spec only branched on is_total() and emitted
2977 // `{` for the `{#` opener too, dropping the `#` marker and
2978 // changing semantics from per-unit-plus-total to plain
2979 // per-unit cost.
2980 let src = "2024-01-01 * \"buy\"\n Assets:Brokerage 10 HOOL {# 500.00 USD}\n Assets:Cash -5000.00 USD\n";
2981 let out = format_source(src);
2982 assert!(
2983 out.contains("{# 500.00 USD}"),
2984 "expected `{{#` opener preserved; got:\n{out}"
2985 );
2986 assert!(!out.contains("{500.00 USD}"), "got:\n{out}");
2987 }
2988
2989 #[test]
2990 fn cost_spec_comma_stays_tight_to_prev_token() {
2991 // Bug: format_cost_spec's catch-all arm inserted a space
2992 // before every non-trivia token including COMMA, producing
2993 // `{500.00 USD , 2024-01-15}` instead of the canonical
2994 // `{500.00 USD, 2024-01-15}`.
2995 let src = "2024-01-01 * \"buy\"\n Assets:Brokerage 10 HOOL {500.00 USD, 2024-01-15}\n Assets:Cash -5000.00 USD\n";
2996 let out = format_source(src);
2997 assert!(
2998 out.contains("{500.00 USD, 2024-01-15}"),
2999 "comma must stay tight to USD; got:\n{out}"
3000 );
3001 assert!(
3002 !out.contains("USD ,"),
3003 "no space allowed before comma; got:\n{out}"
3004 );
3005 }
3006
3007 #[test]
3008 fn custom_directive_preserves_date_value_arguments() {
3009 // Bug: emit_custom's post-seen_type match skipped every DATE
3010 // token, silently dropping legitimate date-typed value
3011 // arguments. The leading directive date is already skipped
3012 // via the seen_type=false phase.
3013 let src = "2024-01-01 custom \"budget\" \"name\" 2024-06-15 100.00 USD\n";
3014 let out = format_source(src);
3015 assert!(
3016 out.contains("2024-06-15"),
3017 "value-position DATE must survive; got: {out}"
3018 );
3019 }
3020
3021 #[test]
3022 fn file_level_adjacent_comments_stay_tight() {
3023 // Bug: format_node's top-level walk inserted a blank `\n`
3024 // separator before every emitted item including comments,
3025 // breaking section-header blocks like `; ====\n; HEADER\n; ====`
3026 // by injecting blanks between every adjacent comment line.
3027 let src = "; ====\n; HEADER\n; ====\n2024-01-01 open Assets:A\n";
3028 let expected = "; ====\n; HEADER\n; ====\n2024-01-01 open Assets:A\n";
3029 assert_eq!(format_source(src), expected);
3030 }
3031
3032 #[test]
3033 fn metadata_internal_whitespace_normalized() {
3034 // Bug: emit_meta_entries_of passed META_ENTRY source text
3035 // through verbatim, so `foo: "bar"` and `foo: "bar"` —
3036 // identical typed ASTs — produced different formatter
3037 // output, violating the gofmt-style invariant the rustdoc
3038 // declares.
3039 let a = "2024-01-01 open Assets:Bank\n starting: \"foo\"\n";
3040 let b = "2024-01-01 open Assets:Bank\n starting: \"foo\"\n";
3041 assert_eq!(format_source(a), format_source(b));
3042 }
3043
3044 #[test]
3045 fn metadata_number_thousands_separator_stripped() {
3046 // Same invariant: numbers inside metadata values share the
3047 // canonical thousands-separator policy with posting numbers
3048 // (otherwise the same file would emit inconsistent numeric
3049 // forms in postings vs. metadata).
3050 let src = "2024-01-01 open Assets:Bank\n starting_balance: 1,000.00 USD\n";
3051 let out = format_source(src);
3052 assert!(
3053 out.contains("1000.00 USD"),
3054 "thousands-sep should strip in metadata too; got: {out}"
3055 );
3056 assert!(!out.contains("1,000"), "got: {out}");
3057 }
3058
3059 #[test]
3060 fn bare_cr_line_endings_normalized_to_lf_before_parse() {
3061 // Bug: the lexer doesn't treat bare CR as a line terminator,
3062 // so a classic-Mac-authored `directive\r…\rdirective\r`
3063 // parsed as one broken directive and the rest were silently
3064 // dropped. format_source normalizes line endings BEFORE
3065 // parsing so bare CR (and CRLF) are treated as LF.
3066 let src = "2024-01-01 open Assets:A\r2024-01-02 open Assets:B\r";
3067 let out = format_source(src);
3068 assert!(
3069 out.contains("2024-01-01 open Assets:A"),
3070 "first directive lost: {out:?}"
3071 );
3072 assert!(
3073 out.contains("2024-01-02 open Assets:B"),
3074 "second directive lost on bare-CR input: {out:?}"
3075 );
3076 }
3077
3078 #[test]
3079 fn crlf_input_canonicalizes_to_lf() {
3080 // CRLF and bare CR both fold to LF on the way through the
3081 // canonical pass (the canonical form is LF-only).
3082 let src = "2024-01-01 open Assets:A\r\n2024-01-02 open Assets:B\r\n";
3083 let out = format_source(src);
3084 assert!(
3085 !out.contains('\r'),
3086 "canonical output must be LF-only: {out:?}"
3087 );
3088 assert!(out.contains("2024-01-01 open Assets:A\n"), "got: {out:?}");
3089 assert!(out.contains("2024-01-02 open Assets:B\n"), "got: {out:?}");
3090 }
3091
3092 #[test]
3093 fn metadata_value_with_unary_minus_stays_tight() {
3094 // Bug: emit_meta_entry's tokenized walk inserted a space
3095 // after a unary `+`/`-`, breaking `key: -5.00 USD` →
3096 // `key: - 5.00 USD`. Routed through write_canonical_token_sequence
3097 // so unary detection matches the balance/price/posting paths.
3098 let src = "2024-01-01 open Assets:Bank\n threshold: -5.00 USD\n";
3099 let out = format_source(src);
3100 assert!(
3101 out.contains("threshold: -5.00 USD"),
3102 "unary minus must stay tight in metadata; got: {out}"
3103 );
3104 assert!(
3105 !out.contains("- 5.00"),
3106 "no space after unary minus; got: {out}"
3107 );
3108 }
3109
3110 #[test]
3111 fn metadata_value_with_unary_plus_stays_tight() {
3112 let src = "2024-01-01 open Assets:Bank\n min: +1.00 USD\n";
3113 let out = format_source(src);
3114 assert!(out.contains("min: +1.00 USD"), "got: {out}");
3115 assert!(!out.contains("+ 1.00"), "got: {out}");
3116 }
3117
3118 #[test]
3119 fn cost_spec_negative_cost_stays_tight() {
3120 // Bug: format_cost_spec catch-all had no unary-operator
3121 // handling. `{-500 USD}` formatted to `{- 500 USD}`. Now
3122 // routes through write_canonical_token_sequence.
3123 let src = "2024-01-01 * \"x\"\n Assets:Brokerage 10 HOOL {-500 USD}\n Assets:Cash -5000.00 USD\n";
3124 let out = format_source(src);
3125 assert!(
3126 out.contains("{-500 USD}"),
3127 "negative cost spec must stay tight; got:\n{out}"
3128 );
3129 assert!(!out.contains("{- "), "got:\n{out}");
3130 }
3131
3132 #[test]
3133 fn cost_spec_arithmetic_with_unary_stays_tight() {
3134 // `{500 * -2 USD}` formerly emitted `{500 * - 2 USD}` because
3135 // the cost-spec catch-all didn't understand unary +/-.
3136 let src = "2024-01-01 * \"x\"\n Assets:Brokerage 10 HOOL {500 * -2 USD}\n Assets:Cash -1000.00 USD\n";
3137 let out = format_source(src);
3138 assert!(
3139 out.contains("{500 * -2 USD}"),
3140 "cost-spec arithmetic unary must stay tight; got:\n{out}"
3141 );
3142 }
3143
3144 // ---- Property tests -------------------------------------------
3145 //
3146 // Two invariants the rustdoc's gofmt-style promise depends on,
3147 // pinned over a hand-curated input matrix:
3148 //
3149 // - **Idempotence:** `format_source(format_source(x)) == format_source(x)`.
3150 // - **Round-trip stability for canonicalize_directives:** the
3151 // synthesize-then-canonicalize shim produces text that, when
3152 // parsed back, yields the same Directive count and zero parse
3153 // errors.
3154 //
3155 // The matrix covers every directive kind plus the high-risk
3156 // edge cases the prior reviews surfaced (unary +/- in metadata,
3157 // cost-spec arithmetic, CRLF, bare CR, multi-line strings,
3158 // comments containing quotes, non-Latin accounts). When the
3159 // upstream compatibility corpus is fetched into
3160 // `tests/compatibility/files/` the per-file sweep at the bottom
3161 // also runs; otherwise the file-based test is skipped.
3162
3163 const IDEMPOTENCE_MATRIX: &[(&str, &str)] = &[
3164 ("empty", ""),
3165 ("only_comment", "; header comment\n"),
3166 ("only_directive", "2024-01-01 open Assets:Cash\n"),
3167 (
3168 "two_open_directives",
3169 "2024-01-01 open Assets:A\n2024-01-02 open Assets:B\n",
3170 ),
3171 (
3172 "transaction_with_cost_and_price",
3173 "2024-01-15 * \"buy\"\n Assets:Brokerage 10 HOOL {500.00 USD} @ 510.00 USD\n Assets:Cash -5000.00 USD\n",
3174 ),
3175 (
3176 "transaction_with_per_unit_plus_total_cost",
3177 "2024-01-15 * \"x\"\n Assets:Brokerage 10 HOOL {# 500.00 USD}\n Assets:Cash -5000.00 USD\n",
3178 ),
3179 (
3180 "transaction_with_arithmetic_amount",
3181 "2024-01-15 * \"x\"\n Assets:Cash -(1.00 + 2.00) USD\n Expenses:Misc 3.00 USD\n",
3182 ),
3183 (
3184 "balance_with_arithmetic_and_tolerance",
3185 "2024-01-15 balance Assets:Cash 0.25 + 0.75 USD ~ 0.01 USD\n",
3186 ),
3187 // Regression for Copilot #2: a previous emit_amount_expression
3188 // skipped tokens until the first NUMBER, which dropped a
3189 // leading unary `-` and silently flipped the sign — a
3190 // balance assertion that asserted a debit would assert a
3191 // credit after a round-trip. These fixtures pin the
3192 // sign / paren preservation explicitly.
3193 (
3194 "balance_leading_unary_minus",
3195 "2024-01-15 balance Assets:A -1.00 USD\n",
3196 ),
3197 (
3198 "balance_leading_parenthesized_expression",
3199 "2024-01-15 balance Assets:A (1 + 2) USD\n",
3200 ),
3201 (
3202 "price_leading_unary_minus",
3203 "2024-01-15 price USD -1.00 EUR\n",
3204 ),
3205 (
3206 "price_with_thousands_separator",
3207 "2024-01-15 price USD 1,234.56 EUR\n",
3208 ),
3209 (
3210 "metadata_unary_minus",
3211 "2024-01-01 open Assets:Bank\n threshold: -5.00 USD\n",
3212 ),
3213 (
3214 "metadata_arithmetic",
3215 "2024-01-01 open Assets:Bank\n total: 1000 + 500 USD\n",
3216 ),
3217 (
3218 "cost_spec_with_comma_and_date",
3219 "2024-01-15 * \"x\"\n Assets:Brokerage 10 HOOL {500.00 USD, 2024-01-15}\n Assets:Cash -5000.00 USD\n",
3220 ),
3221 (
3222 "cost_spec_with_negative",
3223 "2024-01-15 * \"x\"\n Assets:Brokerage 10 HOOL {-500 USD}\n Assets:Cash 5000.00 USD\n",
3224 ),
3225 (
3226 "transaction_with_tags_and_links",
3227 "2024-01-15 * \"x\" #tag1 ^link1 #tag2\n Assets:Cash -1.00 USD\n Expenses:Misc 1.00 USD\n",
3228 ),
3229 (
3230 "custom_with_date_value",
3231 "2024-01-01 custom \"budget\" \"name\" 2024-06-15 100.00 USD\n",
3232 ),
3233 (
3234 "non_latin_account_name",
3235 "2024-01-15 * \"x\"\n Активы:Банк -5.00 USD\n Expenses:Misc 5.00 USD\n",
3236 ),
3237 (
3238 "section_header_comments",
3239 "; ====\n; HEADER\n; ====\n2024-01-01 open Assets:A\n",
3240 ),
3241 (
3242 "multiline_note_string",
3243 "2024-01-15 note Assets:Bank \"line 1\nline 2\"\n",
3244 ),
3245 (
3246 "comment_containing_quote",
3247 "; comment with \"a quote\n2024-01-01 open Assets:A\n",
3248 ),
3249 (
3250 "crlf_input",
3251 "2024-01-01 open Assets:A\r\n2024-01-02 open Assets:B\r\n",
3252 ),
3253 (
3254 "bare_cr_input",
3255 "2024-01-01 open Assets:A\r2024-01-02 open Assets:B\r",
3256 ),
3257 (
3258 "file_with_trailing_newlines",
3259 "2024-01-01 open Assets:A\n\n\n",
3260 ),
3261 ("file_without_trailing_newline", "2024-01-01 open Assets:A"),
3262 // Regression for Copilot #1: collect_trailing_comment
3263 // previously returned None for a directive with no
3264 // header-terminating NEWLINE token, which silently dropped
3265 // a same-line trailing comment at EOF when the file lacked
3266 // a trailing newline. The canonical formatter restores the
3267 // trailing newline, but the dropped comment was already
3268 // gone.
3269 (
3270 "trailing_comment_no_final_newline",
3271 "2024-01-15 open Assets:A ; trailing",
3272 ),
3273 (
3274 "posting_with_trailing_comment",
3275 "2024-01-15 * \"x\"\n Assets:Cash -5.00 USD ; pocket\n Expenses:Misc 5.00 USD\n",
3276 ),
3277 (
3278 "balance_assertion_with_meta",
3279 "2024-01-15 balance Assets:Cash 100.00 USD\n source: \"bank\"\n",
3280 ),
3281 (
3282 "options_and_includes",
3283 "option \"title\" \"My Ledger\"\ninclude \"sub.beancount\"\nplugin \"my.plugin\" \"cfg\"\n",
3284 ),
3285 // ---- per-variant coverage ---------------------------------
3286 ("close_directive", "2024-12-31 close Assets:Cash\n"),
3287 ("commodity_directive", "2024-01-01 commodity HOOL\n"),
3288 ("note_directive", "2024-01-15 note Assets:Cash \"a note\"\n"),
3289 ("event_directive", "2024-01-15 event \"location\" \"NYC\"\n"),
3290 (
3291 "query_directive",
3292 "2024-01-15 query \"q1\" \"SELECT account\"\n",
3293 ),
3294 ("pad_directive", "2024-01-15 pad Assets:A Equity:Opening\n"),
3295 (
3296 "document_directive",
3297 "2024-06-01 document Assets:Bank \"stmt.pdf\" #q1\n",
3298 ),
3299 // Note: `#!` and `#+` anywhere on a line, not just at
3300 // line start, open the lexer's SHEBANG / EMACS_DIRECTIVE
3301 // tokens. The fixture places `#+` mid-line and tails it
3302 // with an unbalanced `"`: an incorrect state machine that
3303 // gated the opener on `at_line_start` would stay in Code
3304 // when it hit the `#+`, then flip to InString on the next
3305 // `"` and trap there for the remainder of the file. The
3306 // lexer-agreement property test catches that divergence,
3307 // and the round-trip body runs too because the parser
3308 // treats the mid-line EMACS_DIRECTIVE as same-line
3309 // trailing trivia under the directive-terminator rule.
3310 (
3311 "emacs_directive_mid_line_with_quote",
3312 "2024-01-15 open Assets:A #+stray \"q\n",
3313 ),
3314 ("pushtag_directive", "pushtag #active\n"),
3315 ("poptag_directive", "poptag #active\n"),
3316 ("pushmeta_directive", "pushmeta location: \"NYC\"\n"),
3317 ("popmeta_directive", "popmeta location:\n"),
3318 ];
3319
3320 /// Number of fixtures in [`IDEMPOTENCE_MATRIX`] that legitimately
3321 /// produce zero typed directives — comment-only / empty /
3322 /// pragma-only inputs. The round-trip property test skips these
3323 /// (they have nothing to emit), but every OTHER fixture MUST
3324 /// exercise the body. Bumping this constant when adding such a
3325 /// fixture is the only manual maintenance the coverage floor
3326 /// needs; otherwise the floor (`IDEMPOTENCE_MATRIX.len() -
3327 /// ROUNDTRIP_KNOWN_ZERO_DIRECTIVE_FIXTURES`) tracks the matrix
3328 /// automatically.
3329 ///
3330 /// Today's zero-directive fixtures (skipped by the round-trip
3331 /// body), verified by an exhaustive probe against the live
3332 /// parser:
3333 ///
3334 /// - `empty`, `only_comment` — no directives at all.
3335 /// - `bare_cr_input` — the parser does not recognize bare CR
3336 /// (without a following LF) as a directive terminator, so
3337 /// the file's two would-be directives never surface as
3338 /// structured tokens. The fixture's purpose is the
3339 /// line-ending state-machine pass, not the round-trip body.
3340 /// - `pushtag_directive`, `poptag_directive`,
3341 /// `pushmeta_directive`, `popmeta_directive` — pragma
3342 /// directives don't surface as `Directive` variants on the
3343 /// typed-AST side (the parser also rejects them today, so
3344 /// they produce parse errors and the skip-on-errors guard
3345 /// triggers).
3346 /// - `options_and_includes` — option / include / plugin lines
3347 /// live on separate `ParseResult` collections, not on
3348 /// `.directives`.
3349 ///
3350 /// Note: `comment_containing_quote` and
3351 /// `emacs_directive_mid_line_with_quote` BOTH exercise the
3352 /// body — each is paired with a parseable directive on the
3353 /// same line or an adjacent line, and the trivia token
3354 /// (comment / `EMACS_DIRECTIVE`) attaches as same-line or
3355 /// inter-directive trivia under the directive-terminator
3356 /// rule. Their purpose is the state-machine / lexer agreement
3357 /// property on a comment with an unbalanced `"`, not the
3358 /// zero-directive case.
3359 const ROUNDTRIP_KNOWN_ZERO_DIRECTIVE_FIXTURES: usize = 8;
3360
3361 #[test]
3362 fn lf_to_crlf_outside_strings_preserves_string_interior() {
3363 // Bug: a flat in_string-only state machine would re-inject
3364 // CRLF inside multi-line strings, mutating the user's bytes.
3365 let s = "2024-01-15 note Assets:Bank \"line 1\nline 2\"\n";
3366 let out = lf_to_crlf_outside_strings(s);
3367 assert!(out.contains("line 1\nline 2"), "got: {out:?}");
3368 assert!(out.ends_with("\r\n"), "got: {out:?}");
3369 }
3370
3371 #[test]
3372 fn lf_to_crlf_outside_strings_handles_comment_with_quote() {
3373 // Bug: an unbalanced `"` inside a `;` comment formerly flipped
3374 // in_string=true for the rest of the file, leaving every
3375 // subsequent newline as LF.
3376 let s = "; comment with \"a quote\n2024-01-01 open Assets:A\n";
3377 let out = lf_to_crlf_outside_strings(s);
3378 assert_eq!(
3379 out,
3380 "; comment with \"a quote\r\n2024-01-01 open Assets:A\r\n",
3381 );
3382 }
3383
3384 #[test]
3385 fn lf_to_crlf_outside_strings_handles_percent_comment_with_quote() {
3386 let s = "% percent \"quote\n2024-01-01 open Assets:A\n";
3387 let out = lf_to_crlf_outside_strings(s);
3388 assert_eq!(out, "% percent \"quote\r\n2024-01-01 open Assets:A\r\n");
3389 }
3390
3391 #[test]
3392 fn crlf_to_lf_preserves_crlf_inside_strings() {
3393 // Bug fix mirror: a Windows-authored multi-line string had
3394 // its CRLF folded to LF by the pre-parse normalizer too,
3395 // which silently mutated the user's bytes.
3396 let s = "2024-01-15 note Assets:Bank \"line1\r\nline2\"\r\n";
3397 let normalized = crlf_to_lf_outside_strings(s);
3398 // Outside the string, the trailing CRLF folds to LF; inside
3399 // the string, CRLF stays CRLF (user's bytes preserved).
3400 assert!(
3401 normalized.contains("\"line1\r\nline2\""),
3402 "got: {:?}",
3403 &*normalized
3404 );
3405 assert!(normalized.ends_with('\n') && !normalized.ends_with("\r\n"));
3406 }
3407
3408 #[test]
3409 fn idempotence_matrix() {
3410 // The gofmt invariant in the file rustdoc: f(f(x)) == f(x)
3411 // on every accepted input. Each fixture below covers one
3412 // axis of the canonical-form spec; together they exercise
3413 // every directive kind and every spacing rule shared via
3414 // write_canonical_token_sequence.
3415 for (name, src) in IDEMPOTENCE_MATRIX {
3416 let once = format_source(src);
3417 let twice = format_source(&once);
3418 assert_eq!(
3419 once, twice,
3420 "idempotence broken on fixture `{name}`\n--- once ---\n{once}\n--- twice ---\n{twice}",
3421 );
3422 }
3423 }
3424
3425 #[test]
3426 fn canonicalize_directives_roundtrips_every_synthesized_directive() {
3427 // For each canonical-form fixture: parse → take the typed
3428 // directives → run them through canonicalize_directives →
3429 // re-parse the canonical text → assert the parser reports
3430 // zero errors and the directive count is preserved.
3431 //
3432 // This is the proper end-to-end test of the two-pass shim
3433 // the FFI format.entry and rledger add/extract commands all
3434 // depend on. Without it, a future Directive variant added
3435 // to rustledger-core without matching coverage in
3436 // cst::format would silently round-trip to truncated text.
3437 //
3438 // Counter + assertion guards against silent-skip: if the
3439 // guard at the top of the loop ever filters too many
3440 // fixtures (e.g. a parser regression that drops directives
3441 // from previously-clean fixtures), the test fails instead
3442 // of silently passing with zero coverage.
3443 use rustledger_core::format::FormatConfig;
3444 let cfg = FormatConfig::default();
3445 let mut exercised = 0usize;
3446 for (name, src) in IDEMPOTENCE_MATRIX {
3447 let parsed = crate::parse(src);
3448 if parsed.errors.is_empty() && !parsed.directives.is_empty() {
3449 let dirs: Vec<&rustledger_core::Directive> =
3450 parsed.directives.iter().map(|s| &s.value).collect();
3451 let formatted = super::canonicalize_directives(dirs.iter().copied(), &cfg)
3452 .unwrap_or_else(|e| {
3453 panic!("canonicalize_directives error on fixture `{name}`: {e}")
3454 });
3455 let reparsed = crate::parse(&formatted);
3456 assert!(
3457 reparsed.errors.is_empty(),
3458 "round-trip parse errors on fixture `{name}`:\n--- formatted ---\n{formatted}\n--- errors ---\n{:?}",
3459 reparsed.errors,
3460 );
3461 assert_eq!(
3462 parsed.directives.len(),
3463 reparsed.directives.len(),
3464 "directive count drifted on fixture `{name}`\n--- formatted ---\n{formatted}",
3465 );
3466 exercised += 1;
3467 }
3468 }
3469 let expected = IDEMPOTENCE_MATRIX
3470 .len()
3471 .saturating_sub(ROUNDTRIP_KNOWN_ZERO_DIRECTIVE_FIXTURES);
3472 assert!(
3473 exercised >= expected,
3474 "only {exercised} fixtures exercised the round-trip body, \
3475 expected at least {expected} (= IDEMPOTENCE_MATRIX.len() - \
3476 {ROUNDTRIP_KNOWN_ZERO_DIRECTIVE_FIXTURES}). A parser \
3477 regression or a broken fixture is silently dropping coverage."
3478 );
3479 }
3480
3481 /// `SHEBANG` / `EMACS_DIRECTIVE` lines (`#!…` / `#+…` at line
3482 /// start) also count as comments for the LSP-CRLF state
3483 /// machine. A stray quote inside such a line used to flip
3484 /// `in_string=true` for the rest of the file just like the
3485 /// `;` / `%` comment case the round-3 fix covered.
3486 #[test]
3487 fn lf_to_crlf_outside_strings_handles_emacs_directive_with_quote() {
3488 let s = "#+title: \"My Book\n2024-01-01 open Assets:A\n";
3489 let out = lf_to_crlf_outside_strings(s);
3490 assert_eq!(out, "#+title: \"My Book\r\n2024-01-01 open Assets:A\r\n");
3491 }
3492
3493 #[test]
3494 fn lf_to_crlf_outside_strings_handles_shebang_with_quote() {
3495 let s = "#!shebang \"quote\n2024-01-01 open Assets:A\n";
3496 let out = lf_to_crlf_outside_strings(s);
3497 assert_eq!(out, "#!shebang \"quote\r\n2024-01-01 open Assets:A\r\n");
3498 }
3499
3500 /// `#` NOT at line start is a TAG / HASH token; the state
3501 /// machine must NOT treat it as a comment opener.
3502 #[test]
3503 fn lf_to_crlf_outside_strings_hash_mid_line_is_not_comment() {
3504 let s = "2024-01-15 * \"x\" #tag1\n Assets:A 1 USD\n";
3505 let out = lf_to_crlf_outside_strings(s);
3506 // Every LF outside strings becomes CRLF — including the
3507 // one ending the tag-bearing line.
3508 assert!(out.contains("#tag1\r\n"), "got: {out:?}");
3509 assert!(out.ends_with("\r\n"), "got: {out:?}");
3510 }
3511
3512 /// Regression for Copilot #2 inline review on PR #1284: a
3513 /// previous `emit_amount_expression` dropped leading unary
3514 /// signs and parens, flipping the sign on
3515 /// `2024-01-15 balance Assets:A
3516 /// -1.00 USD` to `1.00 USD` — silent data corruption (a debit
3517 /// asserted as a credit). Byte-exact pins on every shape.
3518 #[test]
3519 fn balance_price_preserve_leading_unary_and_parens() {
3520 // Bare leading minus on balance.
3521 let src = "2024-01-15 balance Assets:A -1.00 USD\n";
3522 assert_eq!(
3523 format_source(src),
3524 "2024-01-15 balance Assets:A -1.00 USD\n"
3525 );
3526
3527 // Bare leading minus on price (sign flip would change
3528 // every quote on the user's commodity).
3529 let src = "2024-01-15 price USD -1.00 EUR\n";
3530 assert_eq!(format_source(src), "2024-01-15 price USD -1.00 EUR\n");
3531
3532 // Leading parenthesized expression. The previous code
3533 // dropped the `(`, which made the trailing `)` unbalanced
3534 // AND made the first-CURRENCY scan find the wrong token.
3535 let src = "2024-01-15 balance Assets:A (1 + 2) USD\n";
3536 assert_eq!(
3537 format_source(src),
3538 "2024-01-15 balance Assets:A (1 + 2) USD\n"
3539 );
3540
3541 // Leading minus on a parenthesized arithmetic expression.
3542 let src = "2024-01-15 balance Assets:A -(1 + 2) USD\n";
3543 assert_eq!(
3544 format_source(src),
3545 "2024-01-15 balance Assets:A -(1 + 2) USD\n"
3546 );
3547 }
3548
3549 /// Regression for Copilot #1 inline review on PR #1284:
3550 /// `collect_trailing_comment` used `?` on the header-terminating
3551 /// NEWLINE, silently dropping same-line trailing comments at
3552 /// EOF when the file had no final newline. The canonical
3553 /// formatter restores the trailing newline, but the dropped
3554 /// comment was already gone — a real-world case for editors
3555 /// that don't insert a trailing newline on save.
3556 #[test]
3557 fn trailing_comment_preserved_at_eof_without_newline() {
3558 let src = "2024-01-15 open Assets:A ; trailing";
3559 assert_eq!(format_source(src), "2024-01-15 open Assets:A ; trailing\n");
3560 }
3561
3562 #[test]
3563 fn try_format_source_returns_ok_on_clean_input() {
3564 let src = "2024-01-15 open Assets:Cash\n";
3565 let out = super::try_format_source(src).expect("clean input should format");
3566 assert_eq!(out, super::format_source(src));
3567 }
3568
3569 #[test]
3570 fn try_format_source_returns_err_on_parse_error() {
3571 // Bare `unparsable` text triggers parser errors. The
3572 // helper must surface them instead of silently emitting
3573 // canonical text around a broken file.
3574 let src = "this is not a directive at all\n";
3575 let err = super::try_format_source(src).expect_err("garbage should error");
3576 assert!(!err.is_empty(), "errors must not be empty");
3577 }
3578
3579 #[test]
3580 fn cr_outside_strings_present_distinguishes_in_string_cr() {
3581 // CR inside a multi-line string literal must NOT count —
3582 // the formatter wouldn't fold it.
3583 let in_string_only = "2024-01-15 note Assets:Bank \"line1\r\nline2\"\n";
3584 assert!(!super::cr_outside_strings_present(in_string_only));
3585
3586 // CR outside any string literal (CRLF line terminator)
3587 // counts — that's what crlf_to_lf_outside_strings would
3588 // fold.
3589 let crlf_terminator = "2024-01-01 open Assets:A\r\n";
3590 assert!(super::cr_outside_strings_present(crlf_terminator));
3591
3592 // No `\r` at all — fast path.
3593 let lf_only = "2024-01-01 open Assets:A\n";
3594 assert!(!super::cr_outside_strings_present(lf_only));
3595
3596 // CR inside a `;` comment is outside any string and counts.
3597 // (Beancount lexer's comment regex excludes the newline, so
3598 // the comment region ends at `\r`; either way, the predicate
3599 // says "yes, the formatter would fold this byte".)
3600 let comment_with_cr = "; comment with \"quote\rstuff\n";
3601 assert!(super::cr_outside_strings_present(comment_with_cr));
3602 }
3603
3604 #[test]
3605 fn canonicalize_directives_directive_count_mismatch_is_reported() {
3606 // Drive the new DirectiveCountMismatch error variant.
3607 // Today's Directive variants all round-trip with matching
3608 // counts, so this test pins the Display rendering of the
3609 // variant (the user-facing message). The positive-count-
3610 // match path is exercised by
3611 // `canonicalize_directives_positive_count_check` below.
3612 let err = super::CanonicalizeError::DirectiveCountMismatch {
3613 input: 3,
3614 reparsed: 2,
3615 };
3616 let msg = format!("{err}");
3617 assert!(msg.contains("3 directive(s)"), "got: {msg}");
3618 assert!(msg.contains("2 survived"), "got: {msg}");
3619 assert!(msg.contains("rledger bug"), "got: {msg}");
3620 }
3621
3622 /// Single source of truth for the variant → fixture mapping
3623 /// used by both the compile-time exhaustiveness check
3624 /// ([`_directive_variant_fixture_coverage`]) and the runtime
3625 /// semantic check
3626 /// ([`directive_variant_fixture_names_resolve_in_matrix`]).
3627 ///
3628 /// Each tuple is `(VariantName, fixture_name)`. The
3629 /// `VariantName` half is the string the runtime check uses to
3630 /// confirm the fixture parses to that variant; the
3631 /// `fixture_name` half is what the compile-time match returns
3632 /// for the same variant. A future `Directive::Hedge` variant
3633 /// only ships with canonical-form coverage if BOTH a new
3634 /// arm is added to the compile-time match AND a row here
3635 /// names a fixture that actually produces a `Hedge` on parse.
3636 const DIRECTIVE_VARIANT_FIXTURE_MAP: &[(&str, &str)] = &[
3637 ("Transaction", "transaction_with_cost_and_price"),
3638 ("Balance", "balance_with_arithmetic_and_tolerance"),
3639 ("Open", "only_directive"),
3640 ("Close", "close_directive"),
3641 ("Commodity", "commodity_directive"),
3642 ("Pad", "pad_directive"),
3643 ("Event", "event_directive"),
3644 ("Query", "query_directive"),
3645 ("Note", "note_directive"),
3646 ("Document", "document_directive"),
3647 ("Price", "price_with_thousands_separator"),
3648 ("Custom", "custom_with_date_value"),
3649 ];
3650
3651 /// Lookup helper: variant tag string → fixture name. Used by
3652 /// the compile-time match below. Panics if the variant is not
3653 /// in the map (which would be an internal-consistency bug, not
3654 /// a user-facing case).
3655 const fn fixture_for_variant(tag: &str) -> &'static str {
3656 let mut i = 0;
3657 while i < DIRECTIVE_VARIANT_FIXTURE_MAP.len() {
3658 let (v, f) = DIRECTIVE_VARIANT_FIXTURE_MAP[i];
3659 // const_str equality: compare byte slices.
3660 let v_bytes = v.as_bytes();
3661 let t_bytes = tag.as_bytes();
3662 if v_bytes.len() == t_bytes.len() {
3663 let mut k = 0;
3664 let mut eq = true;
3665 while k < v_bytes.len() {
3666 if v_bytes[k] != t_bytes[k] {
3667 eq = false;
3668 break;
3669 }
3670 k += 1;
3671 }
3672 if eq {
3673 return f;
3674 }
3675 }
3676 i += 1;
3677 }
3678 panic!("DIRECTIVE_VARIANT_FIXTURE_MAP missing entry for variant tag");
3679 }
3680
3681 /// Compile-time check that every `rustledger_core::Directive`
3682 /// variant has at least one source-text fixture in
3683 /// [`IDEMPOTENCE_MATRIX`] exercising its emit path. The
3684 /// function NEVER runs — its body is an exhaustive `match` over
3685 /// the `Directive` enum. Adding a new variant breaks
3686 /// compilation unless the author adds a match arm referencing
3687 /// `fixture_for_variant("NewVariantName")`, AND adds a row to
3688 /// [`DIRECTIVE_VARIANT_FIXTURE_MAP`] naming the fixture. The
3689 /// runtime test then confirms the fixture parses to a directive
3690 /// of that variant.
3691 ///
3692 /// The non-`Directive` pragma-style directives (Pushtag,
3693 /// Poptag, Pushmeta, Popmeta, options, includes, plugins)
3694 /// don't appear in the typed `Directive` enum; they're covered
3695 /// by separate fixtures whose names map directly into
3696 /// `IDEMPOTENCE_MATRIX`.
3697 #[allow(dead_code)]
3698 fn _directive_variant_fixture_coverage(d: &rustledger_core::Directive) -> &'static str {
3699 match d {
3700 rustledger_core::Directive::Transaction(_) => fixture_for_variant("Transaction"),
3701 rustledger_core::Directive::Balance(_) => fixture_for_variant("Balance"),
3702 rustledger_core::Directive::Open(_) => fixture_for_variant("Open"),
3703 rustledger_core::Directive::Close(_) => fixture_for_variant("Close"),
3704 rustledger_core::Directive::Commodity(_) => fixture_for_variant("Commodity"),
3705 rustledger_core::Directive::Pad(_) => fixture_for_variant("Pad"),
3706 rustledger_core::Directive::Event(_) => fixture_for_variant("Event"),
3707 rustledger_core::Directive::Query(_) => fixture_for_variant("Query"),
3708 rustledger_core::Directive::Note(_) => fixture_for_variant("Note"),
3709 rustledger_core::Directive::Document(_) => fixture_for_variant("Document"),
3710 rustledger_core::Directive::Price(_) => fixture_for_variant("Price"),
3711 rustledger_core::Directive::Custom(_) => fixture_for_variant("Custom"),
3712 }
3713 }
3714
3715 #[test]
3716 fn directive_variant_fixture_names_resolve_in_matrix() {
3717 // Runtime mirror of the compile-time match above:
3718 //
3719 // (1) every fixture name appears in IDEMPOTENCE_MATRIX;
3720 // (2) parsing that fixture produces AT LEAST one
3721 // directive of the variant the map row names.
3722 //
3723 // Without check (2) the compile-time match is satisfied by
3724 // any fixture-name string — a future contributor adding
3725 // a row `("Hedge", "only_comment")` would compile, the
3726 // lookup would resolve, and Hedge would ship with zero
3727 // canonical-form coverage. The semantic check rejects that
3728 // by parsing the named fixture and inspecting the
3729 // directive variant.
3730 use rustledger_core::Directive;
3731 fn matches_variant(d: &Directive, expected: &str) -> bool {
3732 matches!(
3733 (d, expected),
3734 (Directive::Transaction(_), "Transaction")
3735 | (Directive::Balance(_), "Balance")
3736 | (Directive::Open(_), "Open")
3737 | (Directive::Close(_), "Close")
3738 | (Directive::Commodity(_), "Commodity")
3739 | (Directive::Pad(_), "Pad")
3740 | (Directive::Event(_), "Event")
3741 | (Directive::Query(_), "Query")
3742 | (Directive::Note(_), "Note")
3743 | (Directive::Document(_), "Document")
3744 | (Directive::Price(_), "Price")
3745 | (Directive::Custom(_), "Custom")
3746 )
3747 }
3748 for (variant, name) in DIRECTIVE_VARIANT_FIXTURE_MAP {
3749 let (_, src) = IDEMPOTENCE_MATRIX
3750 .iter()
3751 .find(|(n, _)| *n == *name)
3752 .unwrap_or_else(|| {
3753 panic!(
3754 "fixture `{name}` is named by \
3755 DIRECTIVE_VARIANT_FIXTURE_MAP but missing from \
3756 IDEMPOTENCE_MATRIX"
3757 )
3758 });
3759 let parsed = crate::parse(src);
3760 let found = parsed
3761 .directives
3762 .iter()
3763 .any(|s| matches_variant(&s.value, variant));
3764 assert!(
3765 found,
3766 "fixture `{name}` is mapped to `Directive::{variant}` by \
3767 DIRECTIVE_VARIANT_FIXTURE_MAP, but parsing it produced \
3768 no directive of that variant (got {:?}). This silently \
3769 leaves the variant without canonical-form coverage.",
3770 parsed
3771 .directives
3772 .iter()
3773 .map(|s| std::mem::discriminant(&s.value))
3774 .collect::<Vec<_>>()
3775 );
3776 }
3777 }
3778
3779 /// Coverage-mirror check: every `matrix_name` half of the
3780 /// `MIRROR_PAIRS` table in the file-pair integration test
3781 /// (`crates/rustledger-parser/tests/format_compat.rs`) must
3782 /// exist as an entry in [`IDEMPOTENCE_MATRIX`]. The
3783 /// integration test asserts the symmetric half (every
3784 /// `file_pair_name` exists as a directory under `cases/`).
3785 /// Together the two checks guarantee that retiring a
3786 /// bug-class fixture from EITHER side forces an edit to
3787 /// `MIRROR_PAIRS` - which surfaces in review and prevents
3788 /// the silent one-sided drop the README's "two audience" split
3789 /// design would otherwise admit.
3790 ///
3791 /// Hand-maintained copy of the matrix half of the table.
3792 /// Editing `MIRROR_PAIRS` in the integration test requires
3793 /// editing this list too; the test below fires otherwise.
3794 #[test]
3795 fn idempotence_matrix_mirrors_format_compat_pairs() {
3796 const MIRROR_PAIRS_MATRIX_HALF: &[&str] = &[
3797 "balance_leading_unary_minus",
3798 "balance_leading_parenthesized_expression",
3799 "price_leading_unary_minus",
3800 "cost_spec_with_negative",
3801 "cost_spec_with_comma_and_date",
3802 "transaction_with_per_unit_plus_total_cost",
3803 "metadata_unary_minus",
3804 "metadata_arithmetic",
3805 "non_latin_account_name",
3806 "posting_with_trailing_comment",
3807 "multiline_note_string",
3808 "comment_containing_quote",
3809 "transaction_with_tags_and_links",
3810 "custom_with_date_value",
3811 "options_and_includes",
3812 "balance_assertion_with_meta",
3813 "crlf_input",
3814 ];
3815 let matrix_names: std::collections::BTreeSet<&str> =
3816 IDEMPOTENCE_MATRIX.iter().map(|(name, _)| *name).collect();
3817 let missing: Vec<&&str> = MIRROR_PAIRS_MATRIX_HALF
3818 .iter()
3819 .filter(|name| !matrix_names.contains(*name))
3820 .collect();
3821 assert!(
3822 missing.is_empty(),
3823 "IDEMPOTENCE_MATRIX is missing the matrix-half of MIRROR_PAIRS: {missing:?}. \
3824 Either re-add the entry to IDEMPOTENCE_MATRIX, or edit MIRROR_PAIRS in \
3825 tests/format_compat.rs to retire the pair from BOTH sides.",
3826 );
3827 }
3828
3829 /// Property test: the `SourceState` classification used by the
3830 /// line-ending helpers must agree with the lexer's
3831 /// classification on every byte of a corpus of fixtures.
3832 ///
3833 /// Concretely: for every byte offset in every fixture, the
3834 /// state machine's `InString` periods MUST line up with the
3835 /// lexer's STRING token spans, and its `InComment` periods MUST
3836 /// line up with the union of COMMENT / SHEBANG /
3837 /// `EMACS_DIRECTIVE` token spans. A divergence — e.g. the lexer
3838 /// gains a new comment lexeme that the state machine treats as
3839 /// code — fails this test instead of silently mutating user
3840 /// bytes inside the new lexeme on a line-ending round-trip.
3841 #[test]
3842 fn source_state_classification_agrees_with_lexer() {
3843 use crate::logos_lexer::{Token, tokenize_lossless};
3844
3845 for (name, src) in IDEMPOTENCE_MATRIX {
3846 // Run the lexer to get authoritative classification of
3847 // each token. Build a per-byte map of expected state.
3848 let tokens = tokenize_lossless(src);
3849 let mut expected = vec![SourceState::Code; src.len()];
3850 for (token, span) in &tokens {
3851 let classify = match token {
3852 Token::String(_) => Some(SourceState::InString),
3853 Token::Comment(_) | Token::Shebang(_) | Token::EmacsDirective(_) => {
3854 Some(SourceState::InComment)
3855 }
3856 _ => None,
3857 };
3858 if let Some(state) = classify {
3859 for byte in &mut expected[span.start..span.end] {
3860 *byte = state;
3861 }
3862 }
3863 }
3864
3865 // Run the state-machine classifier and compare per
3866 // byte. We skip ONLY the exact bytes where a
3867 // transition fires — the lexer includes those bytes
3868 // inside the resulting token while the state machine
3869 // tags them with the PRE-transition state (the
3870 // 'opener' is still Code, the closing LF is still
3871 // InComment). Tracking the transition indices
3872 // explicitly (rather than skipping every `"`/`;`/`%`
3873 // / newline byte) means a state-machine bug at any
3874 // non-transition `"`/`;`/`%` byte — e.g. inside a
3875 // comment or string — surfaces as a real failure
3876 // instead of being silently masked.
3877 let (actual, transitions) = classify_source_bytes_with_transitions(src);
3878
3879 for (i, (&want, &got)) in expected.iter().zip(actual.iter()).enumerate() {
3880 if transitions.contains(&i) {
3881 continue;
3882 }
3883 assert_eq!(
3884 want,
3885 got,
3886 "state-machine / lexer disagreement on fixture `{name}` \
3887 at byte {i} ({:?}): lexer said {want:?}, state machine said {got:?}",
3888 src.as_bytes()[i] as char
3889 );
3890 }
3891 }
3892 }
3893
3894 /// Walk `s` through the same state-machine logic the
3895 /// line-ending helpers use, returning a per-byte classification
3896 /// AND the set of byte indices where a state transition
3897 /// fired. The transition indices are the ONLY bytes where the
3898 /// state machine and the lexer can legitimately disagree (the
3899 /// off-by-one at opener / closer / terminator); callers
3900 /// comparing against the lexer should skip exactly those
3901 /// indices and assert agreement everywhere else.
3902 fn classify_source_bytes_with_transitions(
3903 s: &str,
3904 ) -> (Vec<SourceState>, std::collections::HashSet<usize>) {
3905 let (body, bom_len) = match s.strip_prefix('\u{FEFF}') {
3906 Some(rest) => (rest, '\u{FEFF}'.len_utf8()),
3907 None => (s, 0),
3908 };
3909 let mut out: Vec<SourceState> = vec![SourceState::Code; s.len()];
3910 let mut transitions = std::collections::HashSet::new();
3911 let mut chars = body.char_indices().peekable();
3912 let mut state = SourceState::Code;
3913 let mut prev_was_backslash = false;
3914 while let Some((rel_i, ch)) = chars.next() {
3915 let i = bom_len + rel_i;
3916 let peek = chars.peek().map(|&(_, c)| c);
3917 // Classify THIS byte under the state BEFORE advancing.
3918 for byte in &mut out[i..i + ch.len_utf8()] {
3919 *byte = state;
3920 }
3921 let prev_state = state;
3922 let next_state = advance_source_state(ch, peek, state, &mut prev_was_backslash);
3923 // Record only OPENING transitions and the comment-
3924 // closing newline, where the state machine and lexer
3925 // legitimately disagree on this single byte:
3926 // - Code → InString : opening `"` is Code-side but
3927 // the lexer puts it inside the STRING token.
3928 // - Code → InComment: opening `;` / `%` / `#!` /
3929 // `#+` is Code-side but the lexer puts it inside
3930 // the COMMENT / SHEBANG / EMACS_DIRECTIVE token.
3931 // - InComment → Code: the `\n` ending the comment is
3932 // classified InComment by the state machine but
3933 // sits OUTSIDE the comment token (the lexer's
3934 // `[^\n\r]*` excludes it).
3935 // The InString → Code transition (closing `"`) is NOT
3936 // a disagreement: the state machine still tags that
3937 // byte as InString (pre-transition), and the lexer
3938 // includes the closing `"` inside the STRING token.
3939 // Skipping it would silently mask a real bug.
3940 if next_state != state {
3941 let opening = matches!(prev_state, SourceState::Code)
3942 && matches!(next_state, SourceState::InString | SourceState::InComment);
3943 let comment_close = matches!(prev_state, SourceState::InComment)
3944 && matches!(next_state, SourceState::Code);
3945 if opening || comment_close {
3946 transitions.insert(i);
3947 // For a `#!` or `#+` opener the lexer's token
3948 // span begins at the `#`, so the second byte
3949 // (`!` / `+`) is also a "before the lexer's
3950 // token start" byte the state machine tags as
3951 // Code. Record it too.
3952 if matches!(ch, '#') && matches!(peek, Some('!' | '+')) {
3953 transitions.insert(i + 1);
3954 }
3955 }
3956 }
3957 state = next_state;
3958 }
3959 (out, transitions)
3960 }
3961
3962 #[test]
3963 fn canonicalize_directives_positive_count_check() {
3964 // Pin the success path of the count check: pass a real
3965 // multi-directive input through canonicalize_directives and
3966 // assert that the output round-trips to the SAME directive
3967 // count. Without this test, a regression that always
3968 // returned CountMismatch (e.g. `==` instead of `!=` on the
3969 // count comparison) would be caught only on production
3970 // calls, not in CI. Together with the Display test above,
3971 // this gives coverage of both arms of the count guard.
3972 use rustledger_core::format::FormatConfig;
3973 let cfg = FormatConfig::default();
3974 let src = "2024-01-01 open Assets:Cash\n2024-01-02 open Assets:Bank\n2024-01-03 close Assets:Cash\n";
3975 let parsed = crate::parse(src);
3976 assert_eq!(
3977 parsed.directives.len(),
3978 3,
3979 "fixture must parse to 3 directives"
3980 );
3981 let dirs: Vec<&rustledger_core::Directive> =
3982 parsed.directives.iter().map(|s| &s.value).collect();
3983 let formatted = super::canonicalize_directives(dirs.iter().copied(), &cfg)
3984 .expect("canonicalize_directives should succeed on this input");
3985 let reparsed = crate::parse(&formatted);
3986 assert_eq!(
3987 reparsed.directives.len(),
3988 3,
3989 "count check accepted but round-trip dropped directives: {formatted}"
3990 );
3991 }
3992
3993 // ---- format_node_range -----------------------------------------
3994
3995 /// Parse `source` via the same pipeline `format_source` uses
3996 /// so the resulting `SyntaxNode`'s `TextRange`s are in the
3997 /// same byte frame `format_node_range`'s `range` argument
3998 /// is expected to use (post-BOM-strip, post-CRLF-to-LF).
3999 /// Returns the syntax node + the normalized source text so
4000 /// tests can compute byte offsets by `.find()`.
4001 fn parse_for_range(source: &str) -> (crate::SyntaxNode, String) {
4002 let (stripped, _bom) = crate::bom::strip_leading(source);
4003 let normalized = crlf_to_lf_outside_strings(stripped).to_string();
4004 let sf = SourceFile::parse(&normalized);
4005 (sf.syntax().clone(), normalized)
4006 }
4007
4008 fn ts(n: usize) -> rowan::TextSize {
4009 rowan::TextSize::try_from(n).expect("offset fits TextSize")
4010 }
4011
4012 /// For any selection covering the whole file, the result text
4013 /// equals `format_node(node)`. Pins the round-trip invariant
4014 /// the design rests on: range formatting is the whole-file
4015 /// formatter restricted to a range, not a parallel canonical
4016 /// form.
4017 #[test]
4018 fn format_node_range_full_range_matches_format_node() {
4019 let source = "\
40202024-01-01 open Assets:Bank USD
40212024-01-15 * \"Coffee\"
4022 Assets:Bank -5.00 USD
4023 Expenses:Food
40242024-01-31 close Assets:Bank
4025";
4026 let (node, src) = parse_for_range(source);
4027 let full = rowan::TextRange::new(ts(0), ts(src.len()));
4028 let (snap, formatted) =
4029 format_node_range(&node, full).expect("full range must include all directives");
4030 assert_eq!(
4031 snap,
4032 rowan::TextRange::new(ts(0), ts(src.len())),
4033 "snap range should be the whole file's textual span"
4034 );
4035 assert_eq!(formatted, format_node(&node));
4036 }
4037
4038 /// A selection that hits only inter-directive whitespace
4039 /// (no directive intersected, no top-level comment
4040 /// intersected) returns `None` — the caller surfaces this
4041 /// as an empty `Vec<TextEdit>`.
4042 #[test]
4043 fn format_node_range_trivia_only_returns_none() {
4044 // The phase-2.0 Directive-Terminator Rule puts every
4045 // inter-directive blank line on the next directive's
4046 // leading trivia, so any byte index between two
4047 // directives is INSIDE the next directive's text_range.
4048 // The only way to reach a truly trivia-only selection
4049 // is a source that has no directives at all (file is
4050 // pure whitespace). That is the case worth pinning —
4051 // the LSP handler maps `None` to an empty
4052 // `Vec<TextEdit>`, which is exactly the right "nothing
4053 // to format" response for a whitespace-only buffer.
4054 let (empty, _) = parse_for_range("\n\n\n");
4055 let sel = rowan::TextRange::new(ts(0), ts(3));
4056 assert!(format_node_range(&empty, sel).is_none());
4057 }
4058
4059 /// Selecting only the first directive's content (the
4060 /// transaction) snaps to that directive and the second
4061 /// directive is left out of both the snap and the output.
4062 #[test]
4063 fn format_node_range_single_directive() {
4064 let source = "\
40652024-01-01 open Assets:Bank USD
40662024-01-15 * \"Coffee\"
4067 Assets:Bank -5.00 USD
4068 Expenses:Food
4069";
4070 let (node, src) = parse_for_range(source);
4071 // Position the selection inside the `open` line. Use
4072 // the byte offset of the word `open` so the test is
4073 // robust to whitespace changes in the fixture.
4074 let open_byte = src.find("open").expect("fixture contains 'open'");
4075 let sel = rowan::TextRange::new(ts(open_byte), ts(open_byte + "open".len()));
4076 let (snap, formatted) = format_node_range(&node, sel).expect("intersects 1 directive");
4077
4078 // Snap should start at byte 0 (the open directive's
4079 // text_range starts at the file's start) and end at
4080 // the open directive's terminating newline.
4081 let open_end = src.find('\n').expect("first directive has terminator") + 1;
4082 assert_eq!(snap.start(), ts(0));
4083 assert_eq!(snap.end(), ts(open_end));
4084 // Output is exactly the open directive's canonical form
4085 // + its `\n` terminator. No second-directive content.
4086 assert_eq!(formatted, "2024-01-01 open Assets:Bank USD\n");
4087 }
4088
4089 /// Multi-directive selection: the author's inter-directive
4090 /// blank lines are preserved (a blank stays a blank; grouped
4091 /// stays grouped), matching whole-file formatting (#1325).
4092 #[test]
4093 fn format_node_range_multi_directive_preserves_blank_lines() {
4094 // #1325: range formatting preserves the author's inter-directive
4095 // blank lines, identically to whole-file formatting. A source
4096 // with a blank between the two directives keeps it...
4097 let spaced = "\
40982024-01-01 open Assets:Bank USD
4099
41002024-01-31 close Assets:Bank
4101";
4102 let (node, src) = parse_for_range(spaced);
4103 let sel = rowan::TextRange::new(ts(0), ts(src.len()));
4104 let (snap, formatted) = format_node_range(&node, sel).expect("intersects 2 directives");
4105 assert_eq!(snap, rowan::TextRange::new(ts(0), ts(src.len())));
4106 assert_eq!(formatted, spaced, "the blank separator must be preserved");
4107
4108 // ...and a grouped source (no blank) stays grouped, rather than
4109 // having a separator inserted.
4110 let grouped = "\
41112024-01-01 open Assets:Bank USD
41122024-01-31 close Assets:Bank
4113";
4114 let (node2, src2) = parse_for_range(grouped);
4115 let sel2 = rowan::TextRange::new(ts(0), ts(src2.len()));
4116 let (_, formatted2) = format_node_range(&node2, sel2).expect("intersects 2 directives");
4117 assert_eq!(formatted2, grouped, "grouped directives must stay grouped");
4118 }
4119
4120 #[test]
4121 fn format_node_range_first_directive_in_snap_keeps_leading_blank() {
4122 // Regression (Copilot review of #1325): when the selection
4123 // covers only the SECOND directive, its predecessor sits outside
4124 // the snap, but the blank line between them is the second
4125 // directive's leading trivia and therefore inside the snapped
4126 // range. Range formatting must re-emit it, not silently delete
4127 // the blank line above the selection.
4128 let source = "2024-01-01 open Assets:Bank USD\n\n2024-01-31 close Assets:Bank\n";
4129 let (node, src) = parse_for_range(source);
4130 // Cursor inside the second (close) directive only.
4131 let close_byte = src.find("close").expect("fixture has 'close'");
4132 let cursor = rowan::TextRange::new(ts(close_byte), ts(close_byte));
4133 let (snap, formatted) = format_node_range(&node, cursor).expect("intersects close");
4134 // The leading blank is preserved in the replacement text...
4135 assert_eq!(formatted, "\n2024-01-31 close Assets:Bank\n");
4136 // ...so applying the edit leaves the blank line intact.
4137 let mut result = src;
4138 result.replace_range(
4139 usize::from(snap.start())..usize::from(snap.end()),
4140 &formatted,
4141 );
4142 assert_eq!(
4143 result, source,
4144 "range-formatting the second directive must not delete the blank above it"
4145 );
4146 }
4147
4148 /// Cursor-only (zero-width) selection inside a directive
4149 /// snaps to that directive. The cursor convention: inside
4150 /// or at the directive's start byte counts as inside;
4151 /// boundary at the directive's end belongs to the next
4152 /// child.
4153 #[test]
4154 fn format_node_range_cursor_inside_directive() {
4155 let source = "\
41562024-01-01 open Assets:Bank USD
41572024-01-31 close Assets:Bank
4158";
4159 let (node, src) = parse_for_range(source);
4160 // Cursor on the `c` of `close` (line 2 of the fixture).
4161 let close_byte = src.find("close").expect("fixture has 'close'");
4162 let cursor = rowan::TextRange::new(ts(close_byte), ts(close_byte));
4163 let (snap, formatted) = format_node_range(&node, cursor).expect("intersects close");
4164 // Snap starts at the close directive's text_range start.
4165 // Per Directive-Terminator Rule the second directive
4166 // OWNS the leading inter-directive trivia — so snap
4167 // starts immediately after the first directive's
4168 // terminator newline.
4169 let close_dir_start = src
4170 .find("\n2024-01-31")
4171 .map(|n| n + 1)
4172 .expect("close directive starts on its own line");
4173 assert_eq!(snap.start(), ts(close_dir_start));
4174 assert_eq!(snap.end(), ts(src.len()));
4175 assert_eq!(formatted, "2024-01-31 close Assets:Bank\n");
4176 }
4177
4178 /// Cursor exactly at the start of a directive snaps to
4179 /// that directive (start-boundary inclusion rule).
4180 #[test]
4181 fn format_node_range_cursor_at_directive_start_includes_directive() {
4182 let source = "\
41832024-01-01 open Assets:Bank USD
41842024-01-31 close Assets:Bank
4185";
4186 let (node, _src) = parse_for_range(source);
4187 // Cursor at byte 0 = start of first directive.
4188 let cursor = rowan::TextRange::new(ts(0), ts(0));
4189 let (_snap, formatted) = format_node_range(&node, cursor).expect("intersects open");
4190 // Only the OPEN should be formatted, not the close.
4191 assert!(formatted.starts_with("2024-01-01 open"));
4192 assert!(!formatted.contains("close"));
4193 }
4194
4195 /// Selection containing a top-level standalone comment
4196 /// (file-leading or between-directive comment that the
4197 /// trivia attachment policy puts on `SOURCE_FILE`) includes
4198 /// the comment in both the snap and the output.
4199 #[test]
4200 fn format_node_range_includes_top_level_comments() {
4201 let source = "\
4202; header
42032024-01-01 open Assets:Bank USD
4204";
4205 let (node, src) = parse_for_range(source);
4206 let sel = rowan::TextRange::new(ts(0), ts(src.len()));
4207 let (snap, formatted) = format_node_range(&node, sel).expect("intersects both");
4208 assert_eq!(snap, rowan::TextRange::new(ts(0), ts(src.len())));
4209 // Header comment, then directive on the next line. No
4210 // canonical blank between a file-level comment group
4211 // and a directive (matches format_node's policy).
4212 assert_eq!(formatted, "; header\n2024-01-01 open Assets:Bank USD\n");
4213 }
4214
4215 /// A selection that lands entirely inside an `ERROR_NODE`
4216 /// (no Directive intersected) returns None. Matches
4217 /// `format_node`'s policy of skipping `ERROR_NODE` children
4218 /// at the top level.
4219 #[test]
4220 fn format_node_range_error_node_only_returns_none() {
4221 // `}}}` at top level isn't a directive — the parser
4222 // wraps it in an ERROR_NODE.
4223 let source = "}}}\n";
4224 let (node, src) = parse_for_range(source);
4225 let sel = rowan::TextRange::new(ts(0), ts(src.len()));
4226 assert!(format_node_range(&node, sel).is_none());
4227 }
4228
4229 /// Past-EOF selection still works: the snap clamps to the
4230 /// last child that intersects within the file. (rowan's
4231 /// `TextRange` is bounded by usize but `format_node_range`
4232 /// doesn't validate `range` against file length — bytes past
4233 /// EOF can never intersect any child, so the rule is
4234 /// degenerate but well-defined.)
4235 #[test]
4236 fn format_node_range_past_eof_clamps() {
4237 let source = "2024-01-01 open Assets:Bank USD\n";
4238 let (node, src) = parse_for_range(source);
4239 let past_eof = rowan::TextRange::new(ts(src.len()), ts(src.len() + 1000));
4240 // The cursor / range is past EOF — no child intersects.
4241 assert!(format_node_range(&node, past_eof).is_none());
4242 // But a range that STRADDLES EOF still snaps to the
4243 // last intersecting directive.
4244 let straddle = rowan::TextRange::new(ts(0), ts(src.len() + 1000));
4245 let (snap, formatted) = format_node_range(&node, straddle).expect("intersects open");
4246 assert_eq!(snap, rowan::TextRange::new(ts(0), ts(src.len())));
4247 assert_eq!(formatted, "2024-01-01 open Assets:Bank USD\n");
4248 }
4249
4250 /// A cursor inside a posting (sub-directive position) snaps
4251 /// up to the enclosing transaction — the design pins
4252 /// "round to top-level directive boundaries, no finer."
4253 #[test]
4254 fn format_node_range_cursor_in_posting_snaps_to_transaction() {
4255 let source = "\
42562024-01-15 * \"Coffee\"
4257 Assets:Bank -5.00 USD
4258 Expenses:Food
4259";
4260 let (node, src) = parse_for_range(source);
4261 // Position the cursor on the `B` of `Bank` in the
4262 // first posting.
4263 let bank_byte = src.find("Bank").expect("fixture has Bank");
4264 let cursor = rowan::TextRange::new(ts(bank_byte), ts(bank_byte));
4265 let (snap, _formatted) = format_node_range(&node, cursor).expect("intersects transaction");
4266 // Snap covers the WHOLE transaction (start of file
4267 // through final posting's newline).
4268 assert_eq!(snap.start(), ts(0));
4269 assert_eq!(snap.end(), ts(src.len()));
4270 }
4271
4272 /// Selection straddling an `ERROR_NODE` between two valid
4273 /// directives: snap range would cover the union (including
4274 /// `ERROR_NODE` bytes), so `format_node_range` returns
4275 /// `None` instead of silently deleting the error content.
4276 ///
4277 /// This is the deliberate divergence from `format_node`'s
4278 /// whole-file policy. `format_source(broken_source)` does
4279 /// drop `ERROR_NODE` content — but that path's callers
4280 /// (`rledger format` CLI, FFI `format.entry`) opt into
4281 /// content loss by invoking the canonical-form pipeline. The
4282 /// per-handler LSP `textDocument/rangeFormatting` path has no
4283 /// such opt-in, so it refuses to delete user content the
4284 /// parser couldn't classify. See the function's rustdoc for
4285 /// the per-handler asymmetry rationale.
4286 #[test]
4287 fn format_node_range_bails_when_snap_covers_error_node() {
4288 let source = "\
42892024-01-01 open Assets:Bank USD
4290}}}garbage{{{
42912024-01-31 close Assets:Bank
4292";
4293 let (node, src) = parse_for_range(source);
4294 let sel = rowan::TextRange::new(ts(0), ts(src.len()));
4295 assert!(
4296 format_node_range(&node, sel).is_none(),
4297 "selection covering both directives + ERROR_NODE between them must bail \
4298 to avoid silently deleting the garbage line — got Some output",
4299 );
4300 }
4301
4302 /// Selection that intersects only the FIRST valid directive
4303 /// in a broken file (no `ERROR_NODE` byte in the snap range)
4304 /// still formats. Pins that the `ERROR_NODE` bail is precisely
4305 /// scoped to the snap range, not to "the file has any
4306 /// `ERROR_NODE` at all".
4307 #[test]
4308 fn format_node_range_formats_directive_when_snap_does_not_cover_error_node() {
4309 let source = "\
43102024-01-01 open Assets:Bank USD
4311}}}garbage{{{
43122024-01-31 close Assets:Bank
4313";
4314 let (node, src) = parse_for_range(source);
4315 // Selection covers ONLY the open directive (first line +
4316 // its terminator). The ERROR_NODE on line 1 sits at byte
4317 // offset == open_end (length of first line including \n)
4318 // onward, OUTSIDE the snap range.
4319 let open_end = src.find('\n').expect("first directive has newline") + 1;
4320 let sel = rowan::TextRange::new(ts(0), ts(open_end));
4321 let (snap, formatted) =
4322 format_node_range(&node, sel).expect("selection covers only the open");
4323 assert_eq!(snap.start(), ts(0));
4324 assert_eq!(snap.end(), ts(open_end));
4325 assert_eq!(formatted, "2024-01-01 open Assets:Bank USD\n");
4326 }
4327
4328 /// `format_node_with_alignment(node, compute_alignment(sf))` is
4329 /// byte-identical to `format_node(node)`. Pins the cache
4330 /// contract: passing the correct alignment is a pure
4331 /// optimization, NOT a behavior change.
4332 #[test]
4333 fn format_node_equals_format_node_with_alignment() {
4334 let fixtures: &[(&str, &str)] = &[
4335 ("empty", ""),
4336 ("open only", "2024-01-01 open Assets:Bank USD\n"),
4337 (
4338 "single txn",
4339 "\
43402024-01-15 * \"Coffee\"
4341 Assets:Bank -5.00 USD
4342 Expenses:Food
4343",
4344 ),
4345 (
4346 "multi txn varying widths",
4347 "\
43482024-01-15 * \"A\"
4349 Assets:Bank -5.00 USD
4350 Expenses:Food
43512024-02-15 * \"B\"
4352 Assets:Investment:Long:Path -123456.78 USD
4353 Expenses:Tax 100.00 USD
4354",
4355 ),
4356 ];
4357 for (label, source) in fixtures {
4358 let (node, _src) = parse_for_range(source);
4359 let source_file = SourceFile::cast(node.clone()).unwrap();
4360 let alignment = compute_alignment(&source_file);
4361 assert_eq!(
4362 format_node(&node),
4363 format_node_with_alignment(&node, alignment),
4364 "format_node_with_alignment must match format_node for {label}",
4365 );
4366 }
4367 }
4368
4369 /// `format_node_range_with_alignment(node, range, compute_alignment(sf))`
4370 /// matches `format_node_range(node, range)` byte-identically.
4371 /// Same shape as the previous test, for the range path.
4372 #[test]
4373 fn format_node_range_matches_format_node_range_with_alignment() {
4374 let source = "\
43752024-01-15 * \"A\"
4376 Assets:Bank -5.00 USD
4377 Expenses:Food
43782024-02-15 * \"B\"
4379 Assets:Investment:Long:Path -123456.78 USD
4380 Expenses:Tax 100.00 USD
4381";
4382 let (node, src) = parse_for_range(source);
4383 let source_file = SourceFile::cast(node.clone()).unwrap();
4384 let alignment = compute_alignment(&source_file);
4385 // Pin the equivalence on three ranges: whole file,
4386 // cursor inside the first transaction, cursor inside the
4387 // second.
4388 let sels = [
4389 rowan::TextRange::new(ts(0), ts(src.len())),
4390 rowan::TextRange::new(ts(0), ts(10)),
4391 rowan::TextRange::new(ts(src.len() - 10), ts(src.len())),
4392 ];
4393 for sel in sels {
4394 let uncached = format_node_range(&node, sel);
4395 let cached = format_node_range_with_alignment(&node, sel, alignment);
4396 assert_eq!(
4397 uncached, cached,
4398 "format_node_range_with_alignment must match \
4399 format_node_range for range {sel:?}",
4400 );
4401 }
4402 }
4403
4404 /// The cached `ParseResult::alignment` value matches what
4405 /// `format_node` would compute on the parsed tree. End-to-end
4406 /// regression: an LSP caller passing `parse_result.alignment`
4407 /// to `format_node_with_alignment` produces the same output
4408 /// as the bare `format_node` (uncached path).
4409 #[test]
4410 fn parse_result_alignment_drives_identical_format_output() {
4411 let source = "\
44122024-01-15 * \"Coffee\"
4413 Assets:Bank -5.00 USD
4414 Expenses:Food
4415";
4416 let parse_result = crate::parse(source);
4417 let node = parse_result.syntax_node();
4418 assert_eq!(
4419 format_node(&node),
4420 format_node_with_alignment(&node, parse_result.alignment),
4421 "ParseResult::alignment must drive identical format output to format_node",
4422 );
4423 }
4424
4425 /// `format_source_with_parsed(parse(s), s) == format_source(s)`
4426 /// byte-identical across a representative fixture set including
4427 /// CRLF and BOM-prefixed sources. This is the load-bearing
4428 /// equivalence for the LSP `format_document` / FFI
4429 /// `format.source` / WASM `ParsedLedger::format` migrations:
4430 /// they swap `format_source(source)` for
4431 /// `format_source_with_parsed(parse_result, source)` on the
4432 /// assumption that the two produce the same output. Without
4433 /// this test, a future converter or formatter change that
4434 /// silently diverged the two paths would break canonical-form
4435 /// expectations in production.
4436 #[test]
4437 fn format_source_with_parsed_matches_format_source() {
4438 let fixtures: &[(&str, &str)] = &[
4439 ("empty", ""),
4440 ("comment only", "; hello\n"),
4441 (
4442 "single transaction LF",
4443 "\
44442024-01-15 * \"Coffee\"
4445 Assets:Bank -5.00 USD
4446 Expenses:Food
4447",
4448 ),
4449 (
4450 "multi transaction varying widths LF",
4451 "\
44522024-01-15 * \"A\"
4453 Assets:Bank -5.00 USD
4454 Expenses:Food
44552024-02-15 * \"B\"
4456 Assets:Investment:Long:Path -123456.78 USD
4457 Expenses:Tax 100.00 USD
4458",
4459 ),
4460 (
4461 "arithmetic amounts LF",
4462 "\
44632024-01-15 * \"Split\"
4464 Assets:Bank -10.00 + 5.00 USD
4465 Expenses:Misc
4466",
4467 ),
4468 (
4469 "CRLF source",
4470 "2024-01-15 * \"Coffee\"\r\n Assets:Bank -5.00 USD\r\n Expenses:Food\r\n",
4471 ),
4472 ("BOM-prefixed", "\u{FEFF}2024-01-01 open Assets:Bank USD\n"),
4473 // BOM + CRLF — Windows-authored ledger with a BOM
4474 // prefix. `format_source` BOM-strips + CRLF→LF
4475 // normalizes before parsing. The cache path consumes
4476 // a CST that's BOM-stripped but NOT CRLF-normalized.
4477 // Byte-identity holds because the formatter rebuilds
4478 // canonical output from typed values (no trivia
4479 // passthrough).
4480 (
4481 "BOM + CRLF combination",
4482 "\u{FEFF}2024-01-15 * \"Coffee\"\r\n Assets:Bank -5.00 USD\r\n Expenses:Food\r\n",
4483 ),
4484 // Parse-error file — exercises the fallback. Without
4485 // the `errors.is_empty()` guard, the cache path would
4486 // emit text for ERROR_NODE-wrapped content while
4487 // `format_source` would drop those bytes; identity
4488 // would fail. The fallback delegates to
4489 // `format_source(source)` so identity holds.
4490 (
4491 "parse errors (exercises fallback)",
4492 "2024-01-15 * \"x\"\n Assets:Bank -5.00 USD\n}}}garbage\n",
4493 ),
4494 // Bare-`\r` (classic Mac) line terminators. The
4495 // `format_source` path normalizes bare-CR to LF via
4496 // `crlf_to_lf_outside_strings`, then parses cleanly.
4497 // `parse_via_cst` does NOT normalize bare-CR, so the
4498 // CST sees broken syntax and `parse_result.errors`
4499 // is non-empty — the fallback fires. Byte-identity
4500 // holds via the same `format_source` delegation.
4501 (
4502 "bare CR line terminators (exercises fallback)",
4503 "2024-01-01 open Assets:Bank USD\r2024-01-02 open Assets:Cash USD\r",
4504 ),
4505 ];
4506 for (label, source) in fixtures {
4507 let parse_result = crate::parse(source);
4508 let baseline = format_source(source);
4509 let cached = format_source_with_parsed(&parse_result, source);
4510 assert_eq!(
4511 cached, baseline,
4512 "format_source_with_parsed must match format_source for {label}: \
4513 baseline {baseline:?}, cached {cached:?}",
4514 );
4515 }
4516 }
4517
4518 /// Mismatched-pair safety: in debug builds, passing a
4519 /// length-mismatched `(parse_result, source)` pair panics via
4520 /// the `debug_assert_eq!`. Release builds silently emit text
4521 /// for the wrong buffer (the producer-only invariant is the
4522 /// caller's responsibility, documented in
4523 /// `ParseResult::alignment`).
4524 #[cfg(debug_assertions)]
4525 #[test]
4526 #[should_panic(expected = "source` whose length doesn't match")]
4527 fn format_source_with_parsed_panics_on_length_mismatch() {
4528 let parse_result = crate::parse("2024-01-01 open Assets:Bank USD\n");
4529 // Different length — debug_assert fires.
4530 let _ = format_source_with_parsed(&parse_result, "different");
4531 }
4532}