Visual Studio 2012 "Smart" Indentation Customization
I am using Visual Studio 2012 and have Smart indentation turned on1 for C++ files.2 I would like to customize Smart indentation's behavior so that it formats the code I enter so that it complies with my company's coding style.
How can I customize all the minute aspects of how Smart indentation behaves?
For example, when I enter this code, Smart indentation formats it exactly like this:
#include <cstdlib>
#include <string>
using namespace std;
struct Foo
{
const string mA;
const int mB;
const string mC;
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
};
int main()
{
}
Most of this is what I want, except for the colon introducing the initializer list, the first item in the initializer list, and the indentation level of the constructor's body. I want these formatted like this, and I want Visual Studio to do it for me automatically:
Foo(const string& a,
const int b,
const string& c)
:
mA(a),
mB(b),
mC(c)
{
}
How can I customize Smart indentation's behavior? I'd prefer to not use any external tools like Visual Assist X.
1: Via Tools > Options > Text Editor > C/C++ > Tabs > Indenting
2: I also have tabstops set to 4, with spaces inserted.
Look into the MS Visual Studio SDK, found here:
http://msdn.microsoft.com/en-us/library/bb139565.aspx
In particular you want to override HandleSmartIndent in the VewFilter class:
http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.package.viewfilter.handlesmartindent.aspx
This gets called whenever you press the Enter key in the editor. Unfortunately, it's not as easy as just changing some rules in a config dialog.
An ugly solution is this:
Foo(const string& a,
const int b,
const string& c)
: mA(a)
, mB(b)
, mC(c)
{
}
Which, for some abominable reason, is the only way I've ever seen to get VS to indent that mess properly.
链接地址: http://www.djcxy.com/p/69614.html