Aligning code in columns with ReSharper and Rider

In our previous post, we looked at how we can use different code styles for different blocks of code in ReSharper 2017.3 EAP and Rider 2017.3 EAP. In this post, we will look at some other improvements to the code formatting engine, such as aligning code in columns and a sneak preview of other changes coming in ReSharper 2017.3.

This post is part of a series around changes in the ReSharper and Rider code formatter:

Some developers like to have properties in a class aligned in columns, e.g. the { get; set; } starting in the same column. Others like to align assignments in columns. ReSharper 2017.3 EAP now supports all of those flavors! Let’s reformat this entire file into columns, using the Reformat Code action (Ctrl+Alt+Enter):

We can configure these options in ReSharper’s settings under Code Editing | C# | Formatting Style | Tabs, Indents, Alignment, then Align Similar Code in Columns.

Code formatting rules not only apply when we write code. When generating code, ReSharper uses these settings as well. For example, when we enerate a constructor (Alt+Insert) ReSharper will use the column settings we configured:

26 Responses to Aligning code in columns with ReSharper and Rider

A very common use case I run into is in writing unit tests with many [TestCase()] attributes. I want to align commas across all test cases so the arguments are easier to read and to block edit. It would be great if R# could support this style.

We align many things other than equal signs – types, names, bodies, comments, binary and ternary operators. As for your case, there is an option named called “Invocations of the same method”, see options on the screenshot in the blog posts. It should also work for commas in attributes’ constructor. But those attributes must be on adjacent lines, no blank lines between them. Please try EAP and tell us if it works for you.

StyleCop is not really designed to be configurable in this manner. It has a fairly rigid set of rules, indeed, that’s the point – it wants everyone’s code to look the same.

But you can disable some rules. You’d have to find the ones that intersect with this ReSharper formatting style and disable them in the settings.stylecop file. This is something you’ll need to look up in the StyleCop help, or follow up with the StyleCop team.

ReSharper is designed to be more flexible than StyleCop. You can set both your formatting and code style (syntax and semantics) and share that with your team or project. It doesn’t enforce things to the same level as StyleCop, so it’s a lot less shouty, and it also doesn’t cover all of the rules that StyleCop uses, but it does give you more flexibility to have things how you want them, but still neat, tidy and consistent.

Nice, very nice!
I am now missing only one thing.
I manually added “// @formatter:int_align_fields true” to a class and figuring out “int_align_fields” and that it has to be lower case is pretty unintuitive.
Which there was some feature to at least give me the name (like a context menu in the settings window)

I see the aligning code in columns feature is available for C++ too in the Options. But why it is disabled in the menu Resharper/Edit/Reformat Code when I have checked all the options? I’m using 17.3.2 Ultimate.

The aligning code in columns features is a great addition to Reshaper, kudos! I’m having an issue with aligning code into columns for class properties. In particular, it’s grouping attributes and access modifiers into the same column, not separate columns. For example:

1

2

3

4

5

6

publicclassPerson

{

[RequiredField]publicstringName{get;set;}

publicintAge{get;set;}

publicstringCountry{get;set;}

}

I would expect the access modifiers to all be aligned in the same column different than attributes. Any advise on whether I might have a setting wrong or there maybe a bug in the feature?

if (CheckKey(KeyCode.RightArrow)) SelectionArea.instance.MoveRight();
if (CheckKey(KeyCode.LeftArrow)) SelectionArea.instance.MoveLeft();
if (CheckKey(KeyCode.UpArrow)) SelectionArea.instance.MoveUp();
if (CheckKey(KeyCode.DownArrow)) SelectionArea.instance.MoveDown();
if (CheckKey(KeyCode.X)) SelectionArea.instance.FlipX();
if (CheckKey(KeyCode.Y)) SelectionArea.instance.FlipY();
if (CheckKey(KeyCode.D)) SelectionArea.instance.Duplicate();