Repository navigation
fix(lsp): keep unparsable text inside a directive when range formatting - #2837
Merged
Merged
Conversation
The LSP range-format fallback (files with parse errors) rebuilt each directive in the selection from its typed values, so text the parser could not read into one (`junk` after a header, after a posting amount, inside a cost) vanished from the edit along with its parse error. A new `format::format_parsed_range` takes the parse result and builds the same `ErrorRanges` the whole-file error-tolerant formatter uses (#2794); a directive overlapping a parse error is copied verbatim. The per-node emission is now one `NodeEmitter` shared by the whole-file and range walks, so the two cannot disagree on which directives they rebuild. Also corrects doc comments that said whole-file formatting drops ERROR_NODE content (it copies it verbatim since #1335). Closes #2803 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…ay quote - The range walk now tracks a top-level WHITESPACE token before a node, as the whole-file walk does, so a verbatim-copied indented directive keeps its indentation (#2786). Dropping it also removed the indentation parse error, so a second pass reformatted the line. - The line-ending helpers (lf_to_crlf_outside_strings and crlf_to_lf_outside_strings) read a `"` as the lexer does: one that no later unescaped quote closes, or that a `\` before a line break stops, is an error token cut at its own line (#2788), not a string. A stray quote copied into a CRLF edit left every later line LF. A failed scan is remembered so many stray quotes stay linear. The lexer-agreement drift guard now runs the same step and has two unterminated-quote fixtures. - Corrects two stale comments that said whole-file formatting drops ERROR_NODE content. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
- cr_outside_strings_present now reads quotes through lexer_source_state, like the two helpers it mirrors, so a stray quote no longer hides the `\r` after it. - A fixture with a later quote (`"a\` + newline, then `"x"`) pins the `\`-before-line-break rule in the lexer-agreement drift guard and the LF->CRLF helper test; an unconditional escape now fails both. - A linear-cost guard counts the bytes quote scans step over (one stray quote then 20k `\"` lines): without the memo it is 400M bytes for 60k. - A test pins the error-tolerant formatter folding a bare `\r` after a stray quote into a line break. - CHANGELOG: name the affected paths precisely (LSP range edits; format_source behind WASM/FFI for bare `\r`), not WASM/FFI CRLF. 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
LSP range formatting on a file with parse errors (the CST-snap fallback) rebuilt every directive in the selection from its typed values. Text the parser could not read into one, such as
junkafteropen Assets:A, after a posting amount, or inside a cost, was not part of any value, so it vanished from the edit along with its parse error:2024-01-01 open Assets:A junk2024-01-01 open Assets:AAssets:A 1 USD junkin a transactionjunk2024-01-02 open Assets:Bnext to itformat::format_parsed_range(parse_result, range, style)builds the sameErrorRangesthe whole-file error-tolerant formatter uses (fix(format): the error-tolerant formatter deletes junk on the same line after a directive #2794) from the parse result's own errors, and copies a directive that overlaps one verbatim. It reuses the cached alignment under the default style and measures it under a grouping style, which is what the LSP handler did by hand before.NodeEmittershared by the whole-file walk and the range walk, so the two cannot disagree on which directives they rebuild (canonical-function discipline).None) when the snap covers a top-levelERROR_NODE, as before.format.rsand the LSP handler said whole-file formatting "silently dropsERROR_NODEcontent"; it has copied it verbatim since [Compat]: rledger format deletes org-mode (*) headers and comments grouped with them #1335. Corrected.Not affected: whole-document LSP formatting (falls back to surface cleanup on parse errors), on-type formatting (its own line-local edits), WASM and the FFI component (no range entry point; their whole-file path is
format_source, fixed in #2801). No parse output changes, so no cache bump.How to test
cargo test -p rustledger-parser --test range_format_keeps_error_text: for five broken shapes (junk after a header, after a posting, in a cost, a stray quote, junk at EOF without a newline) range formatting keeps every non-whitespace byte, still formats the neighbors, is idempotent, and over the whole file equalsformat_sourcebyte for byte (drift guard). Withformat_parsed_rangeignoring the errors, 4 of the 5 fail.cargo test -p rustledger-lsp --lib parse_errors_inside_a_directive_keep_their_text_in_the_fallback_edit: the LSP handler keepsjunkin the edit. Without the fix it fails (2024-01-01 open Assets:Ainstead of2024-01-01 open Assets:A junk).cargo test -p rustledger-parser -p rustledger-lsp --all-features, clippy-D warnings, rustdoc-Dwarnings,cargo fmt --checkall pass.Closes #2803
Review round 1 (ced349f)
"; c\n 2024-01-01 open Assets:A junk\n..."keeps its two-space indent (it used to drop it, and in"; c\n 2024-01-01 open Assets:A\n...junk\n"dropping it also removed the indentation's parse error, so a second pass reformatted). Both inputs are in the whole-file drift-guard test.lf_to_crlf_outside_strings(andcrlf_to_lf_outside_strings) treated every"as opening a string, so a copied stray quote left all later lines LF in a CRLF edit. They now read a quote as the lexer does: one that no later unescaped quote closes, or that a\before a line break stops, ends at its own line (fix(parser): end an unterminated quote at its own line #2788). A failed scan is remembered, so many stray quotes stay linear. (Only the LSP range handler re-injects CRLF on a file with parse errors; WASM and FFI return LF.) The lexer-agreement drift guard runs the same step and has two unterminated-quote fixtures. CRLF cases are tested at the helper, range and LSP levels.ERROR_NODEcontent are corrected.format.rsat the previous commit, 3 of the 7 parser tests and the LSP test fail; all pass now.Review round 2 (73628ef)
cr_outside_strings_presentnow reads quotes through the samelexer_source_statestep aslf_to_crlf_outside_stringsandcrlf_to_lf_outside_strings, so all line-ending walkers share one quote rule (a stray quote no longer hides the\rafter it)."a\then a line break, then"x") pins the\-before-line-break rule in the lexer-agreement drift guard and the LF to CRLF helper test; replacing the rule with an unconditional escape now fails both. (In a CRLF file a\before\r\nescapes the\r, since the lexer's.matches it, so that shape pairs with the later quote there, in the lexer and the helper alike.)\"lines: within 2x the input with the memo, 400,040,001 bytes for a 60,027-byte input without it.format_source(the error-tolerant formatter behind the WASM and FFI component format calls): a bare\r(classic-Mac) line ending after a stray quote is now folded into a line break, so the directive after it is read, instead of being kept as if inside a string. Pinned by a test; the CHANGELOG now names exactly these paths.-D warnings, rustdoc-Dwarnings,cargo fmt --check.🤖 Generated with Claude Code