Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustywind support for sorting Tailwind classes #116

Closed
Rolv-Apneseth opened this issue Apr 12, 2024 · 20 comments · Fixed by #122
Closed

Rustywind support for sorting Tailwind classes #116

Rolv-Apneseth opened this issue Apr 12, 2024 · 20 comments · Fixed by #122

Comments

@Rolv-Apneseth
Copy link

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
Copy link
Owner

bram209 commented May 24, 2024

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
Copy link
Owner

bram209 commented May 24, 2024

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

@Rolv-Apneseth
Copy link
Author

Sure yeah, makes sense

@bram209
Copy link
Owner

bram209 commented May 24, 2024

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

@Rolv-Apneseth
Copy link
Author

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
Copy link
Author

Let me know if that PR is alright

@bram209
Copy link
Owner

bram209 commented Jun 12, 2024

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
Copy link
Author

Sure yeah, I'll check it out

@Rolv-Apneseth
Copy link
Author

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

@bram209
Copy link
Owner

bram209 commented Jun 13, 2024

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
Copy link
Author

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
Copy link
Owner

bram209 commented Jun 13, 2024

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

@Rolv-Apneseth
Copy link
Author

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
Copy link
Owner

bram209 commented Jun 13, 2024

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
Copy link
Owner

bram209 commented Jun 13, 2024

by the way, your portfolio website looks nice 🤩

@Rolv-Apneseth
Copy link
Author

Rolv-Apneseth commented Jun 13, 2024

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
Copy link
Owner

bram209 commented Jun 13, 2024

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
Copy link
Author

Rolv-Apneseth commented Jun 13, 2024

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
Copy link
Owner

bram209 commented Jun 14, 2024

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
Copy link
Author

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 == '_');
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants