Nicole Calinoiu
October 2005 (last edited July 2006)
Introduction
Installing the add-in
Updates
Loading the add-in
Configuring the sorting behaviour
Sorting the imports/using directives in a file
Known issues
Visual Studio 2005 introduces new functionality for automatic generation of imports/using directives when types cannot be resolved. This is a great little addition to the coding UI, but it can pose a bit of a problem for the anal-retentive nit-pickers amongst us since the new directives simply get added to the end of the imports/using directive block. Of course, this causes no problem whatsoever for the compiler, but it's not particularly pretty either. (And "pretty" is, of course, the single most important goal for any code file. <g>)
I happen to be rather picky about what my imports/using blocks look like. In particular, I like to sort the directives in alphabetical order by namespace within the following four groups:
In previous version of Visual Studio, maintaining this sequence wasn't too much extra work since I already had to scroll to top of a code file anyway to add a new directive. However, with the new automated directive insertion, scrolling to the top of the page just to sort the imports/using block manually after an automated directive addition started to feel very inconvenient. Unfortunately, the mere suspicion that my imports/using block might be out of sequence turned out to be a horrible distraction for me, and I was ending up doing a manual sort after each automated addition. After a few days of this nonsense, I figured it was about high time to automate the sorting, and the Bordecal.ImportsSorter add-in was born.
The current version of the add-in has been compiled and tested on the Visual Studio 2005 RTM build (version 8.0.50727.42).
The add-in comes packaged as an MSI, which can be downloaded from http://bordecal.mvps.org/Nicole/ImportsSorter/Bordecal.ImportsSorter.Setup.msi. It can be installed either as an administrator or a normal user. If you choose to install as an administrator, the add-in files will be placed in the %ALLUSERSPROFILE%\Application Data\Microsoft\MSEnvShared\Addins directory, and the add-in will be available for all users on the machine. If the installation is run under a normal user account, the add-in files will be placed in the user's <user's personal directory>\Visual Studio 2005\Addins directory, and the add-in will only be available to that user.
The add-in does not perform automatic self-updates. New releases will be advertised on my blog at http://msmvps.com/blogs/calinoiu/, along with MD5 and SHA1 hashes for the new MSI download. If you're not interested in reading my blathering about security nonsense, you might want to consider to subscribe to the RSS feed dedicated to the Bordecal.ImportsSorter add-in.
The add-in is pre-configured to load automatically at Visual Studio startup. You can manage its startup behaviour via the Tools...Add-in manager... menu item in Visual Studio 2005. If you encounter problems attempting to load the add-in, verify that the following criteria are met:
The add-in configuration dialog can be opened from the Configure the imports/using block sorter... item that should appear under your Visual Studio 2005 Tools menu once the add-in has been loaded. The configuration dialog allows you to specify the sorting order for the imports/using directives, the number of lines (if any) that will be inserted between sorting groups when the sorted directives are written back into your code files, and options for post-sorting display of the imports/using directives.
If you're even remotely interested in using this add-in, I'm guessing that you probably have a pretty good idea of what the sorting groups do. Just in case you're having any doubts, here are some examples of the results that various "typical" configurations would yield on a C# using directive block:
| Configuration | Sorting results | |
|---|---|---|
| Sorting groups | Inter-group line count | |
|
System Microsoft <All other namespaces> YourNamespace |
0 |
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Text; using Microsoft.SqlServer; using CrystalDecisions; using YourNamespace; using YourNamespace.Data; |
|
System Microsoft <All other namespaces> YourNamespace |
1 |
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Text; using Microsoft.SqlServer; using CrystalDecisions; using YourNamespace; using YourNamespace.Data; |
|
System System.Data Microsoft <All other namespaces> YourNamespace |
1 |
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.OleDb; using Microsoft.SqlServer; using CrystalDecisions; using YourNamespace; using YourNamespace.Data; |
|
System Microsoft <All other namespaces> |
1 |
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Text; using Microsoft.SqlServer; using CrystalDecisions; using YourNamespace; using YourNamespace.Data; |
| <All other namespaces> | 1 |
using CrystalDecisions; using Microsoft.SqlServer; using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Text; using YourNamespace; using YourNamespace.Data; |
Some of the configuration aspects that might not be immediately obvious are:
To sort the imports/using directives in an open code file, display the code window shortcut menu (say, by right-clicking on the code editing surface), and select the Sort the imports/using block item. The exact labelling of this menu item will depend on the language of the code file for which it has been displayed, with the language-specific term for an imports/using directive being displayed. The language-specific labels are:
| Language | Menu item label |
|---|---|
| C# | Sort the using block |
| J# | Sort the import block |
| VB.NET | Sort the Imports block |
If you would like to return to the pre-sorting state of your file, the entire sorting operation may be reversed in one undo step, via the usual undo mechanisms (CTRL+z, toolbar button, etc.).
If your attempt at sorting the imports/using directives in a file appears to do nothing, you should check that there are no syntax problems before or amongst the directives since such issues can prevent the recognition of the intented directives in the code model.
Interestingly enough, the C# code model is actually able to detect a using directive located anywhere outside a class declaration (even though such placement results in a compilation error). This has the somewhat convenient consequence that you may type a using directive at the end of your class or namespace code before running the sort, and it will still be sorted into the using block at the beginning of your file. Unfortunately, neither the VB nor the J# code models identify a similarly "misplaced" directive in their code elements collections, so this will only work in C#.