What is the recommended way to format html and rust code within the html! macro invocation?
rustfmt 1.4.14 - doesn't (re)format anything within html! by default
Sadly Rust doesn't provide a way for macros to format the code they're given.
The only option right now is to format the code by hand.
For the Rust code within the macro there's a trick you can use that might help. You can copy the code out of the macro to a normal function anywhere in your code and run cargo fmt. Of course you won't be able to compile this code but rustfmt can still format it.
In the future we might provide an IDE plugin to do this.
I'm seeing some relevant discussion here https://github.com/rust-lang/rustfmt/issues/3434
I looks like people suggest to add
// Skip formatting everything whose name is `html`
#![rustfmt::skip(html)]
// Or make it explicit that you only want to skip macro calls
#![rustfmt::skip::macro(html)]
With my current rustfmt version skip looks to be a default behaviour. Maybe there is a way to enable it explicitly via config.
Although I don't see anything related in the doc https://rust-lang.github.io/rustfmt/?version=master&search=macro
The problem is that rustfmt can format Rust programs, but it has no idea as to how to format the contents of procedural macro invocations.
One nice (more or less) workaround I've found : format selection as html msvsc plugin https://marketplace.visualstudio.com/items?itemName=adrianwilczynski.format-selection-as-html
Most helpful comment
Sadly Rust doesn't provide a way for macros to format the code they're given.
The only option right now is to format the code by hand.
For the Rust code within the macro there's a trick you can use that might help. You can copy the code out of the macro to a normal function anywhere in your code and run
cargo fmt. Of course you won't be able to compile this code but rustfmt can still format it.In the future we might provide an IDE plugin to do this.