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!

This entry was posted in How-To's and tagged , , , , , , , , . Bookmark the permalink.

17 Responses to Aligning code in columns with ReSharper and Rider

  1. Sam says:

    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.

    • Dmitry Osinovsky says:

      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.

  2. cristobalito says:

    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?

    • Matt Ellis says:

      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. Martin says:

    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. JC says:

    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. Stanislaw says:

    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

  6. Paul Creyghton says:

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

    • 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. Alejandro says:

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

  8. Alexey says:

    Hello. How format this:

    to style:

    Thanks.

  9. Alexey says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *