bram209 / leptosfmt

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

Why leptosfmt adds an extra space after closing curly brace? #106

Closed qrilka closed 9 months ago

qrilka commented 9 months ago

Simple example:

$ echo 'view!{<p>Something: {something}.</p>}' | leptosfmt -s
view! { <p>Something: {something} .</p> }

This extra space doesn't look good in the resulting HTML and I need to do let something = format!{"{something}."); as a workaround

qrilka commented 9 months ago

And related error, after applying this workaround I get

                                            // leptosfmt addes a space between dot and a brace
                                            let skills = format!("{skills}.");
                                            view! {

formatted as

                                            let skills = format!("{skills}.");
                                            view! {
                                                // leptosfmt addes a space between dot and a brace

what could be the reason to move around comments?

qrilka commented 9 months ago

And another thing: if I switch to /* */ for this comment it just gets stripped away altogether :(

qrilka commented 9 months ago

Actually the odd thing about comment is even weirder than I described: this comment was also copied into a view! which is below the closure the cited 3 lines are in :-\

bram209 commented 9 months ago

And related error, after applying this workaround I get

                                            // leptosfmt addes a space between dot and a brace
                                            let skills = format!("{skills}.");
                                            view! {

formatted as

                                            let skills = format!("{skills}.");
                                            view! {
                                                // leptosfmt addes a space between dot and a brace

what could be the reason to move around comments?

syn does not parse non-doc comments, they are treated (per specification) as whitespace.

In order to still provide (limited) support for non-doc comments, leptosfmt tries to collect the whitespace between different the spans of each consecutive token. This currently only works for comments placed around rstml nodes and not for comments placed inside rust code blocks that reside inside a view macro. (See the note about non-doc comments on the readme.md for more info)

In your specific example, is this a view! macro call that is nested inside a parent view?

bram209 commented 9 months ago

Simple example:

$ echo 'view!{<p>Something: {something}.</p>}' | leptosfmt -s
view! { <p>Something: {something} .</p> }

This extra space doesn't look good in the resulting HTML and I need to do let something = format!{"{something}."); as a workaround

Thanks for the bug report. This is indeed incorrect behaviour. When this library was created, there was no support for unquoted text inside the view! macro yet, which means it would format into:

view! { <p>"Something:" {something} "."</p> }

but with unquoted text, as you reported, this is problematic. I am currently traveling, but will try to push a fix soon!

qrilka commented 9 months ago

In your specific example, is this a view! macro call that is nested inside a parent view?

Yes, it is

bram209 commented 9 months ago

In your specific example, is this a view! macro call that is nested inside a parent view?

Yes, it is

Could you provide a minimal reproducible example and create a separate issue for it?