ReSharper 5.0 Hidden Gem – Patterns Catalogue

[November 16, 2010] While the information below is still very much relevant, please download the latest version of ReSharper from JetBrains website.

Have I mentioned that I love ReSharper? While patiently waiting for the 5.0 release to RTM, the team at JetBrains have shipped the Beta 2 release (build 1618). This is a very stable, very good improvement over the previous nightly builds.

One of the things that improved the most with this release is the SSR – Structural Search and Replace. It’s a new feature, allowing you to specify code patterns to be added as hints, errors or warnings to the ReSharper code analysis, as well as specifying a replacement pattern.

Here is an example of this feature, taken from the JetBrains .NET Tools blog:

Adding the following pattern:

Will add a new quick-fix action:

The patterns can also be imported/exported as XML files, allowing easy sharing between co-workers.

In order to create a new pattern, select Patterns Catalogue from the Tools submenu of ReSharper menu.

Press Add Pattern.

Pattern catalogue

We can add either a search only pattern, or a search and replace pattern. Let’s add a pattern to turn NUnit Asserts into fluent NUnut Assert syntax:

  • Add Assert.AreEqual($expected$, $actual$) in the “Search Patten” window.
  • Press “Add Placeholder”, select argument, and type expected in the name. Do the same for actual.
  • In “Replace Pattern”, type Assert.That($actual$, Is.EqualTo($expected$)).
  • Choose Pattern severity, I selected “Show as suggestion”.
  • Fill in the description texts.

The final result should look like this:

Add pattern

Press Save and close the Pattern Catalogue dialog.

You will now be able to turn this:

Before

into this:

After

Happy coding!

  • Yaakov

    Is there a way to use Macros here? For example, I’d like to substring the $args$, or lowercase the $name$ etc.