Skip to content

align-match-case โ€‹

A match whose case bodies all collapse to a single expression reads naturally as a dispatch table, with patterns on the left and results on the right.

gathers consecutive single-expression cases into a shared column for the post-pattern : separator, so the pattern column flushes left and the body column flushes right, and the reader reads the table by scanning rows rather than tracing each case body.

The rule fires only on runs of single-expression cases at the same indentation. A multi-statement case body, a comment between cases, or a nested match breaks the run and leaves the surrounding cases aligned in isolation. Pair with

to skip padding on one-arm matches and with to align separators inside dict-returning case bodies.

Configuration โ€‹

KeyTypeDefaultMeaning
enabledbooltrueToggle the rule on or off
max-shiftpositive int8Ceiling on per-line padding
max-shift-policy"split" | "drop""split"How to handle a group whose widest member exceeds max-shift. See the per-rule knobs for the full semantics

max-shift caps the per-line padding the alignment can introduce. When a match's widest pattern would push the post-pattern : column past the cap, max-shift-policy decides the fallback shape, which defaults to "split". The per-rule knobs reference covers the "drop" policy.

The Canonical Case โ€‹

A match whose arms carry a variety of collapsible body kinds, from pass and break to raise and return, all aligning on the post-pattern : separator.

python
def dispatch(token):
    match token:
        case "noop" : pass
        case "skip" : continue
        case "stop" : break
        case "boom" : raise RuntimeError("boom")
        case "echo" : log(token)
        case _      : return None

More Examples โ€‹

Arms carry case A | B alternations of differing width. The widest third arm exceeds max-shift and breaks into its own sub-group, dropping out of the alignment with its : hugging the pattern while the narrower arms share one column.

Five multi-line arms test the 88-column collapse budget. Arms collapse only when the one-line form fits, so the under-88 and exactly-88 arms fold while the 89-column arm, the far-wider arm, and the arm whose if guard pushes it over all stay multi-line. The gate measures guard width alongside pattern width.

A standalone # comment line sits between two arms. The comment rides through unchanged while the arms on either side collapse and align their : to one shared column around it.

The first arm's body is a single assignment whose right-hand side spans several lines across a parenthesized +. The multi-line right-hand side disqualifies the arm even though it is structurally one statement, leaving it untouched. The remaining arm is alone in its sub-group and collapses without padding.

A two-statement arm sits second in a four-arm match. The multi-statement body disqualifies that arm and splits the match into sub-groups, so the lone arm before it collapses without padding and the two arms after it align together at the wider of their patterns. The two-statement arm itself stays multi-line.

An outer arm whose body is itself a match disqualifies and stays multi-line, while the lone sibling arm collapses without padding. The inner match is visited as its own group, collapsing and aligning its three single-statement arms on a separate : column.