bram209 / leptosfmt

A formatter for the leptos view! macro
Apache License 2.0
274 stars 30 forks source link

Don't format code inside macros that look like functions #77

Closed mondeja closed 1 year ago

mondeja commented 1 year ago

Currently, leptosfmt makes really strange changes to the "arguments" passed to macros that look like functions:

- <Link rel="canonical" href=format!("https://{}/", &domain)/>
+ <Link rel="canonical" href=format!("https://{}/", & domain)/>

The problem becomes much worse when these macros that look like functions have complex code, for example:

-                    title=move_tr!("download-filetype", &{
-                        let mut map = HashMap::new();
-                        map.insert("filetype".to_string(), tr!("svg").into());
-                        map
-                    })
+                    title=move_tr!(
+                        "download-filetype", & { let mut map = HashMap::new(); map.insert("filetype"
+                        .to_string(), tr!("svg") .into()); map }
+                    )
+

I think it would make sense to ignore anything inside a macro that looks like a function.

bram209 commented 1 year ago

Hi, can you try again with the latest version0.1.15? It includes some enhancements on std macro formatting.

bram209 commented 1 year ago

The format! macro is now properly formatted in the latest version. Considering a macro in rust effectively maps a stream of tokens to a new stream of tokens, there is no straightforward way to support formatting arbitrary macros (as they can take a self-made syntax as input).

This library depends on prettyplease for formatting rust source code. Feel free to make an issue there.