Repository navigation
fix(format): keep a trailing comment outside a multi-line string - #3048
Merged
Merged
Conversation
The formatter spliced a directive's same-line trailing comment before the first `\n` of the emitted directive. When the header holds a multi-line string (narration, payee, query, custom, note, event, document, option, plugin, include), that `\n` is inside the string, so the comment became part of the string's value. The posting splice had the same bug with a multi-line cost label. Both splices now find the first `\n` outside a string literal, read by the lexer through `StringLiterals`. The LSP's document and range formatting share the emitter and are fixed with it. Adds `format_compat` fixtures for every shape, LSP tests, and a drift guard (`format_preserves_directives`) asserting that formatting never changes the parsed directives, options, includes or plugins. Closes #3046 Co-Authored-By: Claude Opus 5.5 <[email protected]>
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✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
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
rledger formatmoved a same-line trailing;comment into a multi-line string when the string's closing line held the comment, silently changing the string's value (#3046). This keeps the comment after the closing quote, as bean-format 3.2.3 does.Cause
emit_directiveincrates/rustledger-parser/src/cst/format.rsspliced the trailing comment before the first\nof the emitted directive. With a multi-line string in the header, that\nis inside the string.emit_postinghad the same splice and the same bug with a multi-line cost-spec label ({10 EUR, "lab\nel"}).Fix
New
first_line_end_outside_stringsfinds the first\noutside a string literal using the existing lexer-backedStringLiterals(the same oracle the CRLF helpers use), and both splices use it.Shapes fixed (all verified against bean-format 3.2.3, apart from rledger's existing single-space comment canonicalization): transaction narration, payee, narration followed by tags/links,
queryname/string,customname/value,note,eventtype/description,document,option,pluginconfig,include, and a posting's cost label. Metadata values (directive, transaction, posting) were already correct, and fixtures now pin them.The LSP's document formatting and range formatting share the emitter, so they are fixed too. WASM
formatshares it as well; formatter output is not in any cached payload, soCACHE_VERSIONis unchanged.Tests
format_compatfixturesmultiline_string_trailing_comment_{narration,payee,query,custom,note_event_document,option_plugin_include,metadata,cost_label}. The narration fixture is added toREQUIRED_FIXTURES.tests/format_preserves_directives.rs: formatting must leave the parsed directives, options, includes and plugins unchanged and must be idempotent. It runs over every cleanformat_compatinput, a fixed sweep of every string slot × comment variants × multi-line bodies, and a proptest of random bodies ([a-z ;#\n]). With the fix reverted it fails on every affected shape.scripts/format-preserves-comments.shcould not catch this because the comment text survived inside the string.test_handle_formatting_trailing_comment_after_multiline_string(LF and CRLF) andtest_handle_range_formatting_trailing_comment_after_multiline_string. Both fail without the fix.How to test
Closes #3046
🤖 Generated with Claude Code