Dotnet, C#, code format on JetBrain IDE Rider
Tags: dotnet, csharp, jetbrain, codereview
Published: 2023-01-13 04:25:54

Code review is an unavoidable topic at work for developers. Coding style aside, if the code format is different, that would add difficulty to the code review.

Since then, this article has been about the basic code format for everyone in the same team, which can go through different IDE and platforms. Especially for Jetbrain IDE of C#, Rider. (If anyone is interested in VSCode, we can talk about it later).

Here, I'd like to combine **_CSharpier_** and **_Dotnet format_**.

Let's get started!
<br>
<br>

## **[CSharpier](https://github.com/belav/csharpier)**

### What is CSharpier?

CSharpier is an opinionated code formatter for c#. It uses [Roslyn](https://github.com/dotnet/roslyn) to parse your code and re-prints it using its own rules. The printing process was ported from prettier but has evolved over time. 
<br>

### **Quick Start**

Install CSharpier globally using the following command.

```
dotnet tool install csharpier -g
```

Then format the contents of a directory and its children with the following command.

```
dotnet csharpier .
```

[CSharpier on Rider Plugin](https://plugins.jetbrains.com/plugin/18243-csharpier)

Steps:
Open Settings -> Plugins -> Search "CSharpier" 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g444npvk21e7mfp49t4l.png)

After installation, please "On Save" is turned on.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2bugmyqwfmx4spif2yg6.png)
<br>
<br>

##**[Dotnet Format](https://github.com/dotnet/format)**
<br>

### What is Dotnet Format?

dotnet-format is a code formatter for dotnet that applies style preferences to a project or solution. Preferences will be read from an .editorconfig file, if present, otherwise a default set of preferences will be used. At this time dotnet-format is able to format C# and Visual Basic projects with a subset of [supported .editorconfig options. ](https://github.com/dotnet/format/blob/main/docs/Supported-.editorconfig-options.md) 
<br>

### **Quick Start**

Install dotnet format
```
dotnet tool install -g dotnet-format
```

Steps:

1.Open Settings -> Tools -> File Watchers

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9yclap2qqy8eowet9klj.png)




2.Press The “Plus Sign” to Add a Custom watcher
   Set the name to “dotnet format on save”
 - FileType: C#
 - Scope: Open Files
 - Program: Write dotnet-format
 - Arguments: $SolutionPath$ -verbosity diagnostic--include $FileRelativePath$   
     - Append --fix-style warning to fix any style issues automatically on save.
     - Append --fix-analyzers warning to fix any analyzer warnings on save.         
  - Advanced (Options)
     - Disabled all advanced option checkboxes.
  - All other values were left default


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0e7go17e79n3hmc5bme.png)

3.Press Ok, and Save Settings.
<br>
<br>

 
That's it!
Any feedback is welcome. Happy Coding :metal:!
<br/>
<br/>
<h6> Dotnet Format setup reference:</h6>
https://strepto.github.io/Pause/blog/dotnet-format-rider