Skip to main content

compute_alignment

Function compute_alignment 

Source
pub fn compute_alignment(sf: &SourceFile) -> PostingAlignment
Expand description

Compute the file-wide alignment columns for a parsed SourceFile.

Walks every Transaction’s postings once, takes the max LHS width (account + optional flag ) and max number-text width, and derives the column targets from them.

O(N_postings). Public so consumers can pre-compute the alignment once (typically at parse time) and pass the cached PostingAlignment into format_node_with_alignment or format_node_range_with_alignment — eliminates the per-call walk in hot formatting paths (LSP format-on-type through a parse error, repeat-format scripts, etc.).

Tree-shape precondition. sf must be a SourceFile whose CST was produced by parse_structured (directly or transitively via parse_via_cst / parse). Hand-built partial trees (e.g., a GreenNodeBuilder invocation for snippet formatting) silently return PostingAlignment::default() because their wrapping nodes fail the ast::Directive::Transaction::cast check. Likewise, transactions wrapped in ERROR_NODE by mid-edit error recovery are excluded — see parse_result_alignment_cache::mid_transaction_error_node for the pinned behavior. The function never panics on a partial tree; it just returns the all-zero alignment for the no-postings case.

Pinning the contract. ParseResult::alignment is populated by calling this function during parse_via_cst; the equivalence between the cached value and a fresh call is guaranteed by the parse_result_alignment_cache::* regression tests (7 fixtures) in this module.