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:

Aligning code in columns

Another very popular group of code formatter feature requests was around aligning code. There were requests to align equal operands, to implement outdenting and make code using the ternary operator look nicer, and several other requests to make code more readable.

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):
Reformat code in columns

We can configure these options in ReSharper’s settings under Code Editing | C# | Formatting Style | Tabs, Indents, Alignment, then Align Similar Code in Columns.
Align 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:
Generate constructor action

What else is coming?

Another request we received and is quite popular is to insert a blank line before or after a { } block. This feature will light up in the next EAP build (tip: you can use the JetBrains Toolbox App to auto-update to the latest EAP).
Insert blank line after block

In our next and last post of this series, we’ll look at an existing ReSharper feature that helps make sense of the overwhelming amount of code formatter options.

Download ReSharper 2017.3 EAP or Rider 2017.3 EAP that keeps up with ReSharper’s formatter changes. We’d love to hear your feedback!

Comments below can no longer be edited.

27 Responses to Aligning code in columns with ReSharper and Rider

  1. Avatar

    Sam says:

    November 29, 2017

    Will the alignment work for things other than equal signs?

    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.

    • Avatar

      Dmitry Osinovsky says:

      November 29, 2017

      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.

      • Avatar

        Sam says:

        December 4, 2017

        I tried this out in the EAP. It aligns the first character of each argument in the attributes on multiple lines, but it does not align the commas between arguments.

        This is not ideal because you really need the parenthesis, commas, and closing bracket all aligned in order to effectively block edit multiple lines at once.

  2. Avatar

    cristobalito says:

    November 29, 2017

    One problem with this style is that it falls foul of some StyleCop formatting rules – don’t have access to the particulars off hand. Any suggestions on how one might be able to mix styles?

    • Avatar

      Matt Ellis says:

      November 29, 2017

      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.

  3. Avatar

    Martin says:

    November 29, 2017

    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)

  4. Avatar

    JC says:

    December 19, 2017

    You missed a very important one, align key-value pairs in a dictionary.
    For example,
    var dict = new Dictionary
    {“abc”, “abc1”},
    {“cde”, “cde1”},
    {“opq”, “opq1”},

    or with another initializer,

    var dict = new Dictionary
    [“abc”] = “abc1”,
    [“cde”] = “cde1”,
    [“opq”] = “opq1”,

    Is it already added to latest Resharper? Will you consider to add it if not?

  5. Avatar

    Stanislaw says:

    December 20, 2017

    I’m just upgraded to 17.3 and settings Code Editing | C# | Formatting Style | Other does not contain anything similar to Align Similar Code in Columns

    • Avatar

      Maarten Balliauw says:

      December 21, 2017

      In RTM we moved it to the “Code Editing | C# | Formatting Style | Tabs, Indents, Alignment” settings instead.

  6. Avatar

    Paul Creyghton says:

    December 21, 2017

    Same problem here, these options cannot be found, and the ctrl alt enter is not working either

    • Avatar

      Maarten Balliauw says:

      December 21, 2017

      Check the “Code Editing | C# | Formatting Style | Tabs, Indents, Alignment” settings instead (has moved for RTM). Regarding keyboard shortcut, you may have to re-apply the ReSharper keyboard scheme.

  7. Avatar

    Alejandro says:

    December 21, 2017

    I really like this feature, but could be great if it works with tabs.

  8. Avatar

    Alexey says:

    January 12, 2018

    Hello. How format this:

    new Dictionary(){
    	{Part.Hood, Names_Hood},
    	{Part.LeftDoor, Names_LeftDoor},
    	{Part.RightDoor, Names_RightDoor},
    	{Part.RearDoor, Names_RearDoor},

    to style:

    new Dictionary(){
    	{Part.Hood,			Name_Hood},
    	{Part.LeftDoor,	 	 Name_LeftDoor},
    	{Part.RightDoor,	 Name_RightDoor},
    	{Part.RearDoor, 	 Name_RearDoor},


  9. Avatar

    Alexey says:

    January 12, 2018

    The robot broke my example. I wanted to align the second column of the dictionary.

  10. Avatar

    Louis says:

    February 12, 2018

    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.

    • Avatar

      Igor Akhmetov says:

      February 19, 2018

      Hello Louis! If the menu item itself is grayed out then ReSharper C++ is probably disabled on the current file, likely because it’s using C++/CLI or C++/CX.

  11. Avatar

    Andrew Engebretson says:

    February 21, 2018

    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:

    public class Person
        [RequiredField] public string Name    { get; set; }
        public                 int    Age     { get; set; }
        public                 string Country { 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?

    • Avatar

      Maarten Balliauw says:

      February 22, 2018

      That would require an attribute on each property I’m afraid, not only for the first one. I logged an issue for this, feel free to follow:

    • Avatar

      Gauthier Segay says:

      April 24, 2019

      Andrew, maybe making a C# language suggestion to allow attribute after the visibility modifier?

  12. Avatar

    Nim says:

    March 27, 2018

    I not have this options in 2017.3.3

    • Avatar

      Maarten Balliauw says:

      March 27, 2018

      In RTM we moved it to the “Code Editing | C# | Formatting Style | Tabs, Indents, Alignment” settings instead.

  13. Avatar

    Leonardo Carreiro says:

    March 29, 2018

    I saw that a lot of bugs is because you are aligning some specific types of statements. I made an extension for Visual Studio Code that does the same thing, but my approach was different. The user selects the code and I use the Longest Common Subsequence (LCS) algorithm to identify what can be columnized. Please, take a look at the extension or my blog post about it:

  14. Avatar

    Terry says:

    July 25, 2018

    Will this feature be available to JavaScript?

  15. Avatar

    Cezar Wagenheimer says:

    August 29, 2018

    Is it possible to Align multiples If on the same column?

    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();

Discover more