Question

In recent versions of Visual Studio, you can categorize unit tests by tagging the tests with attributes. It shouldn't matter whether you're using MSTest or NUnit, but this example is based on NUnit.

[Test]
[Category("category 1")]
public void Null_IsNull()
{
    Assert.IsNull(null);
}

Now suppose that I have around 50 tests in category 1, spread across a number of files. Is there an idiot proof way (*) to rename my category to something more descriptive ?

(*): not a simple search and replace

Things that I've tried

search and replace

It works, but I don't feel comfortable doing this.

Was it helpful?

Solution

I think the conclusion is that Visual Studio as of yet doesn't have an out-of-the-box-and-idiot-proof way to do what you are aiming to do. Everything to do it is there, but there is no ui or commandline or intermediate window to do it. Other tools like Resharper, DevExpress CodeRush offer an extensibility model that's easier to use than Visual Studio itself. Roslyn, the new compiler framework from Microsoft and teh editor that is based upon it, will offer features close to what you want in the future.

From these options, I suppose that Resharper's "Search With pattern" feature is the closest to what you're looking for, you can even save your pattern as a quick-action for later re-use, which is pretty cool (so it would be rather easy to create a "Rename All" refactoring or quick-action int he correct context.

Search with Pattern

Other than that you could use Visual Commander and write a Macro to do what you're after. The editor and all files in the solution are reachable through the Visual Studio Object Model.

And you could look into Roslyn to learn how this might be possible in a future version of Visual Studio.

OTHER TIPS

This probably won't be exactly what you need but this is how I handle renaming, when I'm not completely sure if I want to rename all the found instances:

  • I keep left hand on F3 to search for next occurrence of the searched term
  • I keep my right hand on the mouse and on the button next to replace all which is replace next

Then I just press F3 if I don't wish to replace the currently found item or I press the mouse on the replace next button if I do wish to replace.

For 50 items I would probably use the above method, with more items (or more categories to rename) I would be searching for a solution as well.

Also narrowing the scope to "Current document" or opening only the files that need the rename and using the "Currently open documents" works well with the described method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top