Defining to-do patterns in ReSharper

In our previous post about working with to-do items in ReSharper, we looked at the fact that many code bases have TODO comments in code. We saw how to use ReSharper (and Rider) to find and navigate them. What if we want to define our own type of to-do items in code? Let’s answer that question in this post!

This post is part of a series around working with to-do items in ReSharper:

To-do item patterns

To recognize to-do items in code, ReSharper needs to know what qualifies as a to-do item. The patterns for a to-do item are defined in ReSharper’s settings, where we can see the default ones (under Tools | To-do Explorer):
To-do Explorer settings

The todobug and NotImplementedException types are in here. We can add our own pattern, either by adding a new one or duplicating one of the existing ones and then editing the duplicate.

To-do patterns are based on regular expressions. We can use a simple one, like the regular expression that checks for NotImplementedException – it’s just looking for that specific word in identifiers:
Edit simple to-do pattern

ReSharper runs this regular expression on our code. We can specify in which language constructs it should be recognized: in comments, in strings, or in identifiers. Our NotImplementedException pattern checks identifiers, whereas the todo and bug patterns will typically only search in comments.

Creating a to-do item pattern for // REVIEW

Let’s create our own pattern! In some code bases, I have seen code comments starting with the word REVIEW, often (but not always) followed by the name of a team member. I would like to get a nice list of those, so let’s create our own pattern!

Again, we can create a new pattern or duplicate an existing one like todo. Since all we will do is search for a new keyword in to-do comments, let’s duplicate and update our pattern to find the word “REVIEW”. In case you have used this editor before: ReSharper 2017.3 comes with an updated UI that has syntax highlighting for regular expressions.
Create a new to-do pattern

The pattern we created matches the regular expression (?<=\W|^)(?<TAG>REVIEW)(\W|$)(.*) in comments, and is not case sensitive. We are looking for any non-word character (\W), then the word “REVIEW”, then again any non-word character. This should definitely match something like // REVIEW Is this code correct?.

There is one thing special in this regular expression: we are capturing the word “REVIEW” in a capture group named “TAG” (the (?<TAG>REVIEW) portion of our regex). The “TAG” capture group is used by ReSharper to determine which part of the comment should be shown in bold in the To-do Explorer window. Here’s what it looks like when we filter our to-do items to our newly created pattern:
Custom to-do pattern in action

Keep in mind the “TAG” capture group is not required, but it can be useful to visually focus on part of the comment in the To-do Explorer tool window.

Storing to-do item patterns in settings layers

By default, to-do item patterns are stored in ReSharper’s settings on our own machine. As a project owner, I’d like to be able to share these cool GitHub issues to-do patterns with team members (and anyone who clones my project from GitHub).

Great news! We can make use of ReSharper settings layers to share these patterns (and other ReSharper settings such as code styles and so on). The Save button lets us specify where settings should be applied:
Save to-do item pattern to settings layer

When saving as team-shared settings, ReSharper will create a <SolutionName>.sln.DotSettings file which can be added to source control and then shared with other developers on the team – whether they’re using ReSharper or Rider.

In our next post, we will explore what is new in ReSharper 2017.3 – linking to external resources in to-do items! Stay tuned.

Download ReSharper 2017.3 and give it a try! We’d love to hear your feedback!

This entry was posted in How-To's and tagged , , , , . Bookmark the permalink.

2 Responses to Defining to-do patterns in ReSharper

  1. HamRusTal says:

    > We are looking for a whitespace, then the word “REVIEW”, then another whitespace.
    “\W” matches “any non-word character”, not whitespace. Whitespace is “\s” but that would be insufficient for the purpose here.
    Please amend the text.

    See https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference#character_classes

Leave a Reply

Your email address will not be published. Required fields are marked *