In this recipe, we will discover the StyleCop add-in for Resharper. We will see its real-time analysis and how to easily fix most of the StyleCop violations.
For this recipe, you will need to have:
StyleCop 4.7 installed.
Resharper 7.1 installed. Evaluation version available at http://devnet.jetbrains.com/docs/DOC-280.
Visual Studio Professional (2008, 2010, or 2012).
A sample C# project to modify.
Note
Before starting to see the benefit of Resharper with StyleCop, I have to say that the installation is not an easy one. First, each version of StyleCop seems to be tightly built on a specific version of Resharper. At the time of writing, StyleCop Version 4.7 is compatible with Resharper v7.1.1000.900. You can find the compatibility matrix at http://stylecop.codeplex.com/.
Then you need to install them in a specific order to be able to use them. The exact order to install them is to begin with Resharper, and installing StyleCop after. If you didn't do it this way, you will have to remove both products and reinstall them in the right order.
When you open Visual Studio for the first time after having installed Resharper, you will be asked if you want to reset the default settings of Resharper to comply with StyleCop rules. Click on Yes to do so.
Now let's open our sample project. The first thing visible is a new column at the right-hand side of the code screen as shown in the following screenshot:
This column updates in real time and gives you all the errors or warnings you have in your document. If you go over one of the warnings displayed in the file, you will be able to see a description of the warning.
If you click on it, a light bulb icon will appear on the left-hand side of your code and provide you options for dealing with the error. Generally the options are of three kinds for each error:
You can either autofix the rule, which is also marked by a light bulb icon
You can explicitly suppress the error by automatically adding a suppress message in your code. This is marked by a hammer icon
And you can change the level of this kind of error for Resharper. This is marked as a barred light bulb
The following screenshot shows you the options available for dealing with the StyleCop violation:
As this is quite a long task, we can fix in one go most of the violations. To do so, use the Clean up command. This command is accessible in three places:
From the Resharper | Tools menu.
In the Explorer solution, from the context menu.
And from the coding panel's context menu.
Whereas the auto clean up feature of Resharper helps to quickly fix a lot of violations, it won't fix all of them, and you will have to check the remaining violations manually or with the help of the fixing feature of Resharper.
If you want to know what the autofixing capabilities of the Resharper add-in are, you can refer to the following link:
http://stylecop.codeplex.com/wikipage?title=ReSharper%20Fixes&referringTitle=Documentation
You can manage the behavior of the Resharper addin in the Resharper | Option… menu. You have two menus that address StyleCop. The first is the Code Inspection | Inspection Severity menu, which allows you to change the way each StyleCop violation is displayed in Resharper.
The second one allows you to manage how StyleCop is running under Resharper, as shown in the following screenshot:
The most important part in this screen is the Analysis Performance as it allows you to control the resources allocated to the StyleCop analysis.
Though Resharper is certainly the most complete tool to autofix your violations and has the advantage of being directly supported by the StyleCop team, it's not the only program that is able to automatically correct violations. Other tools exist, and can help with StyleCop violation fixing.
This is a direct competitor of Resharper. Through plugins, it can also have some StyleCop compliant violation fixing features. However, at the time of writing, they seem to cover only a small part of StyleCop violations.
You can download Code Rush at the following address:
http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/index.xml
The plugin used to include StyleCop violation fixing is CR_StyleNinja and is available at the following site:
http://code.google.com/p/dxcorecommunityplugins/wiki/CR_StyleNinja
Code Maid is a free Visual Studio add-in allowing you to reformat your code. While it doesn't have a plugin for StyleCop, it allows you to reformat your code in order to remove layout and ordering violations.
You can find the tool at the following address:
Narrange is another code beautifier, but unlike Code Maid, it's not a Visual Studio add-in. All configurations are made in an XML file and you can set up NArrange launch from the Visual Studio external tools menu. The configuration has a tool to simplify its edition.
One of the advantages of this tool is the fact it is not dependant on Visual Studio. You can integrate it with other developing tools such as SharpDevelop or MonoDevelop.
You can download it at the following site: