myesn

myEsn2E9

hi
github

CSharpier Code Formatting Tool

Background#

In Visual Studio, formatting the content of the current file is generally done using the default shortcut Ctrl + K + D.

For me personally, code formatting is very important as it affects my reading experience. However, some of my colleagues and senior members of the company I work for do not pay much attention to code formatting, which leaves me speechless. Although I fix it when I see it, it results in unnecessary git commit records and wastes my time.

So, I need to find an automatic formatting tool similar to Prettier to help me automatically format all the code in the entire project before reading it.

By chance, I discovered CSharpier, and I can only say it's good! You can find clear information about it on their official website.

Next, let's see how to install it.

Installing CSharpier CLI#

Installing for a Single Project#

The advantage of doing this is that you can declare the dotnet tool dependencies for the current project, making it easier for others to restore the dependencies.

  1. Create a .config/dotnet-tools.json file:
dotnet new tool-manifest
  1. Install the CLI for the current project:
dotnet tool install csharpier

Update:

dotnet tool update csharpier

Global Installation#

Global installation:

dotnet tool install --global csharpier

Update:

dotnet tool update csharpier -g

Installing Visual Studio Extension#

Download the extension: https://marketplace.visualstudio.com/items?itemName=csharpier.CSharpier

Then, you can right-click in the code and click the Reformat with CSharpier button to format the current file:
image

You can also customize the shortcut or automatically format on save. Refer to the extension homepage: https://marketplace.visualstudio.com/items?itemName=csharpier.CSharpier

Usage#

Execute the following command in the solution root directory or the specified project directory:

dotnet csharpier .

image

Formatting Configuration#

If you need to modify the default formatting parameters, you need to add a configuration file to the project. Currently, there are 3 supported formats:

I'm using a YAML file, so I create a .csharpierrc file in the root directory and copy the following configuration into it:

printWidth: 140
useTabs: false
tabWidth: 4
endOfLine: auto

For more configurations, you can check https://csharpier.com/docs/Configuration

Ignoring File Configuration#

To ignore specific files and folders and not perform formatting on them, you can add a .csharpierignore file. The file content follows the gitignore syntax, for example:

Uploads/
**/App_Data/*.cs

Of course, CSharpier CLI has some default ignore configurations, refer to: https://csharpier.com/docs/Ignore#files-ignored-by-default

If you want to ignore a certain section of code in a formatting file, add the following comment (reference):

// csharpier-ignore

It can also be used to call the API by installing the Nuget package to write a program that formats code. Refer to: https://csharpier.com/docs/API

IDE Integration#

The official website also provides plugins for some IDEs, refer to: https://csharpier.com/docs/Editors

Others#

CSharpier has many features, including support for MsBuild Package, Pre-commit Hook, Continuous Integration, and more. Due to time constraints, I will stop here.

Known Issues#

  • Unable to remove extra line breaks
    image

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.