khalidabuhakmeh / Htmx.Net

Adds extensions methods to HttpResponse and HttpRequest to make working with Htmx easier.
MIT License
472 stars 25 forks source link

Rider not highlighting `hx-controller` and `hx-action` attributes #7

Closed stevenbenitez closed 2 years ago

stevenbenitez commented 2 years ago

I'm not sure if this is an Htmx.TagHelpers issue or a JetBrains Rider issue. I am using the Htmx.TagHelpers and they work great. Razor converts the following code into an hx-get="/actual/path/to/controller".

<select class="form-control custom-select"
        asp-for="SchoolId"
        asp-items="Model.Schools"
        hx-get
        hx-controller="SurveySubmissions"
        hx-action="GetTeachersAjax"
        hx-route-surveyId="@Model.SurveyId"
        hx-route-currentTeacherSelection="@Model.TargetId"
        hx-target="#teachers"
        hx-indicator=".htmx-indicator">
    <option value="">Select a School&hellip;</option>
</select>

However, Rider does not pick up the hx-controller and hx-action attributes as tag helper enhanced. Looking at the code for the tag helper, I see they are defined using square brackets.

[HtmlTargetElement("*", Attributes = "[hx-get]")]
[HtmlTargetElement("*", Attributes = "[hx-post]")]
[HtmlTargetElement("*", Attributes = "[hx-delete]")]
[HtmlTargetElement("*", Attributes = "[hx-put]")]
[HtmlTargetElement("*", Attributes = "[hx-patch]")]
public class HtmxUrlTagHelper : TagHelper

If I instead change my code to include the brackets, then Rider bolds the attributes but Razor no longer processes them.

<select class="form-control custom-select"
        asp-for="SchoolId"
        asp-items="Model.Schools"
        [hx-get]
        hx-controller="SurveySubmissions"
        hx-action="GetTeachersAjax"
        hx-route-surveyId="@Model.SurveyId"
        hx-route-currentTeacherSelection="@Model.TargetId"
        hx-target="#teachers"
        hx-indicator=".htmx-indicator">
    <option value="">Select a School&hellip;</option>
</select>
khalidabuhakmeh commented 2 years ago

This is an issue with the ReSharper engine not detecting attributes in external libraries. There's an issue currently in the JetBrains YouTrack system to address it, but I don't have an ETA. :(

Update: Let me play around again and see if it's an issue in the library.

khalidabuhakmeh commented 2 years ago

Huh... you were right. The issue seems to be the []. I've just removed them and pushed a change up. Not sure why I had them there in the first place. I may have been reading some outdated docs or something. I can't remember, but I tested locally and pushed up a 0.0.16 release. It should be on NuGet soon. Please test and let me know if this fixes it on your end.

stevenbenitez commented 2 years ago

Thanks, @khalidabuhakmeh! That fixed it.

One other thing that I think would be worth adding is putting [AspMvcAction], [AspMvcController], and [AspMvcArea] attributes from JetBrains Annotations on the Action, Controller, and Area properties. I'm not sure if there are any attributes available for the Razor Page property.

https://www.jetbrains.com/help/resharper/Reference__Code_Annotation_Attributes.html#AspMvcActionAttribute

stevenbenitez commented 2 years ago

I'm happy to create a new issue for the JetBrains Annotations since it's outside of the scope of the original problem for this issue. Also happy to put a PR together to add support, if that's something you agree with.

khalidabuhakmeh commented 2 years ago

They're already there. That was the initial bug I thought you were talking about. For some reason, Rider/ReSharper doesn't recognize these attributes from external libraries. I filed an issue in the official JetBrains repo about it already so 🤞.

Thanks for using the library. I hope you find it helpful.

https://github.com/khalidabuhakmeh/Htmx.Net/blob/05309c1a505eeb0cd64e5ff34fe60e0da999cb62/src/Htmx.TagHelpers/HtmxUrlTagHelper.cs#L67-L100

stevenbenitez commented 2 years ago

I do, thanks for your work putting it together!

khalidabuhakmeh commented 2 years ago

@stevenbenitez HEY IT WORKS NOW! v0.0.18. Turns out I had to compile JetBrains.Annotations into the assembly. The NuGet Package only works for local projects. Give it a try. I just did and it seems to work.

stevenbenitez commented 2 years ago

Yep, works perfectly. Thanks!