ReSharper formatting: align equal operands

There is (currently) no way to do this out of the box in ReSharper. Fortunately, ReSharper has a very rich extensibility API (albeit poorly documented). I’ve spent a lot of time with Reflector trying to figure things out.

We use a similar alignment guideline for class members in a company I work for (to the extreme, we also align method parameters). I wrote a plugin for ReSharper to help me do just that. It’s a “Code Cleanup” module, which runs sometime during the code cleanup (CtrlE, CtrlF) and aligns the code for you. It also makes the class sealed, if possible.

Some examples:

Method parameters:

public void DoSomething(string      name,
                        int         age,
                        IEnumerable coll)

(you will need to change Wrap formal parameters to Chop always in Options->Formatting Style->Line Breaks and Wrapping for this to work properly)

Constants:

private const int    RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
private const int    CONNECT_COMMANDLINE        = 0x00000800;
private const int    CONNECT_INTERACTIVE        = 0x00000008;
private const string RESOURCE_NAME              = "Unknown";


You can download the source code from my SkyDrive.
Edit I seem to have lost access to that SkyDrive, and lost the files too. This was before github 🙁

Please note that you’ll need several things to compile/debug it:

  1. Update the Command Line Arguments
    in Debug tab in Project
    Properties
    with the correct path of
    the output DLL:

    /ReSharper.Plugin
    "X:\<projects>\MyCompany.CodeFormatter\MyCompany.CodeFormatter\bin\Debug\MyCompany.CodeFormatter.dll"

    This allows debugging the plugin via
    F5, and it will be
    automatically installed in
    ReSharper’s Plugins in the new
    Visual Studio instance which will
    open.

  2. The plugin is for ReSharper 4.5 and it references the DLLs of this version. If you installed ReSharper anywhere else except C:\Program Files\JetBrains\ReSharper, you will have to fix the references.
  3. This does not align variables inside methods, but it shouldn’t be hard to add 🙂

After you install this, just run Code Cleanup to fix your alignment (I never got a reply from JetBrains about how to do this during brace/semicolon formatting, unfortunately).

Assembly was renamed to protect the innocent 🙂

Good luck!

Leave a Comment