Document Formatter on save leave files in modified state

Mohammad Mohsin Siddiqui 175 Reputation points
2023-09-04T04:33:55.81+00:00

As starting from VS 17.1 (I believe) VS gave Code Cleanup option to format documents on save. I have custom clang-formatter setup in VS. And it works fine on click ctlr K+D. I want to apply this formatter on save also because some time, we change class names or function names and that gets changed in many files, and its not convenient to open each file and do ctrl K+D. I found below link on Code Cleanup setup which will call clang-formatter after following instructions.

https://devblogs.microsoft.com/visualstudio/bringing-code-cleanup-on-save-to-visual-studio-2022-17-1-preview-2/

SO now when I save file, its calls my clang-formatter and formats files if needed. The way I understand it, this is how it works in order.

  1. Saves a files
  2. Run formatter (because of Code Cleanup setup)

Now when when step2 happens as part of formatting is formats file which makes that file modified again. NOw user will get confused thinking that I did save but file is still showing modified which is not intitive.

So is there any way to call save again post-calling format document? OR any other way to solve this?

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
0 comments No comments
{count} votes

Answer accepted by question author
  1. Tianyu Sun-MSFT 34,691 Reputation points Microsoft External Staff
    2023-09-04T07:38:52.04+00:00

    Hello @Mohammad Mohsin Siddiqui ,

    Welcome to Microsoft Q&A forum.

    Sometimes, Code Cleanup feature helps to auto save the file(s) when you run it, which means if you don’t save the modified file(s), but directly run the Code Cleanup(and the formatter detects that some code need to be formatted) from Analyze > Code Cleanup > Run Code Cleanup (Profile X) on Solution, then the modified file(s) will also be formatted and auto saved. Of course, if no code is detected to be formatted then the file will not be formatted and auto saved.

    There’s a quick/easy way to find out whether the opened, modified file(s) have been saved. You can see that there’s a * after your file name if you modified the file or when you are editing this file. It will disappear after you/VS save it.

    User's image

    So is there any way to call save again post-calling format document? Or any other way to solve this?

    I’m afraid, there’s no other way currently.

    BTW, there’s an option for auto saving files(Tools > Options > Environment > Documents > Automatically save files when Visual Studio is in the background).

    Sincerely,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.