Community
Participate
Working Groups
In the C/C++ code style formatter settings window, under the 'Indentation' tab and in 'Indent' box - if I check both: - 'public', 'protected', 'private' within class - Declarations relative to 'public', 'protected', 'private options, then the automatic formatting of a simple C stuct is corrupted. It is indented twice, instead of a single indentation. E.g.: assuming a 4 space indentation the indentation of the inner part of the struct will be 8 spaces. struct MyStruct { char a; char b; }; Instead of: struct MyStruct { char a; char b; }; If I uncheck any of the above options, then the indentation of the C structures are OK, but in that case the c++ classes indentation does not match the requirements. -- Configuration Details -- Product: Eclipse 4.5.0.20150621-1200 (org.eclipse.epp.package.cpp.product) Installed Features: org.eclipse.platform 4.5.0.v20150603-2358
(In reply to Andras Vasko from comment #0) > In the C/C++ code style formatter settings window, under the 'Indentation' > tab and in 'Indent' box - if I check both: > - 'public', 'protected', 'private' within class > - Declarations relative to 'public', 'protected', 'private > options, then the automatic formatting of a simple C stuct is corrupted. It > is indented twice, instead of a single indentation. > > E.g.: assuming a 4 space indentation the indentation of the inner part of > the struct will be 8 spaces. > struct MyStruct { > char a; > char b; > }; > Instead of: > struct MyStruct { > char a; > char b; > }; That is the expected behaviour. > If I uncheck any of the above options, then the indentation of the C > structures are OK, but in that case the c++ classes indentation does not > match the requirements. What are the requirements?
I prefer not to have visibility used within a struct, since if I need to use visibility then using a class is the suggested way. I always see a struct as an "old-fashioned" real struct, and it should not have visibility definitions, so the double indentation is useless. I think the class was derived from a struct, and not the way other, so the formatting of a class should not affect the formatting of a struct. I now checked how Eclipse behaves for different file types. In a .cpp file the formatter uses double indentation for struct, and in a .c file there is only a single indentation, so Eclipse handles a struct in a .cpp file as a class, but I think if someone writes a struct, it should be a struct, and nothing else. This problem is also problematic with .h files, since the default for .h files is the C++ style formatting, and in a mixed C/C++ environment is can make a mess. I'm programming embedded systems and I have to deal with lots of 3rd party chip drivers that are mostly coded and formatted in C, but the business logic that is added by me is in C++. This is the reason why I have to work in a mixed environment, and also forces me to think of structs as only-C structs.
What I would really like to see, is to have an option to force the formatter to always indent the struct in a simple way, regardless the class indentation options.
So, in your coding style, members of classes are indented by two levels, but member of structs only by one level?
Correct! e.g.: class MyClass { public: char a; }; struct MyStruct { char a; };
C++ allows structs to have access specifiers, too, so there is no way CDT could know without additional configuration that it should indent structs in C++ code differently from classes. What we could do is introduce separate "'public', 'protected', 'private' within struct" and "Declarations relative to 'public', 'protected', 'private' within struct" settings for structs. For your coding style, you could then set "'public', 'protected', 'private' within struct" to 0 and get the indentation you describe.
That would be perfect!
(In reply to Nathan Ridge from comment #6) > C++ allows structs to have access specifiers, too, so there is no way CDT > could know without additional configuration that it should indent structs in > C++ code differently from classes. One idea would be to test whether the struct definition has any access specifiers and avoid the extra indent if there is none.