Yew: Recommended way to format html and rust code within the `html!` macro

Created on 25 Jul 2020  路  4Comments  路  Source: yewstack/yew


Question

What is the recommended way to format html and rust code within the html! macro invocation?

What I've tried (optional)

rustfmt 1.4.14 - doesn't (re)format anything within html! by default

question

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.

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Boscop picture Boscop  路  4Comments

wldcordeiro picture wldcordeiro  路  4Comments

sackery picture sackery  路  3Comments

ghost picture ghost  路  5Comments

nixpulvis picture nixpulvis  路  4Comments