Starting a plugin to analyse code
I want to write a plug-in to analyse C# code to provide warnings for particular cases, I've read the documentation and download loaded the SDK (7.1) and had a look at the cyclomatic complexity sample.
I used the R# template to create the project & test project and based it off the structure of the cyclomatic complexity.
I created test data and placed this in the required sub-directory of 'test\data'.
I created a unit test file - 'CodeCleanupTests', this inherits from the R# CodeCleanupTestBase, I've overridden the 'RelativeTestDataPath' property to define where to find the test csharp data.
Am I heading in the correct direction with attempt at writing a plugin to analyse C# code?
Cheers
Ollie Riches
Please sign in to leave a comment.
Well, what you've described sounds right, but that will build a code cleanup module rather than a code analyser. A code cleanup module will allow you to make changes to the formatting of a source file, while a code analyser will allow you to examine the structure of a source file and provide warnings, suggestions, hints and errors for various code constructs.
To create a code analyser, you can right click on your ReSharper project (created by the SDK) and select Add New Item -> Analyser. This will create a class that implements IElementProblemAnalyzer, and allows you to specify a PSI type that you're interested in. In other words, you can tell ReSharper to call your Analyzer for specific objects in the abstract syntax tree of a source file (e.g. IConstructorDeclaration, or IMethodDeclaration, etc). Alternatively, if you'd like to be called for the whole source file, Add New Item -> Code Highlighting Stage. You can think of IElementProblemAnalyzer as a specialisation of a highlighting stage - both add instances of IHighlighting to the source file.
You can then write tests against either of these types using Add New Item -> Highlighting Test. Each test is run against a .cs file, and the output compared with a .cs.gold file, that both live in the test\data folder structure. The gold file marks where the highlights are in the source code and what message is displayed at that point.
Matt - thanks for such a detailed answer
ta
Ollie
Matt - where can i find info on what to put in the 'gold' file for analyser?
ta
Ollie Riches
Sorted - found the sample tests in the SDK
ta
Ollie