Repository navigation
fix(format): align amounts by display width - #2869
Merged
Merged
Conversation
The formatter padded the account field by character count, so a posting to `Expenses:생활` (two characters, four cells) put its amount two columns right of the others in a monospaced editor. A new `format::display_width` (unicode-width: East Asian Wide/Fullwidth 2 cells, combining marks 0, ambiguous 1) is now the one width rule for the alignment pre-pass, the posting emitter, and the LSP inferred-amount inlay hint that places text against the formatter's column. ASCII-only files are unchanged. This deliberately differs from bean-format 3.2.3, which pads by code points; documented in compatibility.md section 19 and pinned by tests/format_display_width.rs. Over 1,844 in-tree and corpus files only two change, both with wide characters in an account name. Closes #2852 Co-Authored-By: Claude Opus 5.5 <[email protected]>
… two doc claims Merges main (CHANGELOG conflict with #2863: both entries kept). Review of #2869: a test now covers the inlay-hint hunk (explicit `Expenses:Restaurants -50.00 USD`, elided `Assets:생활`: UTF-16 position 11 and a 12-space gap; counting characters gave 14). The tab comment said unicode-width gives a tab 0 cells; it gives 1. compatibility.md section 19 now says ASCII-only amount alignment is unchanged and matches bean-format, instead of claiming whole-file parity. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…h' into fix/format-align-by-display-width
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…isplay-width # Conflicts: # CHANGELOG.md
Review round 2 of #2869. bean-format 3.2.3 does not pad `Expenses:생활` by code points: its alignment regex needs each account component to start with an uppercase letter or a digit, so that line is left as written, and only a line that matches (`Expenses:X생활`, fullwidth `Food`) is padded by code points. Verified with bean-format. Corrected in compatibility.md section 19, the display_width doc, the format_display_width.rs header and the CHANGELOG, which also no longer claim ASCII-only alignment matches bean-format (it also aligns dated balance and price lines). Also gives the #1346 inlay test its doc comment back (now in display cells), renames end_col_chars to end_col_cells, and reverts the bot's unrelated utf8_iter imports.lock churn (cargo vet --locked passes either way). Co-Authored-By: Claude Opus 5.5 <[email protected]>
bean-format 3.2.3 matches a precomposed Expenses:Café (U+00E9) and aligns it as rledger does; only a decomposed é (U+0301) is left as written. Its regex also needs every character to be a letter, digit or hyphen, so an emoji or combining mark anywhere in the account leaves the line as written, while the transaction's other matching postings are still realigned among themselves. Verified with bean-format. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…isplay-width # Conflicts: # CHANGELOG.md
…column Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
What changed
format::display_width(unicode-width) gives the number of terminal cells a string takes: East Asian Wide and Fullwidth characters 2, combining marks and zero-width characters 0, ambiguous-width characters 1 (as most terminals render them), and emoji asunicode-widthgives them (2 for emoji-presentation characters).compute_alignment);emit_posting), which also serves LSP document and range formatting through the shared emitter;number_col. Its indent is still counted per character, as before (unicode-width gives a tab one cell, which is no closer to how a terminal renders it, so the count is left alone).unicode-width0.2 becomes a workspace dependency. It was already inCargo.lock(0.2.2) through other crates, but it is new in therustledger-wasm(wasm32) andrustledger-ffi-component(wasip2) build graphs. A minimal wasm32 cdylib probe grew by about 30 KB, the width tables; the WASM size gate warns only above 2 MB. cargo-vet covers it (Mozilla wildcard audit),deny.tomlalready skips it for its two versions, andcargo deny checkreports advisories, bans, licenses and sources all ok.ParseResult::alignmentis not archived in the loader or WASM caches.Why
The formatter padded the account field by character count, so a posting to
Expenses:생활(two characters, four cells) put its amount two columns right of the others in a monospaced editor (#2852).A deliberate difference from bean-format
bean-format 3.2.3 never lines these amounts up. Its alignment regex is
[\p{Lu}][\p{L}\p{Nd}\-]*(?::[\p{Lu}\p{Nd}][\p{L}\p{Nd}\-]*)+followed by whitespace: each component after the root must start with an uppercase letter or a digit, and every character must be a letter, a digit or a hyphen. So:Expenses:생활orExpenses:食品(a component starting with a letter that has no case), or to an account with an emoji (Assets:CORP✨) or a combining mark anywhere (Expenses:Cafefollowed by U+0301, a decomposedé), does not match, and bean-format leaves its spacing as written, while still aligning every matching line, in that transaction and elsewhere in the file, to its one file-wide column. A precomposedExpenses:Café(U+00E9) does match, andéis one cell, so bean-format and rledger write it identically;Expenses:X생활, fullwidthExpenses:Food) is padded by code points (Python'slen()), so the wide characters put its amount further right on screen: bean-format writesExpenses:X생활 12345 KRWaboveAssets:Bank -12345 KRW, two cells off. Emoji and combining marks never reach this case.Both checked with bean-format in the dev shell. rledger lines the amounts up on screen in both cases. For ASCII-only text, amount alignment is unchanged by this PR. It still differs from bean-format's in other ways: bean-format also aligns dated
balanceandpricelines, which widen the posting column, and leaves other spacing as written. This is documented indocs/reference/compatibility.mdsection 19 and atdisplay_width.Which files change
I ran
rledger formatfrom a main build and from this branch (separate target dirs; the binaries differ) over all 1,844 in-tree.beancount/.beanfiles and the compatibility corpus. Only two files change, both with a wide character in an account name:double-entry-generator/example_hxsec_example-hxsec-output.beancount(Assets:Hxsec:Positions:沪深300): its amounts shift right by two cells, 35 lines.fava-portfolio-returns/src_fava_portfolio_returns_test_ledger_unicode.beancount(Assets:CORP✨): one line shifts by one cell.The corpus baselines record parser output, not formatter output, so none move.
How to test
cargo test -p rustledger-parser --test format_display_widthruns 4 tests:display_widthcell counts, including an ambiguous-width character.The parser, LSP, CLI and WASM suites pass (91 result lines), as do format_compat, the idempotence matrix and the range-vs-whole-file drift guard. Workspace clippy, rustdoc and fmt are clean.
Review round 1
test_inlay_hint_wide_account_pads_by_display_width_2852: an explicitExpenses:Restaurants -50.00 USDand an elidedAssets:생활give the hint UTF-16 position 11 and a 12-space gap. It fails with the inlay-hint change reverted (counting characters gives 14).compatibility.mdsection 19 no longer claims ASCII-only files format exactly as bean-format does (rledger also normalizes other spacing, such as2021-01-01 open Assets:Bank USD); it says amount alignment for ASCII-only text is unchanged and matches bean-format's.Review round 2
Expenses:생활as written and pads only matching lines by code points. Fixed in compatibility.md section 19, thedisplay_widthdoc, theformat_display_width.rsheader, the CHANGELOG and this description.balanceandpricelines).end_col_charsis renamedend_col_cells.utf8_iteraudit churn insupply-chain/imports.lock:cargo vet --locked(0.10.2) passes with and without it (312 fully audited, 201 exempted).Review round 3
Expenses:Caféexample: a precomposedématches bean-format's regex and aligns identically in both tools; only the decomposed form (Cafe+ U+0301) is left as written. The example now names the decomposed form explicitly.display_width's doc, the test header and the CHANGELOG.Caféaligned (and byte-identical to rledger's output), decomposedCaféandCORP✨left as written,Expenses:생활left as written whileAssets:Bank/Assets:Cashwere still aligned to the file-wide column.Closes #2852
🤖 Generated with Claude Code
Review round 4
Expenses:食品in section 19, where a Hangul품had slipped in.format_display_width.rs.