ReSharper has the fantastic StringFormatMethodAttribute annotation which provides code analysis for format strings and their parameters. It can be added to any custom string format methods like this:
[StringFormatMethod("format")]
void DebugFormat(string format, params object[] args);
This works well when using integer placeholders, e.g.:
x.DebugFormat("ReSharper is {0} for {1} placeholders", "great", "numbered");
However, some libraries (like Serilog and ASP.Net Core / Microsoft.Extensions.Logging) offer named format placeholders which are used for (semantic | structured) logging and this seems to be something that ReSharper does not (yet) offer. For example, a line of code like this:
x.DebugFormat("ReSharper is {Quality} for {Style} placeholders", "poor?", "named");
Results in the ReSharper code analysis warning
Format item index must be a number starting 0 without leading or trailing whitespaces
Searching on ReSharper support, this site, and Google draws a blank. Has anyone found a way to get string format annotations working with named placeholders?
Relevant JetBrains links:
ReSharper help on StringFormatMethodAttribute
ReSharper help on custom string formatting methods
I have implemented an R# extension for this. Download it here from JetBrains.
It is also hosted on GitHub.
This extension highlights structured logging templates and contains some useful analyzers. Here is how the structured logging events look with the plugin installed:
Related
Hi fellow programmers and nerds!
When creating regular expressions Visual Studio, the IDE will highlight the string if it's preceded by a verbatim identifier (for example, #"Some string). This looks something like this:
(Notice the way the string is highlighted). Most of you will have seen this by now, I'm sure.
My problem: I am using a package acquired from NuGet which deals with regular expressions, and they have a function which takes in a regular expression string, however their function doesn't have the syntax highlighting.
As you can see, this just makes reading the Regex string just a pain. I mean, it's not all-too-important, but it would make a difference if we can just have that visually-helpful highlighting to reduce the time and effort one's brain uses trying to decipher the expression, especially in a case like mine where there will be quite a quantity of these expressions.
The question
So what I'm wanting to know is, is there a way to make a function highlight the string this way*, or is it just something that's hardwired into the IDE for the specific case of the Regex c-tor? Is there some sort of annotation which can be tacked onto the function to achieve this with minimal effort, or would it be necessary to use some sort of extension?
*I have wrapped the call to AddStyle() into one of my own functions anyway, and the string will be passed as a parameter, so if any modifications need to be made to achieve the syntax-highlight, they can be made to my function. Therefore the fact that the AddStyle() function is from an external library should be irrelevant.
If it's a lot of work then it's not worth my time, somebody else is welcome to develop an extension to solve this, but if there is a way...
Important distinction
Please bear in mind I am talking about Visual Studio, NOT Visual Studio Code.
Also, if there is a way to pull the original expression string from the Regex, I might do it that way, since performance isn't a huge concern here as this is a once-on-startup thing, however I would prefer not to do it that way. I don't actually need the Regex object.
According to https://devblogs.microsoft.com/dotnet/visual-studio-2019-net-productivity/#regex-language-support and https://www.meziantou.net/visual-studio-tips-and-tricks-regex-editing.htm you can mark the string with a special comment to get syntax highlighting:
// language=regex
var str = #"[A-Z]\d+;
or
MyMethod(/* language=regex */ #"[A-Z]\d+);
(the comment may contain more than just this language=regex part)
The first linked blog talks about a preview, but this feature is also present in the final product.
.NET 7 introduces the new [StringSyntax(...)] attribute, which is used in .NET 7 on more than 350 string, string[], and ReadOnlySpan<char> parameters, properties, and fields to highlight to an interested tool what kind of syntax is expected to be passed or set.
https://devblogs.microsoft.com/dotnet/regular-expression-improvements-in-dotnet-7/?WT_mc_id=dotnet-35129-website&hmsr=joyk.com&utm_source=joyk.com&utm_medium=referral
So for a method argument you should just use:
void MyMethod([StringSyntax(StringSyntaxAttribute.Regex)] string regex);
Here is a video demonstrating the feature: https://youtu.be/Y2YOaqSAJAQ
Just a thought came in mind.
Why the first of these two tags does not work in asp.net?
To me both are string values whose final value is same. Which is value of attribute href of hyperlink.
Please make me understand it.
Test
Test
Note: I am not looking for solution to it. Only want to understand what's logically difference between these two case.
To me both are string values whose final value is same
Not really. Consider first if you just had a plain non-razor HTML form such as if you'd hard-coded the beginning of the path:
Test
Now, here "/SomeProject/Test/Index" is not a string. It's an attribute value in HTML. HTML has no concept of "string". Sure it happens to use the same delimiter as C# does for string, but this isn't C# here.
With Razor we have a few means to indicate we want something done by Razor to the otherwise plain HTML.
# is one of them, indicating that we want a C# expression evaluated and the result (if not void) output. And the result of (string.Format("~/Test/Index")) is ~/Test/Index so that is output.
~/ is, in certain contexts, another one, indicating that it should be replaced with the value of evaluating Href("~/") or Url.Content("~/"), which would be something like / or /SomeProject/ or whatever. It's not so much a string as something more like a keyword.
If you have an expression returning a string, you can still use strings similarly with:
Test
Which incidentally, was the only way this could be done in Razor 1.0. The direct parsing of "~/… was added as a convenience and it is indeed convenient.
I think you're misunderstanding what string.Format(string format, params Object[] args) does. It will only replace specified placeholders {0}, {1}, etc with provided paramters. Take a look at the docs in the link provided for more info.
In short it doesn't do any automatic replacement of environment variables or any other substitutions not already covered by the placeholders.
String.Format sits in the System namespace, therefore I wouldn't expect it to know anything about web sites, file systems or anything more complicated than simple placeholder substitution and formatting.
You should be using Url.Action i.e.
Test
or even easier
#Html.ActionLink("Test", "Index", "Test")
Also note that in ASP.Net MVC you don't use the full controller name, only the prefix i.e. Test instead of TestController
i have a code like this
void method1(param1
,param2
,param3){...}
but when i press ctrl+k , ctrl+d to reformat the class, it will be formatted as flow
void method1(param1
,
param2
,
param3){...}
i know that it will be fix if i put the comma after param Name but i really want it to be before param Name. how can i inform the auto format future of vs2010 to format my method as follow:
void method1(param1
,param2
,param3){...}
Tools > Options > Text Editor > C# > Formatting
if you use resharper loot at here
Have a look at ReSharper you can configure how you want application tidy-ups to be run (along with providing some other useful features).
how can I escape "[" in Resharper 6s Structural Search (C#). If i am searching for a pattern which contains a square bracket i just got the info: pattern is ambiguous.
Is there any documentation of the syntax? I can't find a syntax documentation on the resharper homepage except for the $VarName$ syntax.
Standard regex escapes like \[ \\[ where tested. They are not working.
Basically I want to do a search and replace.
Quoting your comment:
something like this: [assembly: AssemblyFileVersion("...")]
Structural Search and Replace looks for executable code, which this isn't. To find what you want (the keybindings I use are the Visual Studio ones):
ReSharper | Navigate | Go to Symbol... (or Alt+Shift+T)
Type in AssemblyFileVersionAttribute
If R# doesn't automatically switch to searching library types, check the Include library types checkbox
You should be taken to the definition of this attibute, in the Object Browser
ReSharper | Find | Find Usages (or Shift+F12)
You should now be shown all the usages of this attribute, in the R# Find Results window.
I have researched this for a few hours and I am kind of frustrated. Maybe I am just missing something as I am new to blogging.
I am not hosting my own blog, I am just using WordPress.com.
I want to include snippets of c# code and have them look like they do in Visual Studio, or at least make them look nice, certainly with line numbers and color.
The solutions I have seen for this all seem to assume you are hosting your own blog.
I cannot figure out how to install plugins.
Is there a widget that will make code snippets look nice, or some other solution I can easily use?
Thank you
EDIT: Sarfraz has outlined one way to solve my problem (thank you!), and I have tried it but there is an issue I have, namely that it does not colorize most of my code (newer keywords like var, from, where, select, etc). Is there a fix to this or is there some other solution?
Just edit your aricles in html mode and enclose your code within these tags.
[sourcecode language="css"]
[/sourcecode]
Example:
[sourcecode language="javascript"]
// javascript hello world program
alert('Hello, World !!');
[/sourcecode]
Note: You need to specify correct language identifier for the language attribute as shown above.
More Information Here :)
The [sourcecode] tag usually works fine for C#, but for me it often breaks when I post XAML code.
Instead I use this page to format my code. The result looks nice (you can see it on my blog), but it requires the "Custom CSS" option ($15/year).
EDIT: actually the [sourcecode] tag works fine, and I'm now using it in all my posts
[code language="csharp"]
//Your code here
[/code]
looks like this has been updated, now you can use
[code language="[the lang you are posting]"]
your code here
[/code]
note: you can shorthand language as lang
[code lang="[the lang you are posting]"]
your code here
[/code]
here is the list of supported languages