align

Function align 

Source
pub fn align<C: Cost>(d: Doc<C>) -> Doc<C>
Expand description

Aligns a document.

Aligning resets the indentation level while rendering d to the starting column of d. Any newlines within d will line up with that column. Any uses of nest within d will increase the indentation starting from that column.

After rendering d, the indentation level will return to its previous value.

Alignment is particularly useful for formatting Lisp code, as Lisps tend to prefer to align children based on the start of their parent, rather than relative to an overall indentation level.

§Example

let doc = nl() &
          lparen() &
          text("+") &
          space() &
          align(text("123") &
                nl() &
                align(text("(let ((a 454))") &
                      nest(2, nl() &
                              text("(+ a 2)))"))));

assert_eq!(doc.to_string(), r"
(+ 123
   (let ((a 454))
     (+ a 2)))");

This example uses two alignment points. The first wraps the arguments to + so that they line up in a column. The second sets the alignment for the let form, so that the nest inside it will indent subsequent lines two spaces from the start of the let. That’s not strictly needed in this specific case, since the outer align was at the same column, but the inner align ensures that wherever the let may appear in the document, it will be indented correctly.