Using Resharper's structural search and replace to find usages.

Resharper structural search and replace Part I - Searching

There isn't much documentation on Resharper's Structural Search and Replace (SSR) so here is 2 such ways you might find it useful.

Consider the case where you want to find all the usages of the Text property on a Windows Form:

form_usages.png

If you use Find Usages (Shift F12) you get all usages of any inheritor of System.Windows.Forms.Control which isn't what we want in this case.

usages1.png

Enter Resharpers 'Search with pattern' (Alt-R,F,R)

structural<em>search</em>and_replace2.png

which returns the following usages only:

usages2.png

I can hear the guys up the back:

you can already do that with Resharper's Advanced Find Usages (Ctrl+Shift+Alt+F12)

but this only works in this instance because Text is overridden by System.Windows.Form. Consider a property of Form that isn't overridden (eg form.Font) - your only option here is to use SSR.

Other applications

You are searching for places where an Address is added to a List<Address>

namespace ExampleApp
{
    public class Address
    {

    }

    public class Program
    {
        private static void SomeMethod()
        {
            var strings = new List<string>();
            strings.Add("test1");
            strings.Add("test2");
            strings.Add("test3");//not these usages

            var addresses = new List<Address>();
            addresses.Add(new Address());//this usage only
        }
    }
}

Find Usages will return all usages of List<T>.Add (which likely is very high in a real application)

Search with pattern to the rescue again!

structural<em>search</em>and_replace3.png

This returns only the one usage above instead of 4 usages using F12.

Important! Make sure when specifying the type of $list$ that you specify the fully qualified class name eg List<ExampleApp.Person> - on that note it appears Resharper is showing the < and > characters as their escaped versions in the UI which is a display-only thing.

Part II will cover an example of finding usages and replacing them with a predefined template.

resharperc#
Posted by: Wallace Turner
Last revised: 13 Apr, 2013 01:58 AM History

Comments

20 Feb, 2012 08:32 PM @ version 14

Just great! thanks for the information.

Ronald Zarīts
Ronald Zarīts
21 Feb, 2012 10:25 AM @ version 14

A very good practical post. I did not know this feature, but can surely see myself using it.

This makes me curious about learning more - what are the other possible placeholders (Statement, Type, Identifier) and how they may be used.

06 Mar, 2012 07:09 PM @ version 16

Search pattern feature in resharper rocks. I know something like that we already have in Visual Studio, but this is more powerful.

No new comments are allowed on this post.