Question

I often find myself in situations where I would like to indent preprocessor directives like the rest of the code (e.g. #if indented like if). It seems legal, it's common sense that it's sometimes a good thing, but Visual won't make it easy.

Is there a way to prevent the Visual C++ 2010 editor from sticking the code to the left each time I innocently press #? And to not break preprocessor directives indentation with auto-indent (CTRL+K, CTRL+F)? Or even better, to handle preprocessor directives like everything else?

Était-ce utile?

La solution

My approach is to keep the # in the first column and indent the subsequent word, as in:

#ifdef FIRST
#  include "first.h"
#else
#  include "second.h"
#endif

Autres conseils

At some point visualstudio (checked in vs2015) acquired options > text editor > c/c++ > formatting > indentation > position of preprocessor directives. Choose 'leave indented'. The sample doesn't look exactly like what you want, but it works, just try it.

In the Visual Studio 2010 options (Tools->Options)

Go to Text Editor -> C/C++ -> Tabs

Under indenting select Block instead of smart.

This will prevent the # key from forcing you to the start of the line, however if you use Ctrl+K Ctrl+F it will still apply smart formatting and remove the tabs.

Edit: This will also disable automatic indenting/unindenting in other places. Beware.

Workaround: When you first type the # and Visual Studio removes your indentation, press ctrl+z to undo the auto formatting.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top