Adam
Adam

Reputation: 3778

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.

Upvotes: 24

Views: 8380

Answers (4)

Martin Kealey
Martin Kealey

Reputation: 683

Others have adequately explained how to fix the problem, but that leaves open the question of why line breaks are inserted by default. I hope the following helps.

Line breaks before function names is a very long-stand tradition in C, and some common tooling relies on it.

For example, vi and its derivatives have a keyboard shortcut ctrl+] for "jump to definition" that uses an external cross reference list, which is generated by the ctags program, and many versions of ctags rely on C function names being on the left margin.

In hindsight this should have been disabled right from the start when dealing with C++, because there are too many aspects that are different.

In particular, line breaks before method names inside a class are less useful because, since they're indented, they won't be recognized by ctags and other tools. Worse, line breaks on declarations that are not definitions are entirely useless, as they distract from finding the actual definitions.

Upvotes: 0

greywolf82
greywolf82

Reputation: 22183

There's a specific preference in the formatter options starting from cdt 9.8 included in Eclipse 2019-06.

Upvotes: 0

Halil
Halil

Reputation: 2173

If you have a custom formatter config, export it first (settings>C/C++ General>Formatter>Edit>Export). Then change the following line to "do not insert". Save the XML.

<setting id="org.eclipse.cdt.core.formatter.insert_new_line_before_identifier_in_function_declaration" value="do not insert"/>

Delete the current config and import the one you changed.

Upvotes: 7

user1967159
user1967159

Reputation: 206

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.

Upvotes: 19

Related Questions