Highlighting Custom Patterns with ReSharper

A new feature that has shipped with ReSharper 5 is the Structural Search and Replace. It is a way for us to locate certain patterns in our code, and optionally replace them. However, it is more than just a Find and Replace. It allows us to somehow extend ReSharper’s inspection settings by offering us means to identify patterns and highlight them.

As many of you know, ReSharper offers a series of Suggestions, Hints, Warnings and Errors when it detects certain code patterns. For instance, when explicitly declaring a variable, ReSharper suggests you use var (and with good reason)

image

These are configurable, in that you can tell ReSharper whether you want them to be displayed as hints, suggestions, warnings or errors, which is done via ReSharper | Options | Inspection Severity

image

Setting it to Show as error for instance will display a red (or whatever color you have configured) underlining and the right-hand side border will display the a Red box indicating an error in the file.

SNAGHTML12140a89

If we now combine this with Solution Wide-Analysis, ReSharper will also indicate an error in the solution.

image

 

Extending Inspections

Have you ever encountered the following?

image

This is a perfect candidate for using String.Format. How about this?

image

Did you know StringBuilder has an AppendFormat? You might know that, I might, but maybe other developers don’t. It would be good to provide developers with a hint or suggestion that they could use AppendFormat. Up to version 5 of ReSharper, the inspections were limited to what ReSharper provided out of the box and the only thing we could do to extend this functionality was create/use a plug-in We can now use Structural Search and Replace to accomplish this. Let’s see how it works.

1. Open up a Solution (I’m using BlogEngine.NET but feel free to create your own with some sb.Append calls).

2. Select ReSharper | Tools | Pattern Catalog.

SNAGHTML1228e7ce

By default, the Pattern Catalog is empty.

3. Click on the Add Pattern

SNAGHTML122b7d0a

4. In this first sample, all we want to do is highlight code, so click on the Find button at the top to switch the dialog to Find mode only.

SNAGHTML122d5169

The Search Pattern is what we’ll use to define what we are looking for. The Description is a text which helps us identify the pattern easily in the Catalog. The box on the right is for place holders which we’ll see in a moment.

5. Enter the following pattern in the Search Pattern editor

image

What this does is indicate the pattern we are looking for. The values surrounded with $’s (same notation as that used in Live Templates) represent placeholders. A placeholder can be an expression, statement, type, etc. If it is red, it means it has not yet been defined (next step).

6. Click on the Add Placeholder button on the right box and select Expression, and enter the following values:

SNAGHTML12c3cb9a

We are indicating that the placeholder sb (first one used) is an expression of type System.Text.StringBuilder

7. Click on the Add Placeholder button to define the second placeholder, args. Select Argument and enter the following values:

SNAGHTML12c63dfb

We can optionally indicate whether we want to limit the number of minimum or maximum arguments.

If both placeholders have been defined correctly, the Search pattern box should now look like this:

SNAGHTML12c9a0e2

(we’ve entered a description at the bottom)

8. We need to define the type of severity we want. In this case we want it to be marked as a Suggestion. We do that using the Pattern severity dropdown and then click Save

Our Pattern Catalog should now be updated to reflect this new pattern

SNAGHTML12caa7e4

9. At this stage we can either close the Patterns dialog or Search for a pattern. Let’s see what happens if we click Search now button.

SNAGHTML1376eb2e

A Find Results window is displayed highlighting everywhere where ReSharper can find a pattern match. But what about the Pattern severity? Where does that come into play? Let’s double-click on one of the entries:

image

See the highlighting of the sb.Append call? Set the Pattern Severity to Error and that will display in Red.

image

Turn on Solution-Wide Analysis and that will list as an error!

So as we can see, the Structural Search (we haven’t replaced yet), allows us to monitor for certain code patterns in our projects. Potentially we could use it to detect certain code smells.

Fixing things up

Now that we have certain patterns located, and even highlighted, how do we go about fixing things? As we saw in the previous screenshots, we have a little icon pop up when placing the cursor on the line:

image

This context action however doesn’t offer us anything in order to fix the situation, i.e. there is no QuickFix. The reason is obvious: ReSharper doesn’t know what a fix is. Let’s show it!

1. Open up the Patterns Catalog and Edit the previously created Pattern

2. Click on the Replace button to open up the Replace editor at the bottom

SNAGHTML1384f656

3. Enter the following text in the Replace pattern section

image

4. Provide a description (this is what the hint of the QuickFix will dispaly)

image

5. Click on the Save button

Locate the previous sb.Append call and place the cursor on top of the line. We can now see a QuickFix icon appear (A red bulb in this case since it’s an Error)

image

Alt+Enter and we’re done!

image

 

Global Fix

What we’ve just done is a manual local fix, that is, locate the offending entry and hit Alt+Enter to apply a QuickFix. We can do this at a global scope by using the Pattern Catalog tool window.

1. Undo the prefix fix (so as to have several instances)

2. Open up the Patterns Catalog

3. Select the recently created Pattern and click on Search now. This time, instead of the Find Results dialog, we get a Replace dialog which displays all matching patterns and a Replace button

SNAGHTML138b5c29 

4. We can select the entries we want replacing (by default all checked). Click Replace

We’re done! ReSharper will now replace all occurrences. So we’ve applied a QuickFix globally.

 

There’s more

What we’ve seen here is just a simple example of how to improve on a call to a class. We can of course do more complex searches, allowing us to identify code smells, etc. The pattern we created in this tutorial is actually included in a Patterns catalog you can download from the documentation section of our web site (or click here for direct link). All you need to do is unzip it and import the XML from the Patterns Catalog window (Import button). Currently, Pattern Catalogs are PER solution.

There’s much more planned for Structural Search and Replace in vNext and we are always happy to receive feedback, so please try it out, and let us know what you think.


Posted 08-19-2010 4:37 PM by Hadi Hariri

[Advertisement]

Comments

Steve Dunn wrote re: Highlighting Custom Patterns with ReSharper
on 04-07-2011 9:24 AM

Is it possible to search for a method in a for loop?  e.g. I want to pick out things like:

for(int i=0; i<[really expensive property or method]; i++)

but not for

for (int i=0; i<100; i++)

Is this possible?  I can't get the differentiation between '100' and 'really expensive method/property' working

Mattie wrote re: Highlighting Custom Patterns with ReSharper
on 10-10-2011 1:32 AM

Thanks for writing such an easy-to-undertansd article on this topic.

bookmarking wrote re: Highlighting Custom Patterns with ReSharper
on 01-18-2013 5:56 PM

9FekLF Major thanks for the article post.Much thanks again.

generic stendra online wrote re: Highlighting Custom Patterns with ReSharper
on 01-27-2013 8:12 AM

wbyA2a Thank you for your blog article.Really looking forward to read more. Cool.

buy imitrex wrote re: Highlighting Custom Patterns with ReSharper
on 02-14-2013 5:56 AM

SmDTfr A round of applause for your article post.Much thanks again. Awesome.

buy cialis online wrote re: Highlighting Custom Patterns with ReSharper
on 02-24-2013 2:26 PM

kRhD9M Appreciate you sharing, great blog.Really thank you! Fantastic.

amazing site wrote re: Highlighting Custom Patterns with ReSharper
on 03-02-2013 3:46 PM

KwtgKe Thanks-a-mundo for the blog post.Really looking forward to read more. Really Cool.

buy discount viagra wrote re: Highlighting Custom Patterns with ReSharper
on 03-03-2013 12:01 PM

JEUpoC Thanks again for the post.Much thanks again. Fantastic.

bookmaring service wrote re: Highlighting Custom Patterns with ReSharper
on 03-13-2013 7:47 PM

FzDaf9 Thanks so much for the blog.Much thanks again. Much obliged.

bookmaring service wrote re: Highlighting Custom Patterns with ReSharper
on 03-15-2013 6:00 AM

KIbbFh Really appreciate you sharing this blog.Really thank you! Much obliged.

buy social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 03-24-2013 8:11 PM

f0Pj5g Major thankies for the article.Really thank you! Cool.

social bookmarking service wrote re: Highlighting Custom Patterns with ReSharper
on 04-03-2013 5:42 PM

sj4roQ Very good article.Really looking forward to read more. Really Cool.

comedy wrote re: Highlighting Custom Patterns with ReSharper
on 04-05-2013 9:02 PM

Thank you ever so for you blog.Thanks Again. Really Cool.

bears wrote re: Highlighting Custom Patterns with ReSharper
on 04-06-2013 2:14 PM

Hey, thanks for the blog post.Really thank you! Awesome.

buy social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 04-08-2013 2:17 AM

AL7gBV I really enjoy the blog post. Keep writing.

Social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 04-14-2013 3:29 AM

7QUQOr Very good article post.Really thank you! Really Great.

buy social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 04-19-2013 7:24 AM

POZTG1 I truly appreciate this blog post.Really thank you! Really Great.

Social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 04-24-2013 9:35 AM

kW72dI Very good article.Really looking forward to read more. Cool.

social bookmarking service wrote re: Highlighting Custom Patterns with ReSharper
on 04-27-2013 10:07 PM

hB86lT Very good article.Really looking forward to read more. Much obliged.

seo pack wrote re: Highlighting Custom Patterns with ReSharper
on 05-19-2013 4:55 PM

QzGgoh I appreciate you sharing this post.Much thanks again. Want more.

social bookmarking service wrote re: Highlighting Custom Patterns with ReSharper
on 06-05-2013 11:54 PM

B0NnCT I am so grateful for your blog.Thanks Again. Fantastic.

social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 06-19-2013 12:30 PM

RTeZxf I truly appreciate this blog post.Really thank you!

social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 06-21-2013 2:53 AM

LsALLA Im grateful for the article post. Keep writing.

great service wrote re: Highlighting Custom Patterns with ReSharper
on 07-04-2013 9:32 AM

80jlg5 Major thankies for the article post.Thanks Again. Much obliged.

news wrote re: Highlighting Custom Patterns with ReSharper
on 07-09-2013 4:42 AM

ke4bBW Muchos Gracias for your article.Thanks Again. Keep writing.

click here wrote re: Highlighting Custom Patterns with ReSharper
on 07-23-2013 6:18 PM

I cannot thank you enough for the article post.Really looking forward to read more. Fantastic.

news wrote re: Highlighting Custom Patterns with ReSharper
on 07-26-2013 9:06 AM

Dh4Weo I really like and appreciate your blog post.Really thank you! Really Cool.

social bookmarks wrote re: Highlighting Custom Patterns with ReSharper
on 07-29-2013 6:29 AM

WUJsAf Thanks again for the post.Really thank you! Great.

news wrote re: Highlighting Custom Patterns with ReSharper
on 08-02-2013 10:21 AM

MiAYok Enjoyed every bit of your blog post.Really looking forward to read more. Keep writing.

greatest news wrote re: Highlighting Custom Patterns with ReSharper
on 08-04-2013 2:37 AM

EmPsgt Muchos Gracias for your blog. Keep writing.

great link buildng wrote re: Highlighting Custom Patterns with ReSharper
on 08-19-2013 8:40 AM

Qb7YYX Wow, great blog.Thanks Again. Awesome.

awesome links for you wrote re: Highlighting Custom Patterns with ReSharper
on 08-19-2013 11:18 PM

plClIu Thanks-a-mundo for the article post. Keep writing.

great link buildng wrote re: Highlighting Custom Patterns with ReSharper
on 08-21-2013 11:52 AM

SkSUP6 Thanks a lot for the blog.Much thanks again. Really Cool.

buy viagra online cheap wrote re: Highlighting Custom Patterns with ReSharper
on 09-01-2013 8:40 PM

Thanks-a-mundo for the blog.Really looking forward to read more. Will read on...

best link building wrote re: Highlighting Custom Patterns with ReSharper
on 09-03-2013 10:41 PM

DBhr8m Wow, great blog post.Much thanks again. Really Great.

link building wrote re: Highlighting Custom Patterns with ReSharper
on 09-07-2013 3:49 AM

pFRSbr Very neat blog. Keep writing.

visit now wrote re: Highlighting Custom Patterns with ReSharper
on 09-08-2013 5:57 AM

Hey, thanks for the blog post.Really looking forward to read more. Will read on...

make money online wrote re: Highlighting Custom Patterns with ReSharper
on 09-11-2013 4:34 PM

o8sc6W Major thankies for the article post.Really looking forward to read more. Great.

online business wrote re: Highlighting Custom Patterns with ReSharper
on 09-12-2013 8:57 PM

KiRz35 Muchos Gracias for your blog article. Great.

pro link building wrote re: Highlighting Custom Patterns with ReSharper
on 09-24-2013 6:30 PM

q6O1ZY Great, thanks for sharing this blog.Thanks Again. Keep writing.

great seo work wrote re: Highlighting Custom Patterns with ReSharper
on 09-28-2013 10:02 PM

G58pel Major thanks for the post.Thanks Again. Really Great.

best linkbuilding wrote re: Highlighting Custom Patterns with ReSharper
on 09-30-2013 10:43 PM

Wg8MKB Thank you for your article post. Really Great.

best link build wrote re: Highlighting Custom Patterns with ReSharper
on 10-15-2013 3:48 PM

E0IPmG Muchos Gracias for your post.Much thanks again. Really Cool.

top seo guys wrote re: Highlighting Custom Patterns with ReSharper
on 10-24-2013 7:43 AM

xW1abN A big thank you for your article.Really thank you! Much obliged.

smashing top seo wrote re: Highlighting Custom Patterns with ReSharper
on 10-30-2013 8:08 PM

Pc0Ird Thanks for the post.Thanks Again. Want more.

great things to know wrote re: Highlighting Custom Patterns with ReSharper
on 11-18-2013 4:59 PM

wIA5go Muchos Gracias for your blog article.Thanks Again.

check this out wrote re: Highlighting Custom Patterns with ReSharper
on 11-20-2013 11:15 PM

SlNLig Thank you ever so for you blog.Much thanks again. Awesome.

seo service wrote re: Highlighting Custom Patterns with ReSharper
on 12-15-2013 8:13 AM

r5vYwN Really enjoyed this blog post. Great.

awesome things! wrote re: Highlighting Custom Patterns with ReSharper
on 01-08-2014 4:02 AM

8WnKKO Very neat article post.Really looking forward to read more.

awesome things! wrote re: Highlighting Custom Patterns with ReSharper
on 01-09-2014 4:39 PM

DQ5205 Major thankies for the blog post.Thanks Again.

nice seo guys wrote re: Highlighting Custom Patterns with ReSharper
on 01-16-2014 12:13 AM

e9Q0JQ Im thankful for the article.Really looking forward to read more. Cool.

stunning seo guys wrote re: Highlighting Custom Patterns with ReSharper
on 01-19-2014 3:26 PM

hJriby I think this is a real great blog.Much thanks again. Awesome.

check it out wrote re: Highlighting Custom Patterns with ReSharper
on 02-28-2014 3:48 PM

9BXiqr I loved your article.Thanks Again. Will read on...

nice backlinking wrote re: Highlighting Custom Patterns with ReSharper
on 03-13-2014 5:16 AM

D6zxN4 Muchos Gracias for your article.Really thank you!

seo for cheap wrote re: Highlighting Custom Patterns with ReSharper
on 03-22-2014 4:18 PM

J3OTqc Really appreciate you sharing this blog.Really looking forward to read more. Keep writing.

nice seo guys wrote re: Highlighting Custom Patterns with ReSharper
on 03-25-2014 5:01 PM

tVOU9q Very good blog article.Really looking forward to read more. Fantastic.

nice seo guys wrote re: Highlighting Custom Patterns with ReSharper
on 04-01-2014 9:01 AM

QYkcqC I am so grateful for your blog article. Fantastic.

best prices wrote re: Highlighting Custom Patterns with ReSharper
on 04-01-2014 12:51 PM

8JKmrU Thank you for your article post.Really looking forward to read more. Really Great.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)