DEV Community

Cover image for Dotnet, C#, code format on JetBrain IDE Rider
TSOTSI1
TSOTSI1

Posted on • Updated on

Dotnet, C#, code format on JetBrain IDE Rider

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!



CSharpier

What is CSharpier?

CSharpier is an opinionated code formatter for c#. It uses 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.

Quick Start

Install CSharpier globally using the following command.



dotnet tool install csharpier -g


Enter fullscreen mode Exit fullscreen mode

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



dotnet csharpier .


Enter fullscreen mode Exit fullscreen mode

CSharpier on Rider Plugin

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

Image description

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

Image description



Dotnet Format

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.

Quick Start

Install dotnet format



dotnet tool install -g dotnet-format


Enter fullscreen mode Exit fullscreen mode

Steps:

1.Open Settings -> Tools -> File Watchers

Image description

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

3.Press Ok, and Save Settings.



That's it!
Any feedback is welcome. Happy Coding 🤘!



Dotnet Format setup reference:

https://strepto.github.io/Pause/blog/dotnet-format-rider

Top comments (4)

Collapse
 
isaacdlyman profile image
Isaac Lyman

1 - As another commenter mentioned, there's now a format utility built into .NET. The new command is dotnet format, without the hyphen.

2 - If you don't specifically need the command-line version of dotnet format, Rider understands the same .editorconfig file format that dotnet format uses, so you can easily turn it on in Jetbrains Rider > Settings > Tools > Actions on Save > Reformat and Clean up Code.

Collapse
 
hasankaraoglu profile image
Hasan KARAOĞLU

Not working. It gives me error like below:
"Error: Invalid executable"

Image description

Image description

Collapse
 
tsotsi1 profile image
TSOTSI1

This picture looks like it's from an Apple device. I don't know if it will look the same on other systems. Just so you know, I'm using Windows for this example.

Collapse
 
mathiash98 profile image
Mathias Haugsbø

dotnet-format is not a command in dotnet6+ anymore. The new correct command is dotnet format and so the program should be dotnet and arguments should be prefixed with format