Reputation: 1173
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
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.
Upvotes: 4
Reputation: 296
to format code for visual studio code 2023, use
SHIFT + OPTION + F
Upvotes: -3
Reputation: 81
"Format Document On Save" worked for me.
Configuration:
Upvotes: 8
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:
Analyze > Code Cleanup > Configure Code Cleanup
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:
visual studio 2019 allow import and export Configure Code Cleanup profile
Provide a way to share Code Cleanup profiles as related to EditorConfig
Upvotes: 58
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