fix(autofmt): preserve indentation inside multiline string literals#5659
fix(autofmt): preserve indentation inside multiline string literals#5659devin-ai-integration[bot] wants to merge 1 commit into
Conversation
Co-Authored-By: Nico Burns <nico@nicoburns.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
We should avoid the issue in the first place instead of fixing it after we already format the code |
|
Agreed that avoiding the corruption is nicer than repairing it — let me lay out what I found so we pick the right spot. The string bytes get mangled by two independent, textual, line-by-line re-indent passes, and the offending literal usually lives inside a macro token stream (e.g. the
So there's no single "format step" to guard; the value passes through both. To truly avoid touching the bytes, the clean approach is mask-then-restore, mirroring the existing The current PR instead makes those two passes literal-aware (skip whitespace edits on lines that begin inside a string literal), which keeps prettyplease's wrapping faithful to the real content and is fully contained. Happy to switch to the mask-then-restore version if you prefer that as the canonical fix — or if you had a specific location in mind for the guard, point me at it and I'll move it there. |
Summary
Fixes #5648.
dx fmtwas rewriting the interior of multiline string literals in rsx expressions (e.g. aformat!("...\n ...")used as an attribute value), silently changing the string's value and shifting it further right (or left) on every format pass.Two independent line-based transforms in
dioxus-autofmtwalked the pretty-printed expression line-by-line and blindly adjusted leading whitespace, without accounting for lines that fall inside a string literal:writer::write_mulitiline_tokensadded the current indent to every non-blank line → string grew right each pass.prettier_please::unwrappedstripped the 4-spacefn main() {}wrapper indent from every line → string shrank left each pass.Fix: added a small string-literal state scanner and skip whitespace edits on lines that begin inside a string literal, so string contents are emitted verbatim.
The reported input is now idempotent under
dx fmt(both a normal and a raw multiline string are preserved exactly):Testing
twoway!regression fixturemultiline-string(tests/wrong/multiline-string.{wrong.,}rsx, identical files → asserts idempotency) covering both a normal and a raw multiline string literal.cargo test -p dioxus-autofmt(all samples + wrong-tests) andcargo clippy -p dioxus-autofmtpass.Link to Devin session: https://app.devin.ai/sessions/9f5bdce719284acfb3022140fe900863
Requested by: @nicoburns