Sorting using directives by assembly and length with Roslyn

Sorting using directives by assembly and length with Roslyn

but-why.gif

Exactly, why would I do that? Usually people don't care about them, I know I didn't until I started my internship at Thinslices a few years ago. That's when my mentors explained to me that when I go into a new company I should adopt their coding guidelines and this was one of them so I went along with it. It may sound silly, but I actually liked it and I did it for a few years even after I left the company, then I mostly gave up because it was a waste of time to do it manually. If you don't understand what I mean, here's an example, I had to transform this: into this: It looks better, right? But as I said, it's not something that I want to waste time on, so I've wondered if there's a way to sort them using Resharper or some other plugin, and although I found options to sort by assembly or removed unused references, I haven't found an option to sort them by length. Crazy, right :D? It's like we're the only people who would want this. So as I said, I stopped doing this manually...until a few weeks ago when I attended a Codecamp presentation about Roslyn and then it hit me...

I'll make my own roslyn plugin for sorting usings...with analyzers and code fixes

So let's get started. There's a ton of documentation and articles about Roslyn, you'll find in there how to setup your environment and some nice examples, so I'm not going to repeat that stuff. Once your environment is ready, go ahead and create a new project of type "Analyzer with Code Fix" named "MyCodeAnalyzers". Visual Studio will create 3 projects: one for the analyzer and the code fix, one for the unit tests, and another one for the extension. The analyzer is very simple, we just need to get the syntax tree of a file, grab its usings and create a diagnostic report for each one that isn't ordered correctly. Now you can hit F5 and open the Hive, a clean Visual Studio instance that will only have your extension installed. You can create or open an existing project, then go to a .cs file and if the using directives are not ordered correctly, you should see something like this:

usings.png

So for each using that is not placed correctly we're shown the yellow light bulb suggesting that something is wrong and that there's also a code fix available. Once a diagnostic report is created we can register a code fix for it, so to do that we'll open UsingsAnalyzerCodeFixProvider.cs and override the function RegisterCodeFixesAsync; Our function GetTransformedDocumentAsync will receive the current document and will return a modified document with our code fix. To do that we'll have to get the usings as we did in the analyzer, then sort them and replace them if we find any that are not positioned correctly. Also, we'll add a new line between usings from different assemblies. The function will be invoked once when Visual Studio shows a preview of the fix, and another time when it is applied. Here's how it should work:

analyzer-2.gif

Once you got it working in the Hive, the next step is to use it in your primary Visual Studio instance. To do that, you'll need to install the analyzer as a .vsix extension, so we'll have to build the MyCodeAnalyzers.Vsix project in Release mode, and then go to its bin/Release folder to find our extension. Just install it and you can enjoy sorted usings from now on! If you want to skip the steps of cloning and building the solution, you can download the extension from my github account or from the Visual Studio Gallery. I'll try do add more custom analyzers and if you have any ideas feel free to contribute or submit an issue in the github repo.

Did you find this article valuable?

Support Bogdan Bujdea by becoming a sponsor. Any amount is appreciated!