Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse C++ formatter puts new line before method identifiers

Tags:

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.

like image 800
Adam Avatar asked Dec 15 '12 16:12

Adam


People also ask

How do I fix lines in eclipse?

Open the required file. Go to Source | Format Document or press Ctrl+Shift+F.


2 Answers

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.

like image 107
user1967159 Avatar answered Nov 14 '22 02:11

user1967159


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.

like image 45
Halil Kaskavalci Avatar answered Nov 14 '22 03:11

Halil Kaskavalci