Skip to main content

Crate source_edit

Crate source_edit 

Source
Expand description

Surgical source rewriting: replace exactly one syn::Item’s byte span with a re-printed, mutated version of itself, leaving the rest of the file byte-identical.

taint-generate and taint-refactor both need to add an attribute (or insert a call) to one specific fn/mod inside a real source file without disturbing anything else in it. A full syn::parse_file + prettyplease::unparse round-trip of the whole file would work, but it silently reformats every other item and strips blank-line structure wherever syn doesn’t preserve it — an unacceptable side effect for a tool whose entire job is “add one attribute, touch nothing else”. This crate instead: (1) locates the exact byte range of the one item being changed via span_byte_range (using proc_macro2’s span-locations line/column data, since this always runs outside a real proc-macro), (2) re-prints just that mutated item alone via prettyplease (print_item), and (3) splices it back into the original text (apply_edits). Everything outside a changed item’s own span is untouched; the changed item itself may come out less indented than its surrounding context if it was nested (prettyplease always prints from column zero) — recommend running cargo fmt after applying edits.

Structs§

SourceEdit
One pending replacement: swap the original source’s [start, end) byte range for replacement.

Functions§

apply_edits
Apply every edit to source, returning the rewritten text.
byte_offset
Convert a 1-indexed line number and 0-indexed, char-counted column (proc_macro2::LineColumn’s own convention) into a byte offset.
parse_attribute
Parse a single standalone #[...] outer attribute from text.
print_item
Pretty-print item alone (wrapped in a bare, attribute-less syn::File) via prettyplease, trimmed of its trailing newline so callers can splice it directly into a SourceEdit::replacement.
span_byte_range
The [start, end) byte range span covers in source.