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!

About Maarten Balliauw

Maarten Balliauw is a Developer Advocate at JetBrains, working on .NET tools and Space. He focuses on .NET, Azure, web technologies and application performance. Maarten is a frequent speaker at various national and international events. In his free time, he brews his own beer. Follow him on Twitter or check out his personal blog.
This entry was posted in How-To's and tagged , , , , , , , , . Bookmark the permalink.

27 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:


  9. Alexey says:

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

  10. Louis says:

    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.

    • Igor Akhmetov says:

      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. Andrew Engebretson says:

    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:

    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?

  12. Nim says:

    I not have this options in 2017.3.3

  13. Leonardo Carreiro says:

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

    Will this feature be available to JavaScript?

  15. Cezar Wagenheimer says:

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

Leave a Reply to Stanislaw Cancel reply

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