Search code examples
c++eclipseconstructorformattingeclipse-cdt

Format(line wrapping) constructor initializer list in Eclipse CDT


I tried to find a solution for now ~30min and couldn't find any. I am trying to set up the code style in CDT so it gives me:

MyClass::MyClass() :    
var1(1), 
var2(2), 
var3(3){

}

instead of

MyClass::MyClass() :    
var1(1), var2(2), var3(3){

}

but I couldn't find an option to do so.

The only 'initializer list' option I could find is actually for arrays and therefore not useful for me.

My question is: Am I missing the right spot? Is there a plug-in out there which does better formatting of C++ code than CDT?


Solution

  • @Eric provides manual solution, but to make this setting auomatic, you need to edit eclipse preferences.

    Click on:
    Window -> Preferences

    Go to:
    C/C++ -> Code Style -> Formatter

    Here, as first thing you have to create a new profile.

    Select tab:
    Line Wrapping

    Go to:
    Function declarations -> Constructor initializer list

    On the bottom, you have to set:

    • Line wrapping policy: Wrap all elements, every element on a new line.
    • check "Force split, even if line is shorter than maximum"
    • Indentation policy: indent on column