-
Notifications
You must be signed in to change notification settings - Fork 0
Switch wrapping to textwrap with adapter and unified prefixes #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
96fe341
docs(execplans): add execplan for replacing bespoke wrapping with tex…
leynos 3a51831
feat(wrap): replace bespoke wrapping engine with textwrap integration
leynos e826392
docs(wrapping): add detailed flow diagrams and explanation for wrap_text
leynos 6ce5bc8
fix(wrap): prevent line overflow after tail rebalancing
leynos acb7a03
feat(wrap): add tests and refine inline wrapping post-processing
leynos 4169227
feat(wrap): replace custom LineBuffer with textwrap for paragraph wra…
leynos 665ab56
feat(wrap): replace bespoke wrapping with textwrap and unicode-width
leynos 59115d7
docs(users-guide): fix punctuation in documentation for clarity
leynos 545e5b5
refactor(tokenize): refactor tokenize_markdown and add fence utils re…
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Architecture Decision Record (ADR) 0002: Delegate line fitting to `textwrap` | ||
|
|
||
| - Status: Accepted | ||
| - Date: 2026-04-22 | ||
|
|
||
| ## Context | ||
|
|
||
| The previous wrapping engine in `src/wrap/line_buffer.rs` implemented a bespoke | ||
| `LineBuffer` struct that accumulated tokens, tracked a split-point cursor, and | ||
| flushed completed lines one at a time. This approach had three compounding | ||
| problems: | ||
|
|
||
| - Width measurement was byte-based in early versions, producing incorrect splits | ||
| for non-ASCII characters such as CJK glyphs and emoji. | ||
| - The split-with-carry logic required carefully coordinated state between | ||
| `push_span`, `split_with_span`, and `flush_trailing_whitespace`, making the | ||
| code difficult to reason about and extend. | ||
| - Each fragment addition triggered a full re-evaluation of the buffer, risking | ||
| quadratic behaviour on long paragraphs. | ||
|
|
||
| ## Decision | ||
|
|
||
| Replace `LineBuffer` with `textwrap::wrap_algorithms::wrap_first_fit` and a | ||
| fragment model built on the `textwrap::core::Fragment` trait. Each token group | ||
| becomes an `InlineFragment` that carries pre-computed display width (via | ||
| `unicode-width`) and a `FragmentKind` tag. `wrap_first_fit` performs greedy | ||
| line fitting over the fragment slice; post-processing in | ||
| `src/wrap/inline/postprocess.rs` normalizes whitespace-only lines and | ||
| rebalances atomic tails. Prefix handling is centralized in | ||
| `ParagraphWriter::wrap_with_prefix`, which computes available width once and | ||
| prepends the correct prefix to every wrapped output line. | ||
|
|
||
| The greedy first-fit algorithm is chosen over `textwrap`'s optimal-fit | ||
| algorithm because the optimal algorithm may produce non-local changes to | ||
| earlier lines when a later fragment is added, which conflicts with the | ||
| incremental buffer model and produces surprising diffs. | ||
|
|
||
| ## Consequences | ||
|
|
||
| Positive: | ||
|
|
||
| - Line fitting is delegated to a well-tested upstream crate; the bespoke split | ||
| logic and `LineBuffer` state machine are removed entirely. | ||
| - Display widths are computed by `unicode-width` according to Unicode Standard | ||
| Annex `#11`, giving correct column counts for non-ASCII text. | ||
| - `InlineFragment::kind` centralizes token classification, so post-processing | ||
| predicates (`is_whitespace`, `is_atomic`, `is_plain`) do not repeat | ||
| classification logic. | ||
|
|
||
| Negative: | ||
|
|
||
| - Greedy first-fit produces wider first lines than optimal-fit would in some | ||
| cases, though this difference is not visible in standard Markdown prose. | ||
| - The project now depends on `textwrap 0.16` in addition to `unicode-width`. | ||
|
|
||
| ## Alternatives considered | ||
|
|
||
| - **Optimal-fit algorithm** (`textwrap::wrap_algorithms::wrap_optimal_fit`): | ||
| rejected because it requires the complete fragment list upfront and may | ||
| redistribute earlier lines when later fragments are added, which conflicts | ||
| with the streaming model. | ||
| - **Patching `LineBuffer` for Unicode correctness**: rejected because the | ||
| split-point cursor and carry semantics remained inherently fragile; the | ||
| maintenance burden outweighed the risk of introducing a new dependency. | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.