Skip to content

fix(format): keep range formatting on a top-level comment idempotent - #2863

Merged
robcohen merged 2 commits into
mainfrom
fix/range-format-leading-comment-idempotent
Nov 5, 2026
Merged

robcohen merged 2 commits into
mainfrom
fix/range-format-leading-comment-idempotent

Conversation

@robcohen

@robcohen robcohen commented Nov 5, 2026

Copy link
Copy Markdown
Member

What changed

format_node_range_with_style: when the snapped range ends on a top-level comment token, the snap now takes that comment's own NEWLINE. The snap also uses the canonical convert::is_comment_kind instead of a hand-written list of comment kinds.

Why

A directive owns the NEWLINE that ends it, but a top-level comment token does not, and the emitter ends the comment's line with one. Selecting a file-leading comment snapped to 0..3 (; c), and the replacement was ; c\n, so every format-selection added a blank line after the comment. That happened on clean files too.

How to test

cargo test -p rustledger-parser --test range_format_comment_idempotent (2 tests, both fail on main):

  • Five shapes: a clean file, a blank line after the comment, a parse error later in the file, two comment lines, and a comment between directives. Range-formatting the comment's line leaves each file unchanged, on the first run and the second.
  • A comment at the end of the file with no newline gains one final newline, once.

The parser and LSP suites pass, including the range-vs-whole-file drift guard from #2837.

Closes #2838

🤖 Generated with Claude Code

A directive owns the NEWLINE that ends it; a top-level comment token does
not, while the emitter ends the comment's line with one. The range snap
stopped at the comment token, so each run replaced `; c` with `; c\n`
and a file-leading comment gained a blank line after it every time. The
snap now takes the comment's own NEWLINE. The snap also uses the
canonical `is_comment_kind` instead of a hand-written list.

Closes #2838

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner November 5, 2026 04:01
@codecov

codecov Bot commented Nov 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 49.9ms ± 0.4ms 822.7ms ± 3.7ms 16.5x 34.7 MB / 51.1 MB
investment 76.9ms ± 1.0ms 2981.9ms ± 13.0ms 38.8x 44.7 MB / 59.0 MB
fifo 75.2ms ± 2.6ms 14610.8ms ± 22.6ms 194.2x 40.8 MB / 59.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 49.7ms ± 0.5ms 49.9ms ± 0.4ms +0.4% ✅ within noise 33.4 MB → 34.7 MB (+3.9%)
investment 75.9ms ± 0.9ms 76.9ms ± 1.0ms +1.4% ✅ within noise 44.5 MB → 44.7 MB (+0.5%)
fifo 74.1ms ± 1.2ms 75.2ms ± 2.6ms +1.5% ✅ within noise 40.8 MB → 40.8 MB (-0.2%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.3ms, investment 0.6ms, fifo 1.3ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

github-actions Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 826
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

@robcohen
robcohen enabled auto-merge (squash) November 5, 2026 05:12
@robcohen
robcohen merged commit c0956b9 into main Nov 5, 2026
60 of 61 checks passed
@robcohen
robcohen deleted the fix/range-format-leading-comment-idempotent branch November 5, 2026 05:28
robcohen added a commit that referenced this pull request Nov 5, 2026
… 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]>
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.

fix(lsp): range-formatting a file-leading comment adds a blank line on every run

1 participant