Repository navigation
feat: add weak whitespace and indentation policy - #17
Merged
Merged
Conversation
QuadnucYard
force-pushed
the
weak-space-2
branch
2 times, most recently
from
October 1, 2026 13:04
a4fa401 to
6c2f74e
Compare
Options are plain configuration with no invariants to protect, so reading and writing fields directly beats getter indirection. Mark the struct `#[non_exhaustive]` so future options stay additive, and keep `new` plus the fluent `with_*` methods as the construction path. `Printer` now carries `options: RenderOptions` instead of separate fields, so render state tracks one configuration value.
QuadnucYard
force-pushed
the
weak-space-2
branch
from
October 2, 2026 02:10
6c2f74e to
4a8b1ac
Compare
Add `WeakSpace` and `WeakLine` primitives with consistent constructors across allocator and document APIs. `weak_space()` defers spaces after nonempty text. Consecutive weak spaces accumulate and are committed by following nonempty text or a line-suffix flush. Leading spaces and uncommitted trailing spaces are omitted; empty text does not commit them. A line-suffix flush commits them by presence alone, without inspecting the suffix document. `weak_line()` emits a break only when the current line contains nonempty text or a pending suffix. It has no flat alternative; callers can supply one explicitly with `flat_alt(space())` or `flat_alt(nil())`. Extend `ColumnState` to track deferred indentation, pending padding, and line freshness. Share these transitions between rendering and fitting so suppressed breaks do not terminate fit checks and committed padding participates in width decisions. Add `IndentationPolicy` to `RenderOptions` to choose when a structural break writes its indentation. `Eager` writes it right after the terminator, so blank lines and a terminal break carry trailing spaces; this stays the default for compatibility with upstream `pretty`. `Deferred` postpones indentation until content commits it, so blank lines and a terminal break emit none, a later break replaces pending indentation, and the end of output discards it. The policy changes only when indentation is written: the break itself, its indentation value, blank-line counts, fitting, union rollback, and contextual columns are unaffected, and explicit text and spaces stay literal. `OnColumn` callbacks and alignment observe the prospective column without forcing pending whitespace to be emitted. Treat pending suffixes as content without evaluating their documents during fitting. A suffix flush commits weak spaces even for an empty suffix. Suffix text remains excluded from group and partial-union fitting, while padding it commits counts toward the width. Suffixes combined with weak whitespace must not emit line breaks. Add regression tests for constructor consistency, explicit flat alternatives, empty text, zero-width Unicode content, indentation, contextual callbacks, suffix handling, and nested union rollback. Cover both indentation policies and assert they agree except on blank and terminal lines. Add benchmarks for weak lines with suffixes, accumulated weak spaces committed by text, and trailing weak spaces discarded at breaks or EOF.
QuadnucYard
force-pushed
the
weak-space-2
branch
from
October 3, 2026 08:50
4a8b1ac to
80a5bd4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
WeakSpaceandWeakLineprimitives with consistent constructorsacross allocator and document APIs.
weak_space()defers spaces after nonempty text. Consecutive weakspaces accumulate and are committed by following nonempty text or a
line-suffix flush. Leading spaces and uncommitted trailing spaces are
omitted; empty text does not commit them. A line-suffix flush commits
them by presence alone, without inspecting the suffix document.
weak_line()emits a break only when the current line containsnonempty text or a pending suffix. It has no flat alternative; callers
can supply one explicitly with
flat_alt(space())orflat_alt(nil()).Extend
ColumnStateto track deferred indentation, pending padding,and line freshness. Share these transitions between rendering and
fitting so suppressed breaks do not terminate fit checks and committed
padding participates in width decisions.
Add
IndentationPolicytoRenderOptionsto choose when a structuralbreak writes its indentation.
Eagerwrites it right after theterminator, so blank lines and a terminal break carry trailing spaces;
this stays the default for compatibility with upstream
pretty.Deferredpostpones indentation until content commits it, so blanklines and a terminal break emit none, a later break replaces pending
indentation, and the end of output discards it. The policy changes only
when indentation is written: the break itself, its indentation value,
blank-line counts, fitting, union rollback, and contextual columns are
unaffected, and explicit text and spaces stay literal.
OnColumncallbacks and alignment observe the prospective columnwithout forcing pending whitespace to be emitted.
Treat pending suffixes as content without evaluating their documents
during fitting. A suffix flush commits weak spaces even for an empty
suffix. Suffix text remains excluded from group and partial-union
fitting, while padding it commits counts toward the width. Suffixes
combined with weak whitespace must not emit line breaks.
Add regression tests for constructor consistency, explicit flat
alternatives, empty text, zero-width Unicode content, indentation,
contextual callbacks, suffix handling, and nested union rollback. Cover
both indentation policies and assert they agree except on blank and
terminal lines.
Add benchmarks for weak lines with suffixes, accumulated weak spaces
committed by text, and trailing weak spaces discarded at breaks or EOF.