yew icon indicating copy to clipboard operation
yew copied to clipboard

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

Open alun opened this issue 5 years ago • 13 comments

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

alun avatar Jul 25 '20 09:07 alun

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.

siku2 avatar Jul 25 '20 13:07 siku2

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

alun avatar Jul 25 '20 17:07 alun

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.

teymour-aldridge avatar Jul 25 '20 17:07 teymour-aldridge

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

alun avatar Aug 09 '20 09:08 alun

I've produced a crate called Malvolio (available on crates.io) which allows you to construct HTML trees using the builder pattern and use them inside Yew components. Because the API is macro-free it works with rustfmt.

It can be incrementally adopted but is currently lacking support for child components (which I intend to add in a future release) and listeners (support for those is also planned.)

teymour-aldridge avatar Jan 23 '21 23:01 teymour-aldridge

In vim i have following:

" Format selected block of HTML code
noremap <leader>f :'<,'>! prettier --parser html --stdin-filepath<cr>

This allows me to format the block in the html macro. This can be added to almost any other editor. The only thing is that it has to be run manually.

VersBinarii avatar Sep 08 '21 12:09 VersBinarii

In vim i have following:

" Format selected block of HTML code
noremap <leader>f :'<,'>! prettier --parser html --stdin-filepath<cr>

This allows me to format the block in the html macro. This can be added to almost any other editor. The only thing is that it has to be run manually.

lua version for neovim:

vim.api.nvim_set_keymap( 'n', '<F7>', 'vi{:! prettier --parser html --stdin-filepath<CR>vi{>', {noremap = true})

what this does:

  • selects code block between curly brackets
  • uses prettier to format it nice
  • selects code block between curly brackets (again)
  • indents once

advantages: touching F7 within the html macro will prettify html code disadvantages: touching F7 anywhere else will force you to press u problems: does not work with all code blocks; it gets confused oh yew-specific stuff

g00nix avatar Mar 20 '22 02:03 g00nix

Here is a version that find the html! macro and format the following code block:

vim.cmd.nnoremap('<leader>h', ':vimgrep /\\Vhtml\\!/ % | normal jvi} <Esc>:!prettier --parser html --stdin-filepath<CR>vi}>')

I failed to make it work on any rust file we can use it with autocmd but I'm sure someone can :)

BTW switch to lua

kantum avatar Jul 01 '23 18:07 kantum

Here is a version that find the html! macro and format the following code block:

vim.cmd.nnoremap('<leader>h', ':vimgrep /\\Vhtml\\!/ % | normal jvi} <Esc>:!prettier --parser html --stdin-filepath<CR>vi}>')

Btw are you sure prettier html parser doesn't conflict with some of yew's syntaxes like dynamic tags? @kantum

ttax00 avatar Jul 07 '23 05:07 ttax00

@TechTheAwesome No idea, hopefully if someone finds out they will tell us here :)

kantum avatar Jul 07 '23 08:07 kantum

@TechTheAwesome No idea, hopefully if someone finds out they will tell us here :)

Well about that.... @ttax00 your solution works only on the first html! macro(or I'm stupid, both could be correct)

prettier struggles with: input oninput={rust_function} => input oninput="{rust_function}" Can't process </> and gives errors Can't process nested tags like: <Link<Route>> gives errors Changes {false} to "false" (it causes errors in trunk) Changes Nav / to nav/ (when you have custom struct component called Nav it messes it up) messes up indentation relatively to other rust code(that one was pretty obvious and I don't mind it that much) and probably many others

for the <> MAYBE something like this would work: in lua

vim.api.nvim_set_keymap("n", "<leader>h=",    "<cmd> execute '%s/<>/<diamond_tag>/g' | execute '%s#</>#</diamond_tag>#g' <CR> | vi{:! prettier --parser html --stdin-filepath<CR>vi{>:%s/<diamond_tag>/<>/g | %s#</diamond_tag>#</>#g <CR>", { noremap = true, silent = true })

Grandkahuna43325 avatar Dec 21 '23 14:12 Grandkahuna43325

@kantum there's feedback for your vim command

ttax00 avatar Dec 27 '23 13:12 ttax00