Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up C# editorconfig Code Cleanup on build/save and commit

Our team want to enforce styling rules in our C# project. I read somewhere some time that Microsoft said that ".editorconfig is the future" so we want to use this. NOTE: We don't want to use ReSharper.

C# has a lot of great rules that can be defined in editorconfig now, see Microsofts own editorconfig guide

We want to use this, and enforce that the rules set in the editor config is followed both when coding in Visual Studio and enforce that the code commited to git is following the rules.

When adding the .editorconfig rules, we get great linting on our files like this:

use var warning

Running a fully enabled "Code Cleanup" in Visual Studio 2019 it completely formats our code as desired:

run code cleanup

Question 1: How can we make the "Run Code Cleanup" run automatically on save/build? Even if we set certain rules as severity ":error" the compiler still don't complain about issues in C# files on build.

NOTE: I have tried the plugin for Visual Studio called Format document on Save but it does not follow all the rules set in the editorconfig (only a few, like fixing tabs/spaces and end of file newline)

We would also like to make sure that all commits to our git repository gets formated.

There is a tool called dotnet-format that is supposed to format the code according to the editorconfig rules.

We would like to add a pre-commit hook that runs the following 2 commands:

dotnet tool install -g dotnet-format
dotnet-format

This would work fine, but the issue is that dotnet-format also don't fix the issues in files with code giving severity ":error".

dotnet-format behaves the same way "Format document on Save" does, only fixing a few things like tabs/spaces and end of file newline.

EDIT: dotnet-format appearently only supports a few of the rules for now as per their Wiki

Question 2: How can we, from a command line, run a command behaving the same way as the "Code Cleanup" command in Visual Studio 2019 does?

like image 436
Totty Avatar asked Mar 29 '19 09:03

Totty


People also ask

How do I set up C and C++?

Install Visual Studio Code. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for 'c++' in the Extensions view (Ctrl+Shift+X). Get the latest version of Mingw-w64 via MSYS2, which provides up-to-date native builds of GCC, Mingw-w64, and other helpful C++ tools and libraries.

What is the C environment?

C/C++ integrated development environments, or C/C++ IDEs, are software platforms that provide programmers and developers a comprehensive set of tools for software development in a single product, specifically in the C and/or C++ programming languages.

How do I set up my environment for C?

C - Environment Setup. If you want to set up your environment for C programming language, you need the following two software tools available on your computer, (a) Text Editor and (b) The C Compiler. Text Editor. This will be used to type your program.

How do I get Started with C++ development?

For core C++ support, choose the "Desktop development with C++" workload. It comes with the default core editor, which includes basic code editing support for over 20 languages, the ability to open and edit code from any folder without requiring a project, and integrated source code control.

How do I install the C++ C++ extension?

Search for 'C++'. Select Install. After you install the extension, when you open or create a *.cpp file, you will have syntax highlighting (colorization), smart completions and hovers (IntelliSense), and error checking. C++ is a compiled language meaning your program's source code must be translated (compiled) before it can be run on your computer.

How do I get Started with C++ in VS Code?

Install the Microsoft C/C++ extension. Open VS Code. Click the Extensions view icon on the Sidebar (⇧⌘X (Windows, Linux Ctrl+Shift+X)). Search for c++. Click Install. Hello World tutorials. Get started with C++ and VS Code with Hello World tutorials for your environment: GCC on Windows; Microsoft C++ on Windows; GCC on Linux; GCC on Windows ...


1 Answers

I am able just to answer the your first question:

I have been looking for this and I found a extension called Code Cleanup on Save, you will just have to install it and configure it at Tool->Options->Code Clean up on Save, decide what of your profiles you want to set and that's all, I hope it will help you!

like image 70
Jesus Sanz Losa Avatar answered Oct 27 '22 01:10

Jesus Sanz Losa