Skip to content

Add opt-in options for a compact multiline formatting style#7

Open
sw1sh wants to merge 2 commits into
WolframResearch:masterfrom
sw1sh:compact-multiline-options
Open

Add opt-in options for a compact multiline formatting style#7
sw1sh wants to merge 2 commits into
WolframResearch:masterfrom
sw1sh:compact-multiline-options

Conversation

@sw1sh

@sw1sh sw1sh commented Jun 6, 2026

Copy link
Copy Markdown

Summary

Adds seven opt-in options that together produce a compact, consistent multiline
style (one element per line, never a mid-expression wrap), best used with
BreakLinesMethod -> "LineBreakerV2". All seven default to the current
behavior, so existing output and the test suite are unchanged.

These came out of formatting a real paclet to a house style that the existing
options could not express.

Options

  • "KeepBindingsInline" — decide whether a scoping (Module/Block/With/
    Function) or control (If/Switch/Which/...) construct must explode based
    on the width of its leading line (head + binding list / condition, e.g.
    Block[{a, b},) rather than the maximum width of the whole construct. A long
    body line no longer forces a short binding list / condition onto its own line.

  • "SpaceAfterControlOpener" — insert a space after the opener of a multiline
    control structure so the condition lines up with the branches (If[ cond,).

  • "GlueAssignmentRHS" — keep the RHS of an assignment (Set, SetDelayed,
    UpSet, ...) on the operator line (f := Block[{) instead of breaking after
    the operator. Applies to top-level and inner assignments.

  • "TrailingCommas" — when breaking a comma-separated sequence whose elements
    are all single-line, keep the comma at the end of the element line; when an
    element is multiline, the comma stays on its own line as a boundary marker.

  • "InlineShortControl" — a control structure that fits within LineWidth
    and whose branches are all single-line stays on one line (If[a, b, c])
    instead of always breaking.

  • "SpaceAfterPrefixNot" — insert a space after a prefix Not (! cond).

  • "SpaceAroundPatternOperators" — space Pattern (:), Optional (:),
    and PatternTest (?) like ordinary operators (name : pat, x_ ? NumericQ);
    MessageName (::) and Power (^) stay tight.

Example (all options on, LineWidth -> 60):

run[dir_String] := Block[{cmd, res},
    If[ ! FileExistsQ[dir],
        Return[<|"code" -> 1, "err" -> "missing"|>]
    ];
    res = exec[cmd];
    res
]

Tests

Adds Tests/GuideStyle.mt (12 tests) covering each option and a combined case.
The existing suite is unchanged (the new options default off).

Notes

Implementation is localized to the scoping/control indenters, the prefix and
operator/comma renderers, and the pattern-operator spacing rule in Indent.wl,
plus option plumbing in CodeFormatter.wl. Each option is gated, so default
formatter output is unchanged.

🤖 Generated with Claude Code

sw1sh and others added 2 commits June 6, 2026 05:38
Seven options, all defaulting to the current behavior so existing output and
tests are unchanged (best used together, with BreakLinesMethod ->
"LineBreakerV2"):

- "KeepBindingsInline": decide the scoping/control break on the leading-line
  width (head + binding list / condition) rather than the max width, so a long
  body line does not force a short binding list / condition onto its own line.
- "SpaceAfterControlOpener": `If[ cond,` so the condition aligns with branches.
- "GlueAssignmentRHS": keep an assignment RHS on the operator line
  (`f := Block[{`), for top-level and inner Set / SetDelayed / UpSet / ... .
- "TrailingCommas": comma at the end of a single-line element; on its own line
  only when an element is multiline.
- "InlineShortControl": a control structure that fits on one line and whose
  branches are all single-line stays inline (`If[a, b, c]`).
- "SpaceAfterPrefixNot": `! cond`.
- "SpaceAroundPatternOperators": space Pattern (:), Optional (:), and
  PatternTest (?) like ordinary operators; MessageName (::) and Power (^) stay
  tight.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
12 tests covering each option (KeepBindingsInline on/off, SpaceAfterControlOpener,
GlueAssignmentRHS for Set/SetDelayed and inner assignments, TrailingCommas
single-line and multiline-boundary, InlineShortControl, SpaceAfterPrefixNot,
SpaceAroundPatternOperators) and a combined case. The existing suite is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@sw1sh sw1sh force-pushed the compact-multiline-options branch from 426f163 to a90524b Compare June 6, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant