118

[Enable intellisense on HTMLHelper attribute for css classes]

I have this HTMLhelper:

public IHtmlString MyTextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TProperty>> propertyExpression, 
    string cssClass)
{
    // ...
}

I want Resharper to give me IntelliSense for CSS classes defined in my application when passing the value for the "cssClass" parameter.

There are some code annotation attributes that Resharper recognizes, but none that seem directly related to marking a method parameter as being CSS classes.

The closest I could find was [HtmlAttributeValue(string name)]. I tried to apply to the cssClass parameter like this:

public IHtmlString MyTextBoxFor<TModel, TProperty>(
    this HtmlHelper<TModel> html, 
    Expression<Func<TModel, TProperty>> propertyExpression, 
    [HtmlAttributeValue("class")] string cssClass)
{
    // ...
}

But that doesn't work. It would also be super awesome if Resharper would recognize the entered class and stop bugging me about unknown CSS classes in jQuery selector expressions (that operate on the textbox generated by the helper above).

Edit: Here's a screenshot of the kind of intellisense that is working for the "htmlAttributes" parameter of an action method. This is accomplished by using the [HtmlElementAttributes] annotation on the parameter.

Resharper htmlAttributes intellisense

I want a similar annotation that lets me put css classes in a string parameter and have the same intellisense appear showing css classes.

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
Raif Atef
  • 2,858
  • 1
  • 22
  • 31
  • 10
    Opened an issue on ReSharper bugtracker: http://youtrack.jetbrains.com/issue/RSRP-389238 – Raif Atef Oct 20 '13 at 04:08
  • 4
    Nice Question, I want to know the answer as well. – Murtaza Munshi Aug 06 '15 at 06:36
  • 2
    @RaifAtef does this already exist in ReSharper, could you add a screenshot of what your talking about and just to make sure your not talking about http://stackoverflow.com/questions/529677/how-to-have-comments-in-intellisense-for-function-in-visual-studio – Seabizkit Oct 15 '15 at 19:24
  • 2
    @Seabizkit it is similar to the intellisense you get when you are writing "return View(" and you get intellisense with the view names, or when you are calling "Html.Action(" and you get intellisense for the controller and action name. I just want to make resharper show me CSS classes when I am entering the value for the cssClass parameter. EDIT: No my question is diffferent from yours. Yours is a built-in VS thing, mine is strictly resharper related. – Raif Atef Oct 15 '15 at 23:13
  • 3
    @RaifAtef, Ok.. the problem is, I have ReSharper obviously **but can not see what feature of ReSharper you are talking about**... I just see normal built-in intellisense... I'm genuinely interested as maybe I'm missing out on some ReSharper magic, but i cannot see it. Could you please post a screenshot of what i should be seeing. I tried with "Html.Action(" and "Html.MyCustomStuff" don't see any difference between the two. using ReSharper 9.1.3 which is like one of the latest. – Seabizkit Oct 16 '15 at 06:36
  • 2
    @Seabizkit I have edited the question with a screenshot – Raif Atef Oct 16 '15 at 09:44
  • 2
    @RaifAtef oooo! that helps a lot! ill investigate. Thanks! – Seabizkit Oct 16 '15 at 10:38

1 Answers1

3

Use [ValueProvider]

From the Code Annotations currently supported by Resharper 10, the best candidate would to use this attribute. From the above link:

ValueProviderAttribute

For a parameter that is expected to be one of the limited set of values. Specify fields of which type should be used as values for this parameter.

Unfortunately, I've not figured out how it works. Maybe it's buggy in my Resharper version 9.2.

What I've tried so far:

namespace ValueProviderSample
{
    public static class MyValuesContainer
    {
        public static readonly string[] Values = { "one", "two", "three" };
    }

    public class MyMethodContainer
    {
        public string MyMethod([ValueProvider("ValueProviderSample.MyValuesContainer.Values")]
                               string parameter)
        {
            return string.Empty;
        }
    }
}

Even if you make it work, you'll still have to populate the Values list.

And of course, you can still develop your code annotation/extension for Resharper.

Why not using a strong typed object instead of string ?

Sometimes instead of using string and int, we could use stronger-typed classes of our own design. Since it seems you control your code, you can instead of using string with a css name in it, you can create a new type like CssClass.

You just need to add as a prebuilt event a call to a generator which parses every css in the project and dynamically create a class like that:

public class CssClass
{
    public string Name { get; private set; }

    public static CssClass In = new CssClass("in");

    /// <summary>
    /// Initialise une nouvelle instance de la classe <see cref="T:System.Object"/>.
    /// </summary>
    private CssClass(string name)
    {
        Name = name;
    }
}

and then your sample will look like that:

public class MySample
{
    public IHtmlString MyTextBoxFor<TModel, TProperty>(
        this HtmlHelper<TModel> html,
        Expression<Func<TModel, TProperty>> propertyExpression,
        CssClass cssClass)
    {
        // ...
    }

    public void Usage()
    {
        MyTextBoxFor(html, expression, CssClass.In);
    }
}
Fab
  • 11,403
  • 3
  • 41
  • 60
  • This is interesting, but doesn't answer the question, since it won't have all the CSS classes used by the app across all of its CSS files. – Raif Atef Jan 24 '16 at 21:22