'Format code' and 'sort using' on save in Visual Studio 2022
Asked Answered
C

5

38

I and my team spent the last few months defining rules about C# code formatting and style for our project, so we ended up with an .editorconfig file.

Moreover, we all installed the extension Productivity Power Tools, in order to format the code when saving the file (CTRL+S) rather than having to use the VS built-in feature Code Clean Up. We took this decision when we realized that the latter requires a special shortcut (CTRL+K, CTRL+E) and that every member of the team needs to set this setting locally, since there is no way to put it in the source control.

This way of working went great with VS 2019.

But, in the last month most members of the team upgraded to VS 2022 and the extension Productivity Power Tools 22 removed - apparently without any reason - the Format On Save and Remove and Sort Usings on save features.

We apparently solved this issue with the extension VSE-FormatDocumentOnSave - by setting Edit.FormatDocument Edit.RemoveAndSort in the Commands option. It seems to work great but our concern is the fact that this is not a Microsoft tool and who knows what will happen in the future.

So, since the code style and formatting rules are a concern for every team out there, I would like to know if anyone knows a better way to manage this, considering that we are a team of 20+ developers.

Coltson answered 23/12, 2021 at 10:40 Comment(4)
FWIW, our team uses “Artistic Style” VS extension: github.com/lukamicoder/astyle-extension although for 2022 you may need to upgrade it yourself.Miracle
There is a better way - don't enforce IDE's and extensions on the developers to control code styles and formatting (as they may prefer to use VS Code or Vim). Instead use code reviews, gated check ins (reviewed Pull Requests to merge in to main branch for example), static code analysers (Stylecop, SonarCube, Roslynator can all be added to your code from NuGet packages) and incorporate them in your build pipeline. ReSharper has a free command line tool that you may be able to incorporate too.Communism
@Miracle Thanks for your suggestion, but as I stated in my question I would like to use a Microsoft product because all the others may be deprecated in the future.Coltson
@PiersMyers We all use VS, but editorconfig are supported by VS Code too. We already tried to use PR, but it is not that easy. Static code analysers are our next step, but we definitely need an easy way to format the code - we are cleaning 80+ projects from any kind of warning/suggestion, and sometimes pressing CTRL+S is enough to solve 20+ warnings in the same fileColtson
P
50

Starting from VS2022-Preview2, there is no need to additional extension to "format on-save", you just run a customized code cleanup that contains "Format document" action on-save:

  1. Customize your cleanup profile to perform "Format document" action (if not already there), in Analyze > Code Cleanup > Configure Code Cleanup
  2. Enable "Run code cleanup profile on save" in VS configuration Tools > Options > Text Editor > Code Cleanup

Bringing Code Cleanup on Save To Visual Studio 2022 17.1 Preview 2

Unfortunately code cleanup setting is not included to .editorconfig file neither to exported config file .vssettings that could be shared across a team, maybe it will be included in the future.

Some feature requests related to this:

Peridot answered 4/2, 2022 at 10:23 Comment(2)
I think they're going to integrate code clean up and editorconfig in one of the next previews VS 17.2 Preview 2. It could be what I'd like to have! However, thanks for your feedbackColtson
Thank you. This worked for me. I had to set it up to use an edited Profile 2. Maybe in future, this feature will be enhanced. Thanks for the heads up.Sensate
H
7

"Format Document On Save" worked for me.

Configuration:

Configuration

Happening answered 13/1, 2022 at 18:23 Comment(0)
C
6

Why not try the Code Cleanup On Save extension by Mads Kristensen (who works for Microsoft). I have not used it myself but all his other VS extensions are awesome.

Edit: The 'clean on save' feature has been added to Visual Studio 17.1 which is in preview at the moment. So no extensions required.

Communism answered 5/1, 2022 at 0:28 Comment(2)
Thanks for your suggestion. However, we have already tried this extension. You have to configure properly the Visual Studio Code Cleanup feature, a not-sharable but local settingColtson
In the released VS 2022 version the linked “Run Code Cleanup profile on Save feature is not available so you have to use the extension.Endoskeleton
S
1

The Format on Save is still there in VS 2022. Took me a while to find it and it was unchecked. Go to Tools - Options and search for Save.

enter image description here

Supreme answered 31/10, 2023 at 15:9 Comment(2)
As already explained in other comments, this is not the option I was looking for. Clean up profiles ignore .editorconfig files and it is not a versioned settingColtson
Could you explain how your answer is different than mine ?Peridot
S
-1

to format code for visual studio code 2023, use

SHIFT + OPTION + F
Selfmastery answered 2/3, 2023 at 9:1 Comment(1)
question is differentColtson

© 2022 - 2024 — McMap. All rights reserved.