I am currently creating a custom .NET router. It is working as it should, but one thing that bothers me is that I don't get the same level of syntax highlight in rider as I get with other more standard methods.
for example if I use routes.MapRoute with a template string the variables gets highlighted in blue (as shown in the picture below)
But I would like rider to understand that it should treat the string to ShopRouter the same.
Looking into the IRouteBuilder.MapRoute function I can see this
[StringSyntax("Route")] string? template
so I did the same to my constructor
public ShopRouter([StringSyntax("Route")] string template, object defaults, IRouter handler)
but it does nothing at all.
is it at all possible to get rider to understand this, or is it just a built in thing that I have no control over?
2 Answers 2
ReSharper does a lot of heavy lifting in terms of artificially adding attributes to existing Microsoft and .NET package methods which aids its own IDE hints, including string syntax highlighting: https://www.jetbrains.com/help/resharper/Code_Analysis__External_Annotations.html#jb
These are all just bundled directly with ReSharper, but you're also able to add them to your own methods with the JetBrains.Annotations nuget package. I think you'll find on the MapRoute method, there also appears to be a RouteTemplate attribute declared on that parameter which is probably one of the many that JetBrains has defined themselves. I suspect you'll also want to define that against your parameter to get highlighting in Rider.
In my test example, I've just got a method with the following signature
public static void TestMethodWithRoute([StringSyntax("Route"), RouteTemplate] string param)
Which produces the following syntax highlighting for me
Syntax-highlighted code sample
I'd probably suggest reflecting both attributes by including the StringSyntax("Route") one as well as I presume this is for Visual Studio, but that's entirely speculation and someone more educated than me in that area is welcome to correct me. I'm not sure how Visual Studio handles this kind of highlighting.
Also just a note, I did notice when I was experimenting with this that sometimes the UI needed a restart after adding the attribute to actually get the highlighting, but your experience may vary :)
Comments
It seems that the highlighter relies on some rules being followed, for example it prefers instance methods, and sometimes specifically named methods (for local methods):
app.MapGet("/error", _ => throw new NotImplementedException("Hey"));
TestLocal("/error"); // no highlight
MapGet("/error"); // highlights
ShopRouter.MapGet("/error",_ => throw new NotImplementedException("Hey")); // highlights
ShopRouter.MapGet("/error"); // highlights
ShopRouter.MyName("/error",_ => throw new NotImplementedException("Hey")); // highlights
new ShopRouter("/error/{id}") // no highlight
.Do("/api/{controller}/{action}"); // highlights
ShopRouter.Create("/api/{controller}/{action}"); // highlights
void TestLocal([StringSyntax("Route")] string pattern){}
void MapGet([StringSyntax("Route")] string pattern){}
class ShopRouter
{
public ShopRouter([StringSyntax("Route")] string pattern) { }
public void Do([StringSyntax("Route")] string pattern) => new ShopRouter(pattern);
public static ShopRouter Create([StringSyntax("Route")] string pattern) => new ShopRouter(pattern);
public static void MyName([StringSyntax("Route")] string pattern, RequestDelegate requestDelegate){}
public static void MapGet([StringSyntax("Route")] string pattern, RequestDelegate requestDelegate){}
public static void MapGet([StringSyntax("Route")] string pattern){}
}
Notes:
- For my setup it seems moving to a static method in class has helped
- This can be version-depended (I run Rider EAP on Linux + .NET 9) and can be quite brittle
- There is a similar feature in VS and it can behave in totally different way.
- You might want to look at plethora of annotations from the
JetBrains.Annotationspackage, one of which could help you in this case