Krusty
Krusty

Reputation: 1173

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

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.

Upvotes: 43

Views: 57558

Answers (5)

live-love
live-love

Reputation: 52366

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

Upvotes: 4

shine odigie
shine odigie

Reputation: 296

to format code for visual studio code 2023, use

SHIFT + OPTION + F

Upvotes: -3

"Format Document On Save" worked for me.

Configuration:

Configuration

Upvotes: 8

Cfun
Cfun

Reputation: 9671

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:

Upvotes: 58

Piers Myers
Piers Myers

Reputation: 10899

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.

Upvotes: 7

Related Questions