Class StructuralReorderer

java.lang.Object
org.ek9lang.compiler.formatter.StructuralReorderer

final class StructuralReorderer extends Object
Pass 2 of the EK9 source formatter: Structural Reordering.

Phase 1: Module-level reordering:

  • Reference sorting: sorts identifierReference entries within a references block lexicographically
  • Module block ordering: orders defines blocks by canonical type (constant, type, record, trait, class, function, component, text, service, application, program, package)
Phase 2: Blank line normalization (Steve, 2026-10-28 — the author's order of methods and operators within a construct is kept; see below):
  • Rule 1: No blank lines between consecutive field initialisers (unless a comment separates them)
  • Rule 2: No blank lines between argument/return parameter declarations (same comment exception)
  • Rule 3: Exactly one blank line between the last parameter declaration and the method body
Properties are NOT reordered — their order may be significant for JSON serialisation. Methods (constructors included) and operators are NOT reordered either — Steve's decision, 2026-10-28: sorting them (alphabetically, by overload signature, or by a canonical operator order) destroys teaching order ("write what is special, default the rest") and fights the IDE, which formats after every Apply. AggregateMemberOrder (E01086) is the only order the compiler itself requires: properties, then constructors and methods, then operators, then a bare default operator last. That is a KIND-level order; it says nothing about the order of members WITHIN a kind, so a body already satisfying it (the only kind a compiling source can be) needs no member moved at all — this pass leaves such a body exactly as written. A body failing E01086 is left alone too: fixing it is a compile error, not a formatting job. Doc comments travel with the construct they precede.
  • Constructor Details

    • StructuralReorderer

      StructuralReorderer()
  • Method Details

    • reorder

      String reorder(String source)
      Apply structural reordering to the given canonically indented EK9 source.
      Parameters:
      source - The canonically indented EK9 source code.
      Returns:
      The source with references sorted and module blocks reordered, or null if parsing fails.
    • parseToTree

      Parse the source to a CompilationUnitContext. Returns null if parsing encounters errors.