File and Type Layout
JetBrains Rider can reorder types and type members in C# files according to different patterns. Patterns can describe a lot of conditions and constraints that are evaluated when reordering items in a file and/or wrapping them with specified regions.
Introduction to layout patterns
You can configure multiple layout patterns to be applied in different contexts (for example, you can have different patterns for classes and interfaces).
Within the patterns, you can specify matcher entries in the desired order as well as regions/groups, into which the matching items are wrapped/grouped.
The algorithm of applying the current pattern set works as follows:
If a file pattern exists, JetBrains Rider checks whether regions in the file should be removed, then it checks whether each of the matcher entries in the pattern matches any code item in the file.
If there are matching entries, their position in the file is changed according to the position of the corresponding matcher item in the file pattern.
If the same code item matches several matcher entries, then the matcher with higher priority or stronger constraints is applied. That is, if there is pattern A that matches public types and pattern B that matches public static types, then all public static types will be matched by the pattern B. To change this, you can raise the priority of the pattern A.
If there are matcher entries with the same set of constraints, the matching items are moved according to the position of the first of such matcher entries.
If regions or groups are specified, the matched items are grouped or wrapped with the regions accordingly.
A group or a region allows you to specify priority for it. For example, if a group/region has higher priority than other matchers outside it, then JetBrains Rider first processes matchers inside the group/region, and then the rest of the matchers. The only difference between group an region is that the region wraps matched items into
Everything that is not matched is moved after the matched items. If you need to put the unmatched items elsewhere, you can create a matcher without constraints and set the desired position for it in the pattern.
After that, type patterns are applied, if any. JetBrains Rider checks whether each of the type patterns matches any type in the file. If there are several matching patterns for a type, the conflicts are resolved similarly to steps 3 and 4.
When the type match is established, JetBrains Rider checks whether regions in the type should be removed, then it checks whether each of the matcher entries in the pattern matches any member in the file.
If there are matching members, their position in the type is changed according to the position of the corresponding matcher item in the type pattern.
Regions, groups and unmatched members are dealt with similarly to steps 5 and 6.
File and type layout preferences
The default member layout rules are based on numerous best practices and can be recommended in most cases. However, if your personal preferences or company standards differ from JetBrains Rider defaults, you can configure code reordering patterns based on the default ones as well as create new patters for specific cases.
All modifications to the layout patterns are done in the page of JetBrains Rider settings Ctrl+Alt+S. After the editing is done, click Save to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see layer-based settings.
Configuring file and type layout by editing the source XAML
We recommend that you load one of the default patterns and check the tags used in this format. They are rather self-explanatory, for example, the
Entry tag defines a matcher entry and the
Entry.Match specifies what code items should be matched. the
Entry.SortBy tag specifies how the matched items should be sorted, and so on. Consider the example below:
This XAML code matches constructors and sorts them in such way that first go static constructors.
Reordering file and type members
To apply your file and type layout settings in the desired scope, use either run code cleanup with the default Default: Full Cleanup profile or create and run a custom profile solely targeted at your specific task as described below.
To apply file and type layout with custom Code Cleanup profile
Go to the Code Cleanup settings page:.
Create a new profile as described in the To create a new custom cleanup profile section. In the Selected profile settings section for the new profile, tick the Apply file layout checkbox. Optionally, you can enable other code cleanup tasks in this profile.
Click Save to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see layer-based settings.
Select the scope where you want to apply file layout:
Set the caret anywhere in the file to apply file layout to the file.
Select one or more items in the Solution Explorer to apply file layout in the files under these nodes and their child items.
Choose Code | Code Cleanup in the main menu.
In the Code Cleanup dialog that opens, select the newly created profile.
Click OK. JetBrains Rider will apply file layout in the selected scope.