bram209 / leptosfmt

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

Rustywind support for sorting Tailwind classes #116

Closed Rolv-Apneseth closed 5 months ago

Rolv-Apneseth commented 7 months ago

Hey, just wanted to open a new issue to track this instead of asking to re-open #14 but let me know if I should close this.

Now that rustywind_core has been published as a library, can it now be implemented into leptosfmt? I would love to have this feature.

Let me know if you need any help / if you'd like me to have a go at this (I can try my best).

bram209 commented 6 months ago

Hi, thank you for your interest. I can add this feature, but the sort_classes function needs to be public: https://github.com/avencera/rustywind/blob/master/rustywind-core/src/sorter.rs#L60

If sort_classes is exported, I can make it a formatter setting, that when it formats the value of configured attribute key, it will try to parse and format the classes for you. There is a method exported that formats the whole file, but that misses the point of integrating the library in the first place (in that case, why not just call rustywind outside of leptosfmt and pipe the two commands together..)

bram209 commented 6 months ago

@Rolv-Apneseth Considering you contributed to rustywind would you be willing to make a new PR that exports sort_classes?

Rolv-Apneseth commented 6 months ago

Sure yeah, makes sense

bram209 commented 6 months ago

see draft PR here: https://github.com/bram209/leptosfmt/pull/122/files

Rolv-Apneseth commented 6 months ago

Is it alright to just make it public or do you want something else from it? The actual replacing is all done in sort_file_contents but I assume that's fine.

I'll also make the extraction of the regex from the Options struct public in case someone wants to use a custom regex.

Rolv-Apneseth commented 6 months ago

Let me know if that PR is alright

bram209 commented 5 months ago

Let me know if that PR is alright

Hi @Rolv-Apneseth that works, thanks!

I have updated the dependency on #122, would you be willing to test it out? You can install this version by: cargo install --git https://github.com/bram209/leptosfmt.git --branch tailwind-support

And run it with leptosfmt .... --experimental-tailwind

If it works great for you, I will merge the PR and make a new release : ) Thanks

Rolv-Apneseth commented 5 months ago

Sure yeah, I'll check it out

Rolv-Apneseth commented 5 months ago

I can't seem to get it to work (even regular leptosfmt), is there some debug/verbose option I can use?

bram209 commented 5 months ago

cargo install --git https://github.com/bram209/leptosfmt.git --branch tailwind-support

What exactly is not working? If I run leptosfmt . --experimental-tailwind it works fine for me.

Rolv-Apneseth commented 5 months ago

Sorry should have been more clear. So, running even just leptosfmt . runs no problem, and reports that it formatted files, but doesn't seem to actually format anything. Confirmed it works with the version of leptosfmt installed using cargo install leptosfmt so I'm not sure what the issue is.

Is there a logging system to see what's happening? Otherwise I can have a look at the code when I have some time and do some println debugging.

bram209 commented 5 months ago

Do you have a snippet of source code that did not format correctly?

Rolv-Apneseth commented 5 months ago

Sure yeah:

#[component]
pub fn ExternalLink(#[prop()] href: String, #[prop()] content: String) -> impl IntoView {
    view! {
        <a class="not-italic font-bold duration-300 hover:text-black hover:underline motion-safe:transition-all hover:dark:text-slate-200"
                href=href
            target="_blank"
            rel="noreferrer noopener"
        >
            {content}
        </a>
    }
}

The release version puts <a on it's own line and de-indents href, but the version with these changes seems to do nothing.

I apologise if this is some problem with my system / specifically my project, as I'm not sure why it would work for you but not for me. The repo itself can be found here if that helps at all.

bram209 commented 5 months ago

If I paste your snippet in test.rs and run cat test.rs | leptosfmt --stdin --experimental-tailwind > test_formatted.rs && diff test.rs test_formatted.rs -y I get:

image

There was one small issue with stripping the double quotes around the value of the class attribute, but that didn't prevent it from fixing the indentation of href.

Can you tell me how you run leptosfmt?

bram209 commented 5 months ago

by the way, your portfolio website looks nice 🤩

Rolv-Apneseth commented 5 months ago

I was running it with just leptosfmt . or leptosfmt **/*.rs, and running it the way you just did still did not format anything (using diff shows no changes).

However, I tried creating the file outside of that repo and it works - so I guess something with the settings/setup of that repo? This is the leptosfmt.toml:

max_width = 100                         # Maximum width of each line
tab_spaces = 4                          # Number of spaces per tab
indentation_style = "Auto"              # "Tabs", "Spaces" or "Auto"
newline_style = "Auto"                  # "Unix", "Windows" or "Auto"
attr_value_brace_style = "WhenRequired" # "Always", "AlwaysUnlessLit", "WhenRequired" or "Preserve"
macro_names = ["leptos::view, view"]    # Macro names which will be formatted

by the way, your portfolio website looks nice 🤩

Thanks, the style is mostly copied from someone else though haha

Edit: I just remembered that I have that repo using nightly - maybe that has something to do with it?

bram209 commented 5 months ago

I was running it with just leptosfmt . or leptosfmt **/*.rs, and running it the way you just did still did not format anything (using diff shows no changes).

However, I tried creating the file outside of that repo and it works - so I guess something with the settings/setup of that repo? This is the leptosfmt.toml:

max_width = 100                         # Maximum width of each line
tab_spaces = 4                          # Number of spaces per tab
indentation_style = "Auto"              # "Tabs", "Spaces" or "Auto"
newline_style = "Auto"                  # "Unix", "Windows" or "Auto"
attr_value_brace_style = "WhenRequired" # "Always", "AlwaysUnlessLit", "WhenRequired" or "Preserve"
macro_names = ["leptos::view, view"]    # Macro names which will be formatted

by the way, your portfolio website looks nice 🤩

Thanks, the style is mostly copied from someone else though haha

Edit: I just remembered that I have that repo using nightly - maybe that has something to do with it?

I see the issue:

macro_names = ["leptos::view, view"]  

should be:

macro_names = ["leptos::view", "view"]  

also, please run cargo install --git https://github.com/bram209/leptosfmt.git --branch tailwind-support again (pushed a small fix earlier today)

Rolv-Apneseth commented 5 months ago

Oh man, sorry about that. Yes can confirm all works beautifully now.

Is there a way to add some kind of error output from leptosfmt if there is an issue with the parsed config?

bram209 commented 5 months ago

Oh man, sorry about that. Yes can confirm all works beautifully now.

Is there a way to add some kind of error output from leptosfmt if there is an issue with the parsed config?

The config is technically correct (a list of string values). So it does a successful run, formatted each file, only none of them contained a macro named “leptos::view leptos”, it did not swallow an error or anything. I suppose for this specific case we could validate that the macro name is a valid path according to the rust syntax though.

Rolv-Apneseth commented 5 months ago

Yeah, tricky I suppose.

I suppose for this specific case we could validate that the macro name is a valid path according to the rust syntax though.

It would be nice for at least that level of validation though yes. Were you thinking something simple like this (though returning errors instead of the asserts), or something more sophisticated?

for macro_name in settings.macro_names {
    assert!(!macro_name.is_empty());
    for c in macro_name.chars() {
        assert!(c.is_alphanumeric() || c == ':' || c == '_');
    }
}