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.
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
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:
So in my code I have a lot of places where I'm using _SendPropertyChanged("FancyFuuBar"); which I'd like to replace with _SendPropertyChanged(nameof(FancyFuuBar)); - obviously the FancyFuuBar part is not the same everywhere but I'd like a single command that can tackle this everywhere.
I "know" this is possible with regex (actually, I think and assume it is, but I know not).
How do I have regex "remember" the FancyFuuBar name and use it again in the replace instance? Is this possible in Visual Studio 2015 find and replace?
If this is not possible with a single regex, what's the best way to go about doing it, without loosing control? I.e. I would like to be able to limit this to a few files at first and preferably be able to click "Find Next" and then decide if I want to Replace that instance or not.
I dare not big bang this on all the code just yet.
What you ask for is backreferences that hold the value captured with corresponding capturing groups.
It is easy if you do not have any complex cases with lazy matching:
\b_SendPropertyChanged\("(.*?)"\);
And replace with _SendPropertyChanged(nameof($1));
V
Here is the structure of a match with the current expression:
\b_SendPropertyChanged\("(.*?)"\);
| ^$1-^ |
^--------------- Match Value ----^
What we match will get replaced with the replacement pattern. The captures start with 0 index, and the zeroth group is the whole match. $1 is the value captured with the first (...). And so on.
In C# (as in Java), curly braces are optional for e.g. if blocks and loops that only contain a single statement:
if (condition) DoSomething();
I am looking for a tool that inserts missing optional curly braces for my entire solution, turning the above code into something like this:
if (condition) {
DoSomething();
}
I know that Eclipse can do this for Java. Unfortunately, I am not aware of a tool that can do this for C#. I would be grateful for suggestions! Thanks!
JetBrains Resharper gives you the possibility to do such code refactorings by a short keystroke.
You could write a ReSharper Replace Pattern.
Add a pattern to Pattern Catalog by (in ReSharper 5.1.3 ReSharper->Tools->Pattern Catalog->Add Pattern).
Then you write your pattern like so:
Unfortunatly this does not work for if-else. So you need another pattern like so:
Then you can set pattern's severity in Pattern Catalog dialog and can click Search now.
With ReSharper, go to ReSharper | Options -> Code Editing | C# | Formatting Style | Braces Layout and change neccessary options in Force Braces section to Add braces. Then find your solution in Solution Explorer, invoke context menu and choose Cleanup code... from it. Select Default: Reformat code and press Run. But be carefull! It would also reformat all code files in your solution. Be sure to backup if you do it the first time, just in case you wouldn't like ReSharper's default formatting. Maybe you would need to play with formatter settings to make it suit you.
you can use Brace Completer
http://visualstudiogallery.msdn.microsoft.com/0e33cb22-d4ac-4f5a-902f-aff5177cc94d
I'm wanting to replace all references to a resource file in my C# code.
An example is a page that contains several references such as:
Resources.Global.Firstname
Resources.Global.Surname
I'd like the regular expression to find all of these (they could end either with a ; or a )).
Total beginner with regular expressions, so any advice here would be gratefully received.
You can just use the Find and Replace window in Visual Studio.
Press Ctrl-H to open the window.
Put Resources\.Global\.{[^,) ;]+} in the "Find what:" text box.
Put GetStringValue("\1") in the "Replace with:" text box.
Make sure the "Look in:" dropdown is set to the scope you want to search
Expand the Find options subpanel.
Check the box next to "Use:" and make sure that "Regular expressions" is selected.
What this is doing:
The first regular expression will find anything that starts with Resources.Global. and capture whatever is after it until it finds a space, a comma, a close paren, or a semi-colon.
The second one replaces the entire text that was found with GetStringValue("") and puts the captured text inside the quotes in the parentheses.
Why not just do CTRL+H (quick find and replace) and search on the actual terms rather than the regex pattern? What are you trying to rename from and to?
UPDATE
The pattern to match would be something like: Resources.Global.([^};]+)
Replace pattern would be GetStringValue("\1")
As Blazes said, in the scenario you mentioned, Refactoring is the actual answer. If you just want to see them, right click on the definition and select Find all references. If you want to change it, just make the changes and then press ctrl+shift+F11, a context menu appears which gives you the chance to rename all references.
Regular expression:
(Resources\.Global\.[A-Z][a-zA-Z]*[;\)])
will find only the last two lines out of the following tested lines:
Resources.Global.Firstname
Resources.Global.Surname
Resources.Global.Firstname;
Resources.Global.Surname)
used code to verify:
Regex regex = new Regex(#"(Resources\.Global\.[A-Z][a-zA-Z]*[;\)])");
MatchCollection mc = regex.Matches("Resources.Global.Firstname\n" +
"Resources.Global.Surname" +
"Resources.Global.Firstname;" +
"Resources.Global.Surname)");
foreach (Match match in mc)
{
Console.WriteLine(match.Groups[1].ToString());
}
This software might help you:
Rad Software Regular Expression Designer