bram209 / leptosfmt

A formatter for the leptos view! macro
Apache License 2.0
272 stars 29 forks source link

Clarification needed on recent PR for `macro_names` parameter #114

Closed carlca closed 8 months ago

carlca commented 8 months ago

I would really like to use your leptosfmt to reformat yew files containing html! macros. I tried it out with a leptosfmt.toml file containing...

max_width = 100
tab_spaces = 2
attr_value_brace_style = "WhenRequired" # "Always", "AlwaysUnlessLit", "WhenRequired" or "Preserve"
macro_names = ["yew::html, html"]       # Macro names which will be formatted

but the source doesn't seem to change. This is the source I am trying to reformat...

use yew::prelude::*;

#[function_component]
fn App() -> Html {
  let counter = use_state(|| 0);
  let onclick = {
    let counter = counter.clone();
    move |_| {
      let value = *counter + 1;
      counter.set(value);
    }
  };

  html! {
      <div>
          <button {onclick}>{ "+1" }</button>
          <p>{ *counter }</p>
      </div>
  }
}

fn main() {
  yew::Renderer::<App>::new().render();
}

Any ideas?

bram209 commented 8 months ago

It takes a list of identifiers, so change macro_names = ["yew::html, html"] to macro_names = ["yew::html", "html"]

Note that it is not guaranteed to work with yew's macro at all unless they use the rstml library (or the exact same syntax which is unlikely). You can read the syntax here: https://github.com/rs-tml/rstml, if it is not parsable by rstml's parser, it won't format.

If you decide to give it a try, let me know how it goes!

carlca commented 8 months ago

Thanks for the reply! As it turns out, there is a tool called yew-fmt which does exactly what I need (after wrapping it in a crafty shell script to work recursively through a project's Rust files)! https://crates.io/crates/yew-fmt