Visual Studio 2022 now performs automatic code cleanup on file records – Visual Studio Magazine

News

Visual Studio 2022 now performs automatic code cleanup when saving files

One cool little feature in the all new Visual Studio 2022 17.1 Preview 2 is automatic code cleanup on save.

Code Cleanup was introduced in 2018 with Visual Studio 2019 Preview 1. It allows developers to configure any code styles they want to apply by choosing one of two profiles in the Configure Code Cleanup dialog box.

Microsoft’s August 2021 documentation says, “For C # code files, Visual Studio 2019 has a Code cleanup button at the bottom of the editor (keyboard: Ctrl + K, Ctrl + E) to apply code styles from an EditorConfig file or the code style options page. If a .editorconfig exists for the project, the settings take precedence. ”

“Code Cleanup On Save” has actually been available for some time as well, thanks to a Visual Studio Marketplace extension aptly called Code Cleanup On Save, created by Microsoft extension guru Mads Kristensen. “Code Cleanup is a new feature in Visual Studio 2019 that will automatically clean up your code file to make sure it is properly formatted and your coding style preferences are applied,” the Marketplace description states. “This extension will automatically clean up the code when saving the file.” It has over 30,000 installs and got a perfect score of 5 by 12 developers who reviewed it.

Now, the feature is being integrated into the company’s flagship IDE for the first time, though you’ll need to dive into the preview channel to use it right now.

Cleanup of code when recording in an animated action
[Click on image for larger, animated GIF view.] Cleanup of code when recording in an animated action (source: Microsoft).

“Starting with Visual Studio 2022 17.1 Preview 2, developers can now automatically perform code cleanup when a file is saved!” Microsoft product manager Denizhan Yigitbas said in a January 10 blog post. “Automatic code cleanup on save is a new feature built into Visual Studio 2022 that can clean up your code file to make sure it’s formatted correctly and your encoding style preferences are applied. Some preferences Customizable features include: format document, sort uses, remove unnecessary uses, and more. This feature can help minimize style violations in PRs and more to allow developers to spend less time fixing unresponsive code to specific standards and more time doing what they do best. ”

Its instructions for using it are:

  • Go to Analyze> Code Cleansing> Configure Code Cleansing to customize the desired customizations for your code cleansing profile (s).
  • Go to Tools> Options> Text Editor> Code Cleanup. Add a check mark in the “Run code cleanup profile on save” box. Make sure you select the correct profile that you want to run automatically every time you save!

In the comments of the post, it was revealed that Code Cleanup on Save does not yet work with C ++.

Another developer also sounded: “Indeed, expect c ++ support here …. In fact, just including ‘Document Format’ support will get you there most. time. (it looks like the msvc tooling API itself won’t happen, so I have to stick with the clang toolsets for now). ”

Another comment read: “This is a nice feature, but it is unclear and confusing how the code cleanup interacts with EditorConfig.”

The response was, “We’re currently working to improve this experience which you can follow here,” pointing to a GitHub issue appropriately titled Easy Code Cleanup with EditorConfig # 40163.

Or you can just look at the existing documentation titled Appropriately Code Styles in EditorConfig files.

About the Author

David Ramel is editor and writer for Converge360.

Comments are closed.