Personally I am a huge fan of Rider, the IDE from JetBrains when developing Unity games, C# code or even Angular and NodeJS applications. One of the things I like is to organise my methods, properties, fields etc into regions and sorted by name, access type and so forth. Luckily Rider can do this for you and much more. In this post I will show you how to do it yourself.
How to automatically cleanup your Unity C# code using Rider
Code style defines how you want to validate, reformat and refactor your code for a specific language. In Rider this means that you can define how to:
- Work with tabs, indents and alignment
- What casings to use for naming
- What warnings, errors and hints to generate when writing your code and during code inspection
- How you want your braces to appear
- How you want your lie braks and wrapping and spaces
- How to check for null values in asserts, contracts, expressions and so on
- How you want the layout of your code
Code cleanup is the process of running one of your Code style configurations for a selection, a file, a folder, a solution or just for everything. In the Code cleanup configuration you define which parts of the Code style as defined above you want to apply.
In this post I will focus on applying a custom layout to my code. And believe me, it is worth it!
Defining a custom file layout
A file layout defines how to organise your methods, fields, properties, imports, namespaces, classes, types, etc etc. In Rider this means that your will define a set of logical rules where you define how to match things and what you want to do with them.
Riders comes out of the box with two file layouts but none of these worked the way I wanted. First of all, when you have organised your code into regions, none of these two default layouts will actually work and your regions will even be removed.
In my case I was looking for a specific layout when developing C# code. I had also a wish for grouping specific methods which are defined by Unity when developing games in C#. Nevertheless, it should work just fine for regular C# code as well.
My requirements
- Create regions in a specific order
- Unity events: this region is meant for Unity specific lifecycle functions like Start, Awake, Update etc. Basically setup and teardown methods. I want these sorted by name.
- Actions: this region contains all the methods which are not events. You can name it whatever you like of course. I want these members to be sorted by name.
- Events: this region contains all the methods which are used for events. In my case these methods start with ‘On’ like ‘OnResignClicked’. I want these members to be sorted by name.
- Public properties, indexers, fields and enums: this region contains my public properties etc. In Unity these will show in the inspector and changing them might break your game scene if you have assigned values through the inspector and not through code. That is why I want them separate and sorted by name again.
- Private properties, indexers, fields and enums: this region contains all the private properties etc. So these will only be used in your class. I want these sorted by name as well.
- Delegates: in this region I will group all delegates and sort them by access type and then by name.
- Nested types: Sometimes I nest types and I want these to show in the last region also sorted by name.
As you can see, these regions group members into logical sections. And this is just my way of doing things. You can easily change this to your liking once you know the appropriate syntax. So let’s show you how to define the file layout.
The file layout syntax
In Rider you define the file layout as a XML document.
The XML document contains Type patterns. Everything that the Code cleanup will encounter in your code is called an Entry. So when you want to select for instance all the methods which start with ‘On’ then you have to define an Entry match. If you want to sort the result entries then you have to use an Entry sort by. Let’s have a look at an example:
As you can see I am using an AND tag and a KIND and NAME tag. The KIND tag I am using to select methods. I don’t want properties for instance. The Name tag uses a regular expression. Here I select all methods which have a name that starts with On. It will automatically select for instance a method called OnButtonClicked.
If I would like to group these methods in their own region, I would use a REGION tag:
This will group all methods which start with On into a region with the name “03. Events”.
Now let’s have a look at how to group all public properties etc into their own region:
As you can see I am using a combination of AND and OR tags as well as a new tag called ACCESS,
The ACCESS tag is used to select Public entries. In this case entries are defined as either a Property, Index, Field or Enum.
To sort every property, I am using an ENTRY.SortBy tag and a NAME tag inside. You can use multiple tags inside the .SortBy tag.
Now that you know how to define a file layout, I will show you the final result of the layout that I am using below. Before I do that I will explain how to create this in Rider IDE.
Adding file layout to Rider IDE
- Go to the Settings of Rider (CMD + , on Mac)
- Go to Editor -> Code Style -> C#
- Select File layout tab
- Select Default pattern
- Delete all of the XML and paste the one shown below or create your own
- Go to Code cleanup under Editor setting
- Verify if the built-in cleanup configurations have the Apply File Layout checked
- Personally, I have created my own configuration by clicking on the +-sign
- Under C# make sure you have Apply file layout checked.
Cleaning up your code
There are various ways to trigger the cleanup of your code:
- You can select Code -> Code Cleanup from the menu
- Then select a scope: either Whole solution, Project, Uncommitted files, File or a custom scope
- Select your own profile or one of the build in profiles and click OK
Usually, this is the first thing that I do: select scope File and then my own profile. Click OK. This setting is now default.
The next time I am working on a file I just press CMD + E and the C for code cleanup popup or CMD + E and then F for a silent code cleanup (usually).
Another way of triggering Code Cleanup is through a Commit or Push. In Rider you can select Code Cleanup when pushing your code to a VCS / GIT repository.
The benefits
Previously I was spending a lot of time to keep my files and classes consistent. And most of the times I forgot to organise things in the correct way, ending up with different coding styles across files.
With this approach you can create any method, property etc wherever you want and finally press CMD + E and then F to reorganise things. I also spent some time to configure the other Code Style tabs so that things like empty lines, braces, new lines, wrapping etc are applied automatically as well.
The nice thing is: once you have defined it like this you can let it run on all files at once! How nice is that.
So, I hope you like this post and feel free to leave a comment if so. Here is the resulting File Layout template that I am using.
The final File layout