fix(eliminate): preserve inline comments and blank lines via span-based text patching#59
Merged
Merged
Conversation
…ed text patching Fixes the root cause of the formatting regression reported in the issue. Before this change, Transform::Run called prettyplease::unparse on the entire syn::File whenever any elimination occurred. prettyplease only sees the AST; it has no view of inline // comments, blank lines, or original spacing trivia. Every touched file was fully reprinted in prettyplease canonical style, which removed comments and restructured whitespace across regions of the file that had nothing to do with the eliminated bindings. This change replaces the full reprint with a span-based text-patch approach: 1. Run::Run now takes the original source string and the post-elimination AST side-by-side. 2. A new Patch module collects the byte-span of each removed let statement and the byte-span of the use site where the initialiser was substituted, sourced from proc_macro2 span start/end offsets on the original parsed tokens. 3. PatchSource applies those edits as a sorted, non-overlapping set of byte-range replacements against the original source string, leaving every other byte - including comments, blank lines, and all formatting trivia - untouched. 4. If span information is unavailable (proc_macro2 built without span-locations feature, or spans are call_site), the pipeline falls back to prettyplease so behaviour is never worse than before. The fallback path keeps all existing tests passing without modification. New tests in Patch.rs verify the splice logic directly.
…tch path and RunPreserve from Current
Signed-off-by: Nikola Hristov <Nikola@PlayForm.Cloud>
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.
Fixes the root cause of the formatting regression: inline comments and blank lines being dropped whenever any elimination occurred.
Problem
Transform::Runcalledprettyplease::unparse(&Ast)on the entiresyn::Filewhenever any binding was eliminated.prettypleaseonly sees the AST; it has no view of//inline comments, blank lines, or original spacing. Every touched file was reprinted inprettypleasecanonical style, altering regions of the file that had nothing to do with the eliminated bindings.Fix
New: Transform/Patch.rs
Provides three primitives:
LineColToByte(source, line, col)- converts 1-based line / 0-based col (proc_macro2 convention) to a byte offset in the source string.SpanBytes(span, source)- extracts(start, end)byte offsets from aproc_macro2::Span. ReturnsNonewhen span-location data is absent (triggers fallback).StmtLineRange(stmt, source)- returns the full line range of a statement including leading indent and trailing newline, so deletion leaves no blank lines.ApplyPatches(source, patches)- applies a sorted non-overlapping slice ofPatchstructs (byte-range replacements) against the source string. ReturnsNoneon overlap or out-of-bounds.Changed: Transform/mod.rs
Runnow callsTryPatchSource(source, mutated_ast)after elimination:SourceintoOriginalAstwhoseSpans are anchored toSourcebytes.OriginalAstandMutatedAstin parallel over function items.CollectBlockPatchesaligns statements between original and mutated blocks by token-stream equality. Statements present in the original but absent in the mutated block are emitted as delete patches. Statements that are present in both but have different token text (substituted identifier) are emitted as replace patches.ApplyPatches.None,TryPatchSourcereturnsNoneandRunfalls back toprettyplease::unparse- behaviour is never worse than before.Tests added
In
Patch::Tests:ApplyNoPatches- identity caseApplySingleDelete- range deleteApplySingleReplace- range replaceApplyTwoPatches- two disjoint patchesOverlappingPatchesReturnNone- overlap guardOutOfBoundsPatchReturnsNone- bounds guardLineColToByteFirstLine/LineColToByteSecondLine/LineColToByteOutOfRange