ReSharper’s Structural Search and Replace

9
July 19, 2013 // tools

It is no secret that I’m a huge fan of JetBrains – most notably for their well beloved tool ReSharper.  🙂  Once you’ve spent some time with the tool and its very impressive feature suite, you really won’t be able to work without it anymore.  With so many features, there may be some that slip through the cracks.  So today let’s take a look at a feature you may not have experimented with before – ReSharper’s Structural Search and Replace.

The code transform

Through the INotifyPropertyChanged series, we learned how to simplify our boilerplate code from

public int Iterations
{
    get { return _iterations; }
    set
    {
        if (_iterations != value)
        {
            _iterations = value;
            OnPropertyChanged("Iterations");
        }
    }
}

to

public int Iterations
{
    get { return GetPropertyValue<int>(); }
    set { SetPropertyValue(value); }
}

Fantastic, but who wants to manually make that code change over and over again?

Setting up a Search and Replace pattern

ReSharper’s Structural Search and Replace lets you define a code pattern with some variable substitutions in order to pick up a code pattern. So let’s try it out. 🙂

  1. Highlight the code you want to update. This will help auto-populate the search.
  2. Launch ReSharper’s Structural Search and Replace tool (a.k.a. Search with Pattern)

    ReSharper Structural Search And Replace Menu

    ReSharper Structural Search And Replace Menu

  3. Extend the Find section as needed. In this example the OnPropertyChanged was not included by default. My first attempt was to add
    OnPropertyChanged("$propname$");

    but it wouldn’t match the code so I don’t think placeholders inside strings are accepted. Instead I added

    OnPropertyChanged($str$);

    where str is a placeholder I added that is an expression of type string.

    ReSharper Structural Search and Replace - Find

    ReSharper Structural Search and Replace – Find

  4. Select the Replace button in the top right hand corner to open up a second pane where you can fill out the replacement pattern using the same placeholders as defined in the find section.

    ReSharper Structural Search And Replace - Replace

    ReSharper Structural Search And Replace – Replace

  5. Click the Replace button in the bottom right and ReSharper will search for all matches and show you a confirmation dialog where you can enable/disable individual instances to replace.

    ReSharper Structural Search And Replace - Confirmation

    ReSharper Structural Search And Replace – Confirmation

  6. Enjoy!

Saving Your Pattern

To take it one step further, with ReSharper you can also save this pattern and make it a part of their normal code analysis.

  1. Reopen the Search with Pattern dialog and click Save in the bottom right.

    ReSharper Structural Search And Replace - Save

    ReSharper Structural Search And Replace – Save

  2. To quickly open the dialog they are talking about, click the pattern catalog icon in the top right next to the Replace radio button.

    ReSharper Structural Search And Replace - Toolbar

    ReSharper Structural Search And Replace – Toolbar

  3. When you go to the pattern catalog you’ll see your new pattern which you can set to different severity levels for code analysis to determine how urgently ReSharper flags it.  Suggestion seemed like a good level to me.

    ReSharper Structural Search And Replace - Pattern Catalog

    ReSharper Structural Search And Replace – Pattern Catalog

Now when you’re browsing your code, ReSharper will flag your code pattern.

ReSharper Structural Search And Replace - Suggestion

ReSharper Structural Search And Replace – Suggestion

Provide the Quick Fix replacement you defined.

ReSharper Structural Search And Replace - Quick Fix

ReSharper Structural Search And Replace – Quick Fix

And you’re golden. 🙂

ReSharper Structural Search And Replace - Updated

ReSharper Structural Search And Replace – Updated

Summary

ReSharper’s Structural Search and Replace is an excellent tool for identifying and updating those old code patterns or smells.  It does take some work and potentially trial and error to get the patterns right, but automating busy work is what we developers do best. 🙂

For more details you can check out ReSharper’s documentation.

Here’s an export of the pattern we built in this blog post
INotifyPropertyChanged_ReSharper_Pattern.zip
I hope it helps and if you’ve got any great patterns of your own, please share them in the comments. 🙂

About the author

Steve Cadwallader is a software developer who geeks out on user interfaces, clean code and making things easier.

9 Comments

  1. Structural Search & Replace is fine feature but every time i tried it i cant find anything i need to. Must dig into it deeper 🙂

    • It didn’t match up the first time I tried it either since I was trying to use a placeholder within a string (e.g. “$propname$”) but once I switched that over to an expression of type string it worked like a charm. 🙂

  2. Nice article.
    I’m using nearly the same search pattern to replace OnPropertyChanged from the magic string version to the CallerMemberName version and it’s working fine for me but when I have properties with xml comments resharper removes them and I lose the comments 🙁

Leave a Comment