ReSharper formatting: align equal operands
Asked Answered
W

6

28

Note to Googlers, this question is somewhat out of date as the requested feature is now supported in the current version of ReSharper 2017.3.1

I like to formatting my code to align right side of equal operands.
Like here:

bool    canRead     = false;
bool    canReadClass    = true;
string  className   = boType.Name;

I've switch to ReSharper recently and found it very useful but cannot find option allowing me format code in described way.

Do you know if there is such option / plugin?
Maybe you know other than ReSharp solution allowing that?

EDIT:
How to decide what part of code shall be aligned?
My convention is aligning all variables in same block.
By "block" I meant part of code not divided by empty lines.

eg

// First block
int      count     = 10;
string   name      = "abc";
bool     calculate = true;
.....
.....
// Second block
MyOwnType    myType   = new MyOwntype();
int          count    = 10;

EDIT -2
I've opened R# ticket for this. If anyone interested please vote!

Wilda answered 17/8, 2009 at 8:38 Comment(1)
Note to Googlers, ReSharper 2017.3.1 supports this featureBlackball
H
11

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 (Ctrl-E, Ctrl-F) 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!

Hardden answered 17/8, 2009 at 15:19 Comment(6)
Thank you VERY much for sharing your solution! Really much appreciate! BTW I compiled code, changed cmd line to point my dll but it seems not installed after F5. What I'm doing wrong ?Wilda
Okay - I've copied solution to C:\ drive and now seems working - gonna test it now!Wilda
I've just tested and plugin work as expected. Thanks. Unfortunately (as you mentioned) it not align variables inside methods. Looked quickly into your code and it is not so easy for me to add this. Need to learn more about RS's semantics. But I'll definitely try and share community if I did it. :)Wilda
I'm glad this is working for you. I haven't looked at it yet, but I think there might be a problem aligning variables - how will you know which ones to align? Unless you define all your variables at the top of the method, this won't really work well. Perhaps this could be implemented as a "quick fix" - highlight the variables you want to align, press Alt-Enter and voilla! :) I'll look into it later :)Hardden
See my update to find the rule how to align variables. Quick Fix option sound also reasonable!Wilda
Note to Googlers, this is currently out of date information. ReSharper 2017.3.1 supports this featureBlackball
B
11

I think it is worth noting that the Visual Studio Productivity Power Tools have an Align Assignments feature. Here's a link to the Visual Studio 2013 Productivity Power Tools.

Bernetta answered 21/7, 2010 at 11:52 Comment(4)
Wow. This seems to be what I've looked. Unfortunately we have no VS2010 in my corp :(Wilda
Source code of this extension is also available. See VS Gallery link visualstudiogallery.msdn.microsoft.com/en-us/…Polysyndeton
I would give this +100 if I could. Thanks for this.Didi
These links are dead, including the comment about source code ; you can find the 2017/2019 version here : marketplace.visualstudio.com/… and the source code here : github.com/Microsoft/VS-PPTJenjena
P
9

You can try this: Code Alignment

It supports

  • Align by... (Dialog)
  • Align by position... (Dialog)
  • Align by Equals
  • Align by m_
  • Align by "
  • Align by .
  • Align by Space
Packaging answered 6/12, 2013 at 15:27 Comment(2)
This looks good. Only disadvantage I found is it uses spaces to align but i prefer TABs...Wilda
tabs are not good for this, because if someone else have different width of the tabs, it will not be aligned. Tabs are good only on the beginning of line.Leu
F
3

Productivity Power Tools 2012 also has a command for this: ctrl-alt-]

Other goodies are obviously there as well.

Frymire answered 8/1, 2014 at 16:48 Comment(2)
I know this is old. I just came across this. I already had Power Tools installed, but didn't know it had the alignment thing. I found that in the settings, but not the shortcut key. That is greatness.Fragmental
Version 2017/2019 : marketplace.visualstudio.com/…Jenjena
M
1

As far as I know, this is unfortunately not possible using Resharper.

Misteach answered 17/8, 2009 at 12:54 Comment(1)
Note to Googlers, this is currently out of date information. ReSharper 2017.3.1 supports this featureBlackball
K
1

Years late, but further to the comment from @MickyD, Resharper can do this for you, see Resharper blog. Go to Resharper/ Options/ Code Editing/ C#/ Tabs, Indents, Alignment. Scroll to the bottom of the options in the right hand window pane to find "Align Similar Code in Columns", click things, enjoy.

Kistner answered 24/5, 2019 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.