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.
CSharpier provides a few basic options that affect formatting and has no plans to add more. It follows the Option Philosophy of prettier.
CSharpier is still in active development and the rules it uses to format code are not yet finalized and will change over time.
If you can live with the fact that the formatting will be changing over time, it is safe to use. In addition to a steadily growing set of unit tests; csharpier is tested against ~60k c# files from a range of public repositories to validate it does not result in the lose of any source code.
- It is fast. It can format a solution of ~11,000 files in ~30 seconds.
- It supports validating the syntax of the code it produces to ensure the only changes made were whitespace and line breaks.
- It formats c# <= 9.0 - c# 10 coming soon
Try it out at Playground
public class ClassName {
public string ShortPropertyName {
get;
set;
}
public void LongUglyMethod(string longParameter1, string longParameter2, string longParameter3) {
this.LongUglyMethod("1234567890", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
}
public class ClassName
{
public string ShortPropertyName { get; set; }
public void LongUglyMethod(
string longParameter1,
string longParameter2,
string longParameter3
)
{
this.LongUglyMethod(
"1234567890",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
);
}
}
dotnet tool install -g csharpier
Run csharpier from the directory you wish to format to recursively format all .cs files
dotnet csharpier .
Run csharpier on a specific file
dotnet csharpier /src/UglyUnformattedFile.cs
If you prefer to have csharpier run when a project is built, you can use the CSharpier.MSBuild nuget package
Install-Package CSharpier.MSBuild
It is possible to format code programmatically using the CSharpier.Core nuget library.
using CSharpier;
var unformattedCode = "public class Class2 { }";
var formattedCode = CodeFormatter.Format(unformattedCode);
Command Line Interface
Configuration File
Editors and CI/CD
Integrating with Linters/Formatters
Ignoring Files
ChangeLog
MSBuild Package