pub fn format_source_with_parsed(
parse_result: &ParseResult,
source: &str,
) -> StringExpand description
Like format_source but reuses the caller’s
crate::ParseResult instead of re-parsing source.
Skips both expensive pre-passes the bare format_source runs
every call: the lex+parse from SourceFile::parse(&normalized),
and the O(N_postings) compute_alignment walk. Both pieces
are already on parse_result (in syntax_root and
alignment respectively, populated by parse_via_cst). For
any consumer that already holds a ParseResult — the LSP
format_document handler, the FFI format.source endpoint,
the WASM ParsedLedger::format bridge — this entry skips two
redundant traversals of the file.
Output equivalence with format_source. Pinned by
parse_result_alignment_cache::format_source_with_parsed_matches_format_source_under_fallback
(the fallback exercises broken sources) and
cst::format::tests::format_source_with_parsed_matches_format_source
(the cache path exercises clean sources) across LF / CRLF /
BOM / parse-error / mixed-line-ending fixtures. The cache-
path equivalence holds because the formatter rebuilds output
from each directive’s typed values rather than echoing
trivia, so the CRLF-vs-LF difference in the underlying CST
trivia never reaches the output. The fallback path is
byte-trivially equivalent (it IS format_source).
CRLF re-injection is still the caller’s responsibility.
Same as format_source: this function always returns LF;
LSP consumers that need to preserve CRLF for Windows-
authored files call lf_to_crlf_outside_strings on the
returned text.
Parse-error fallback. When parse_result.errors is
non-empty, this function delegates to format_source(source)
— losing the cache benefit but preserving byte-identity for
inputs whose CST diverges from what format_source’s
pre-parse normalization would produce. Concretely: bare-\r
(classic Mac) line terminators are normalized to LF by
format_source before parsing, but parse_via_cst does NOT
normalize them — so the cached CST treats them as broken
content and parse_result.errors is non-empty. The fallback
path keeps the byte-identity claim total instead of
“holds-only-when-clean”.
Stale parse_result is the caller’s responsibility. The
producer-side cache invariant (see
crate::ParseResult::alignment rustdoc) says
parse_result must come from a fresh parse(source) with
the same source. A debug_assert_eq! compares the CST’s
text length against source.len() - bom_offset to catch the
most common mismatched-pair class (different documents have
different lengths) in debug builds; release builds skip the
check. Identical-length mismatches still pass silently —
the rustdoc-level contract remains the source of truth.
§Panics
Panics if parse_result.syntax_root is not a SOURCE_FILE
(always true for results produced by crate::parse).
In debug builds, panics on a (parse_result, source)
length-mismatch via debug_assert_eq!. Release builds
silently emit possibly-wrong output (the producer-only
invariant is the caller’s responsibility).