Is there a pretty printer / code formatter for C# (as part of build system)?
Asked Answered
C

9

47

Is there a pretty printer / code formatter for C# (as part of build system)? Read as: "lives outside of Visual Studio". It seems like there are plenty of these kinds of things for Java, C++/C, Go -- so it seems more than reasonable that C# should also have some code formatter that lives outside of the IDE?

(I'd actually like to couple the formatter with something like StyleCop, and have devs run it as part of the process before a commit).

NOTE: Not syntax highlighting, as in code in a web page. Instead, a Code beautifier, or code pretty printer, which would take code and format it to a style/standard coding format... which StyleCop does a style check to see if the code meets the format.

Crackling answered 26/10, 2012 at 15:30 Comment(3)
@Alvin That’s unhelpful. Try googling for them yourself and see what relevant/garbage ratio you find. Note, OP isn’t searching for a syntax highlighter for websites, they’re looking for a tool to re-format code properly to conform to style guidelines.Cupidity
@Adriano Nope, that’s unrelated.Cupidity
@AlvinWong I got into an infinite loop because Googling c# prettier code formatter leads me to this exact same page.Speaks
S
28

There finally is; Csharpier!

It was originally ported from the popular Prettier and it is just a code formatter for C#. It can run from the command line and is not tied to an IDE nor an extension, although support for these exists as well. You can make it run as a part of a git-hook to for example automatically format changed files on git commit.

Some benefits of choosing Csharpier over other formatting tools are:

Strang answered 28/2, 2022 at 10:0 Comment(1)
This is more my speed!Matson
C
11

JetBrains also makes a free command line tool named CleanupCode that formats c# based on .editorconfig settings. I wrote a wrapper utility for it named ReGitLint that makes it easier and faster to run as a pre-commit hook or on the build server. This can help out a lot if you've got teammates using Visual Studio Code where ReSharper isn't an option.

To get up and going just run the following commands

dotnet tool install JetBrains.ReSharper.GlobalTools
dotnet tool install ReGitLint

then add the following to .git/hooks/pre-commit

#!/bin/sh
dotnet regitlint -f staged --fail-on-diff

To enforce formatting on jenkins add this to your build script

dotnet tool restore
dotnet regitlint --jenkins

For more options check out the readme

Cavafy answered 12/6, 2020 at 17:9 Comment(2)
this should be accepted answer, nice! i'm glad this is a free tool which should help adoption throughout our teamGiesser
If you're not already down with ReSharper, this link explaining .editorconfig setup might be useful. Great tool, though I wish it were Free & open source. (The first hit is always free, as they say.)Dumuzi
S
7

.NET 6+ — dotnet format:

10 years after this question was asked, the .NET SDK now comes with a built-in formatting command: dotnet format — see the documentation and GitHub repo for more information.

dotnet format used to be a third-party tool which eventually got merged into the official .NET SDK, starting from .NET 6 — see anouncement.

Usage:

You can run the command in any directory that has a .csproj or .sln file present, all C# (or VB.NET) files across the solution/project will be formatted:

dotnet format

Or you can specifically run it for individual files:

dotnet format --include Program.cs

The formatting will be based on preferences outlined in an .editorconfig file, if you have one present, otherwise it'll use its own defaults.

Sedillo answered 8/11, 2022 at 1:36 Comment(0)
A
5

For .NET SDK projects the dotnet format command can be used.

dotnet format is a code formatter 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.

It may also work for classic .NET Framework projects if the project does not import things that are not available on .NET SDK, like text templating msbuild targets.

The dotnet format source repository is located on GitHub dotnet/format.

The .editorconfig rules are documented, and can be exported from and will be used by Visual Studio too.

Autoicous answered 30/8, 2021 at 9:31 Comment(0)
V
4

The first that comes to mind is ReSharper. However, it's an add-in to VS which isn't what you were asking for. I'm not sure if you can set it up to auto-format on save.

From their website:

ReSharper can reformat an arbitrary selected block of code, all code in the current file, all files in a directory, or even in the entire project or solution, according to your Code Style preferences. ReSharper provides distinct formatting options for C#, VB.NET, JavaScript, CSS and XML code. Reformatting affects braces layout, blank lines, line wrapping and line breaks, spaces in various contexts, indentation, aligning multiple constructs, and a lot more options that you can fine-tune in the ReSharper Options dialog box.

Vani answered 26/10, 2012 at 17:3 Comment(1)
It can't - I asked a similar question on SO (oops).Joelie
M
1

Update: NArrange is not developed any more (see also https://github.com/MarcStan/narrange for a slightly newer fork).


To automate code formatting (kind of micro refactoring) during build you need a tool you can run from command line (and you can easily integrate in msbuild).

A good one if your needing isn't too advanced (authors define it as beautifier) is NArrange. It fully supports C# and VB.NET for Framework 2.0 but many widely used features of newer language versions are supported too.

NArrange is a .NET code beautifier that automatically organizes code members and elements within .NET classes.

Merras answered 26/10, 2012 at 17:8 Comment(3)
This looks to be deadBaldpate
@andrewjboyd true, there is a fork (github.com/MarcStan/narrange) slightly newer but the project has been abandoned a long time ago.Merras
(note that this answer is 9 years old)Merras
T
1

If you have an existing and unusual style guideline about whitespaces and brace placements AStyle is probably flexible enough to match it. AStyle is not C# specific, it can do many c-like languages.

NArrange is also good if it happens to match your style guidelines. It can be a more aggressive, for example it can put parts of your classes into different regions and sort your methods.

Telethon answered 29/4, 2015 at 17:9 Comment(0)
L
1

Microsoft has a code formatter tool that use Roslyn to automatically rewrite source code according to their guidelines. Please check that link https://github.com/dotnet/codeformatter

Laudable answered 28/4, 2021 at 21:4 Comment(2)
Are you using this successfully on a current project There's an issue on that project that basically says it's dead. A comment in an issue on that project suggests using dotnet-format, which seems to be active.Dumuzi
I'm not using that currently, I just thought of sharing that there is something doing that requirment, going to have a look at dotnet-format.Laudable
H
-1

You can now use https://www.npmjs.com/package/narrange leveraging the power of node.js Super simple, configurable setup :)

const { createNArrange } = require("narrange");
const path = require("path");

createNArrange({
  srcPath: path.join(__dirname, "src/apps/mango"),
  configFilePath: path.join(__dirname, "config/NArrange.xml")
});
Hancock answered 12/3, 2019 at 12:18 Comment(1)
This appears to be a wrapper around NArrange v0.2.9, created by a different author than the original software.Doubt

© 2022 - 2024 — McMap. All rights reserved.