Search code examples
eclipseformattingeclipse-cdt

Eclipse C++ formatter puts new line before method identifiers


I ran into a problem with the Eclipse formatter. It won't format my code correctly when declaring methods within a class declaration. It puts a new line after the method's return type.

I already exported the style xml file and examined the settings in it, but none of the settings have any apparent connection to this problem, and the settings editor in Eclipse didn't show the same problem happening in it's sample code for method declarations.

Here is an example bit of code for what I want to have happen:

class MyClass
{
    public:
        MyClass();
        void myMethod();
};

However, this is what I get:

class MyClass
{
    public:
        MyClass();
        void
        myMethod();
};

Again, in the styles editor, the code doesn't have this problem and looks just how I want it to, but in the actual code, the story is different.

I'm using version 3.8.0. Any help is appreciated.

Edit: I deleted those source files that were formatted incorrectly (after formatting the code several times to no avail) and replaced them with "identical" files with the same methods, same structure, etc. I formatted the code this time and it worked. This is probably a bug, but I'm leaving it up just in case anyone else encounters a similar problem or has a solution to avoiding this problem in the first place.


Solution

  • I hand edited two files under the main eclipse projects directory

    .metadata\.plugins\org.eclipse.core.runtime\.settings
    

    The two files:

    file 1: org.eclipse.cdt.core.prefs, change this line from "insert" to "do not insert"
     org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration=do not insert
    
    
    file 2: org.eclipse.cdt.ui.prefs,
    scan this file for "insert_new_line_before_identifier_in_function_declaration" and make a similar change from insert to do not insert next to it, should be obvious
    

    Note I seen this problem on indigo and juno, the fix described above was in juno.