Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an automatic code formatter for C#? [closed]

In my work I deal mostly with C# code nowadays, with a sprinkle of java from time to time. What I absolutely love about Eclipse (and I know people using it daily love it even more) is a sophisticated code formatter, able to mould code into any coding standard one might imagine. Is there such a tool for C#? Visual Studio code formatting (Crtl+K, Crtl+D) is subpar and StyleCop only checks the source without fixing it.

My dream tool would run from console (for easy inclusion in automated builds or pre-commit hooks and for execution on Linux + Mono), have text-file based configuration easy to store in a project repository and a graphical rule editor with preview - just like the Eclipse Code Formatter does.

like image 256
skolima Avatar asked Aug 27 '08 13:08

skolima


People also ask

How do I format C code?

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension. You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu.

Does Prettier work with C?

Prettier wasn't intended to format C++ code. You should install the C/C++ for Visual Studio Code extension developed by Microsoft, it supports code formatting.

Which is the best code formatter?

1. Prettier – Code formatter. It is an opinionated code formatter that enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. Besides, it supports many languages.

How do I automatically format code in Visual Studio Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.


2 Answers

For Visual Studio, take a look at ReSharper. It's an awesome tool and a definite must-have. Versions after 4.0 have the code formatting and clean-up feature that you are looking for. There's also plugin integration with StyleCop, including formatting settings file.

You'll probably want Agent Smith plugin as well, for spell-checking the identifiers and comments. ReSharper supports per-solution formatting setting files, which can be checked into version control system and shared by the whole team. The keyboard shortcut for code cleanup is Ctrl + E, C.

In 'vanilla' Visual Studio, the current file can be automatically formatted with Ctrl + K, Ctrl + D, and Ctrl + K, Ctrl + F formats the selected text.

As for a runs-everywhere command line tool to be used with commit hooks, try NArrange. It's free, can process whole directories at once and runs on Mono as well as on Microsoft .NET.

Some people also use the Artistic Style command line tool, although it requires Perl and works better with C/C++ code than with C#.

like image 58
FantaMango77 Avatar answered Sep 20 '22 04:09

FantaMango77


The .NET Foundation just released their code formatting tool on GitHub

https://github.com/dotnet/codeformatter

It uses the Roslyn compiler services to parse project documents and convert them to their expected formatting conventions. They're applying this to some of the very old CLR code in order to make all the projects consistent.

like image 33
Paul Turner Avatar answered Sep 23 '22 04:09

Paul Turner