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:
- Code formatting engine updates in ReSharper and Rider
- Different code styles for different blocks of code
- Aligning code in columns
- Configuring code formatting from code selection
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):
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:
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).
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!
Sam says:
November 29, 2017Will 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:
November 29, 2017We 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.
Sam says:
December 4, 2017I 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.
Dmitry Osinovsky says:
December 5, 2017I’ve created an issue in our issue tracker. Feel free to vote 🙂 https://youtrack.jetbrains.com/issue/RSRP-467438
cristobalito says:
November 29, 2017One 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:
November 29, 2017StyleCop 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.
Martin says:
November 29, 2017Nice, 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)
JC says:
December 19, 2017You 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?
Dmitry Osinovsky says:
January 8, 2018Thanks for a suggestion, we’ll definitely take a look into implementing it. I’ve create https://youtrack.jetbrains.com/issue/RSRP-467836, feel free to vote to track progress.
Stanislaw says:
December 20, 2017I’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
Maarten Balliauw says:
December 21, 2017In RTM we moved it to the “Code Editing | C# | Formatting Style | Tabs, Indents, Alignment” settings instead.
Paul Creyghton says:
December 21, 2017Same problem here, these options cannot be found, and the ctrl alt enter is not working either
Maarten Balliauw says:
December 21, 2017Check 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.
Alejandro says:
December 21, 2017I really like this feature, but could be great if it works with tabs.
Alexey says:
January 12, 2018Hello. How format this:
to style:
Thanks.
Maarten Balliauw says:
January 12, 2018Checking with a few colleagues, but I think it is still pending – check https://youtrack.jetbrains.com/issue/RSRP-467836 for more info
Alexey says:
January 12, 2018The robot broke my example. I wanted to align the second column of the dictionary.
Louis says:
February 12, 2018I 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:
February 19, 2018Hello 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.
Andrew Engebretson says:
February 21, 2018The 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?
Maarten Balliauw says:
February 22, 2018That 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: https://youtrack.jetbrains.com/issue/RSRP-468481
Gauthier Segay says:
April 24, 2019Andrew, maybe making a C# language suggestion to allow attribute after the visibility modifier?
Nim says:
March 27, 2018I not have this options in 2017.3.3
Maarten Balliauw says:
March 27, 2018In RTM we moved it to the “Code Editing | C# | Formatting Style | Tabs, Indents, Alignment” settings instead.
Leonardo Carreiro says:
March 29, 2018I 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 https://marketplace.visualstudio.com/items?itemName=lmcarreiro.vscode-smart-column-indenter or my blog post about it: https://medium.freecodecamp.org/how-to-columnize-your-code-to-improve-readability-f1364e2e77ba
Terry says:
July 25, 2018Will this feature be available to JavaScript?
Cezar Wagenheimer says:
August 29, 2018Is 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();