Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alun opened this issue Jul 25, 2020 · 13 comments
Open

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

alun opened this issue Jul 25, 2020 · 13 comments
Labels
A-yew-macro Area: The yew-macro crate question

Comments

@alun
Copy link

alun commented Jul 25, 2020

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 alun added the question label Jul 25, 2020
@siku2
Copy link
Member

siku2 commented Jul 25, 2020

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.

@alun
Copy link
Author

alun commented Jul 25, 2020

I'm seeing some relevant discussion here rust-lang/rustfmt#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

@teymour-aldridge
Copy link
Contributor

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.

@alun
Copy link
Author

alun commented Aug 9, 2020

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

@teymour-aldridge
Copy link
Contributor

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.)

@VersBinarii
Copy link

VersBinarii commented Sep 8, 2021

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.

@mc1098 mc1098 added the A-yew-macro Area: The yew-macro crate label Sep 20, 2021
@g00nix
Copy link

g00nix commented Mar 20, 2022

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

@kantum
Copy link

kantum commented Jul 1, 2023

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

@ttax00
Copy link
Contributor

ttax00 commented Jul 7, 2023

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

@kantum
Copy link

kantum commented Jul 7, 2023

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

@Grandkahuna43325
Copy link

Grandkahuna43325 commented Dec 21, 2023

@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> 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 })

@ttax00
Copy link
Contributor

ttax00 commented Dec 27, 2023

@kantum there's feedback for your vim command

@emilien-jegou
Copy link

I created this plugin for yew code block formatting, it still has 1 or 2 caveats, but it seems to work fine on my code https://github.com/emilien-jegou/vim-yew-prettifier.

ps: it doesn't use prettier internally, the name just resonates better to me than "formatter"

demo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-macro Area: The yew-macro crate question
Projects
None yet
Development

No branches or pull requests

10 participants