bram209 / leptosfmt

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

Multiline strings and `#[leptosfmt::skip]` #93

Open SV-97 opened 1 year ago

SV-97 commented 1 year ago

I have a multiline string embedded in my code that gets modified by leptosfmt. Something like this:

    view! {
        <button>
        <div class="ascii-art">r#"
,--.                               ,--.   
`--',--,--,  ,---.  ,---. ,--.--.,-'  '-. 
,--.|      \(  .-' | .-. :|  .--''-.  .-' 
|  ||  ||  |.-'  `)\   --.|  |     |  |   
`--'`--''--'`----'  `----'`--'     `--'  
"#
        </div>
        </button>

that gets turned into something like this

    view! {
        <button>
            <div class="ascii-art">
                r#"
                ,--.                               ,--.   
                `--',--,--,  ,---.  ,---. ,--.--.,-'  '-. 
                ,--.|      \(  .-' | .-. :|  .--''-.  .-' 
                |  ||  ||  |.-'  `)\   --.|  |     |  |   
                `--'`--''--'`----'  `----'`--'     `--'  
                "#
            </div>
        </button>
    }

Importantly the two strings aren't the same because of the prepended spaces and they render differently. The only workaround I could think of (short of extracting the string out of the view macro) was to use indoc and do something like

    view! {
        <button>
            <div class="ascii-art">
                {
                    indoc! {
                        r#"
                ,--.                               ,--.   
                `--',--,--,  ,---.  ,---. ,--.--.,-'  '-. 
                ,--.|      \(  .-' | .-. :|  .--''-.  .-' 
                |  ||  ||  |.-'  `)\   --.|  |     |  |   
                `--'`--''--'`----'  `----'`--'     `--'  
                "#
                    }
                }
            </div>
        </button>
    }

which renders correctly - but isn't the best solution imo (and still formats a bit weirdly).

Since this changes the behaviour of the formatted code it should probably be considered a bug?

I couldn't find something similar to #[rustfmt::skip] that I could use to simply skip the formatting of the string - does leptosfmt already support such a feature?

bram209 commented 1 year ago

Since this changes the behaviour of the formatted code it should probably be considered a bug?

Yes, it should. I will see if I can take a look later this week. For now, indeed you can extract the multi line string out of the macro.

n1tranquilla commented 8 months ago

I'm also having the same issues and am eagerly looking for a solution.