Formatter.Logical

The logical stage's entry point: build the Logical Printing Tree from a parsed module. The AST walk itself lives in Formatter.Logical.MakeLogical (lptFromAst); this module runs it and applies the finishing passes.

makeLogicalPrintingTree : Module -> Context -> Result String LPNode

Build the Logical Printing Tree for a parsed module.

The pipeline runs in five stages:

  1. lptFromAst (in Formatter.Logical.MakeLogical) walks the Src.Module and emits one OriginalRows child per top-level declaration, in source order.
  2. Formatter.Logical.Comments.lptAddComments weaves in comment nodes from the parse Ctx.Context, as siblings of the declarations they belong to.
  3. Formatter.Logical.SortSymbols.sortSymbols reorders the symbols inside every exposing ( ... ) list into elm-format's three groups -- operators, then types/unions, then plain values, alphabetically within each -- carrying each symbol's attached comments along with it. import statements themselves are left in source order. Runs after comments are attached, never before -- see that module's docs for why.
  4. Formatter.Logical.VerticalSpace.insertFlowBlankLines walks the whole tree once, inserting EmptyLine siblings between let bindings, before else/else if, and before a comment between when cases.
  5. Formatter.Logical.VerticalSpace.insertEmptyLines inserts EmptyLine siblings to normalise blank-line spacing between top-level declarations.

Only stage 1 can fail; the later stages are total. An Err from lptFromAst comes back wrapped in "Failed to create intermediate representation for pretty printing: ", so the caller can tell which stage produced it. The resulting LPNode is the input to Formatter.Render.