Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advanced (C++) Source Code Re-Formatting with Visual Studio?

We would like to be able to reformat C++ blocks/functions of code directly from the Visual Studio IDE, so that developers easily can assure that the new code they insert adheres to our formatting guidelines.

I have found the Artistic Style tool, which pretty much covers the features we'd need. However, it only can work on whole files from the CLI, so it's not very helpful for what we'd want to use it. (Whole file reformatting is certainly never what we want [a].)

Visual Studio (2005) has limited auto-format features, but AFAICS, these are mostly about correct indenting, which is a bit lacking.

So my question is if there are any tools that can do advanced reformatting on a selection from the VS IDE, or maybe if there's a VS IDE plugin making use of AStyle?

Edit: The question linked to recommends a tool - ProFactor's StyleManager - are there any other tools like this?

Note [a] : Reformatting whole files or whole project trees is only useful for personal projects or for initial code checkins, IMHO. For a (large) team project, blanket reformatting will mess up the change history of files or lines within files, making it a lot harder to track who's changed what.

like image 833
Martin Ba Avatar asked Mar 09 '11 13:03

Martin Ba


1 Answers

I found a blog post that describes how to integrate an external code formatter with Visual Studio: Per-Project Code Formatting in Visual Studio

(...) You can then configure your IDE(s) so that their default formatting commands instead use Uncrustify. In Visual Studio, for example, one can set up an external tool. Assuming uncrustify.cfg is next to the solution file, then the command line

-c $(SolutionDir)uncrustify.cfg --no-backup "$(ItemPath)"

will reformat the current source file. You can then assign a keyboard mapping (or reassign the one for Edit.FormatDocument, like me) for quick access.

I haven't tried it, but it seems to boil down to configure your formatter (e.g. AStlye / Uncrustify) as an External Tool in Visual Studio to be able to call it on a source file from the IDE.

Still only per-file, but seems one of the better ideas I found.

like image 170
Martin Ba Avatar answered Oct 21 '22 09:10

Martin Ba