Format C/C++ code on save
Asked Answered
P

7

28

The formatting options of Eclipse avoids me lots of conflicts with other users, and ensure that my code always looks "clean" (especially regarding my git commits).

I know about the "Format on save" option in Java editor, but I could not find one in C/C++.

Is this deliberate?

May I expect to see such option one day? Or maybe I haven't looked enough?

Pushcart answered 21/3, 2011 at 13:14 Comment(11)
There are options to set you coding style so that it is correctly formatted while editing. Is that not what you want?Doubletime
@Goulou: If you really want people to answer your questions, you should accept some answers to your previous questions. StackOverflow is not a regular forum. People should somehow be rewarded for the time they spent on solving your problems.Slightly
@Bo Persson : does it format everything while editing? I'm not sure (especially when you paste code directly)Pushcart
@Slightly : sorry, I was waiting for even more accurate answers for some of my questions, but they got forgotten... I just accepted some of them now.Pushcart
@Goulou: I'm not sure about the formatting when not typing the code in. I hardly ever paste large amounts of badly formatted code into mine. :-)Doubletime
@Goulou, sometimes, the answers don't get any better because there is too little information in the question. Sometimes, there are not knowledgeable people to answer.Osprey
@Bo Persson Formatted "diffently" with regard to my own habits does not necessarily mean badly formatted :-) @Amigable Clark Kant : I'm afraid I sometime asked for things that proved themselves not doable...Pushcart
@Borealid Would you like to elaborate, why you're featuring this question? May be a reasonable step is to contact the CDT forum or account to the bugzilla system, as I have mentioned in my answer. This would be a nifty feature, I agree.Theirs
@g-makulik I was hoping there was a solution that had been overlooked. It seems like a trivially small feature - why would this not already exist somewhere in the massive CDT project?Anuska
@Anuska I'm not so sure if it's really trivial to implement this feature for the C++ text editor, since it needs to access the currently active formatting strategy. But I don't know nothing about the CDT/Eclipse plugin interfaces or java. Would be really worth it to ask at the CDT forum (and relink any useful answers here).Theirs
After six years now the format on save has been implemented. Your day has arrived :)Eyehole
E
3

Format on save in CDT is now implemented in Eclipse Mars.

Eyehole answered 13/12, 2014 at 20:2 Comment(2)
How does on activate it? What menu options?Princedom
Thanks (+1) that will help others (perhaps you can edit your answer, to make it more visible). What a pity that we can't do the same for source\optimize includes, like they can for Java (if that ever changes in future, could someone please answer #39364452)Princedom
C
15

I know about the "Format on save" option in Java editor, but I could not find one in C/C++ ... Or maybe I haven't looked enough?

I'm afraid that option is simply not available for the CDT C++ editor (Eclipse CDT Juno Service Release 1):

Eclipse Juno C++ CDT Text Editor Save Action Options

That's everything that's available at Eclipses IDE Level.

There are two solutions you might consider to solve your concerns:

  1. You might consider adopting some discipline as mentioned in the other answers
  2. Running an appropriate script to format your source files before committing them. The AStyle tool might be helpful for this purpose.

One could also think about an automatic pre-checkin/-commit interception running a script for the actually used Revision Control System. Many revision/version control systems offer such feature.

May I expect to see such option one day?

Did you ask for this feature on the Eclipse/CDT community forum, or placed a feature request in the Eclipse CDT bugzilla tracking system? If no one knows about such feature demand and whether it is demanded by a majority of users, chances that this will be implemented by 'accident' are low.

Caseworm answered 23/1, 2013 at 20:51 Comment(0)
E
4

You can use the keys "Ctrl+Shift+F" to format your edited c/c++ file.

Eyeful answered 25/4, 2011 at 13:53 Comment(0)
E
3

Format on save in CDT is now implemented in Eclipse Mars.

Eyehole answered 13/12, 2014 at 20:2 Comment(2)
How does on activate it? What menu options?Princedom
Thanks (+1) that will help others (perhaps you can edit your answer, to make it more visible). What a pity that we can't do the same for source\optimize includes, like they can for Java (if that ever changes in future, could someone please answer #39364452)Princedom
B
2

I'm using AStyle program to format my code following a set of rules. At the moment I'm doing this from the CLI only but I know there's a plugin for eclipse, which I haven't used yet:

Astyle Eclipse is based on Astyle program, it aims to provide a C/C++ formatter for CDT plugin in eclipse. so all the supported format optional shall check astyle

Bubbler answered 23/1, 2013 at 21:31 Comment(1)
The question is, does the plugin provide 'Format on save' functionality (can't spot this from the options shown here)Theirs
C
1

There is no such default option in the eclipse cdt yet. You can try and write your own plugin This eclipse forum thread shows how to invoke the CDT formatter from an eclipse plugin. You only need to figure out how to trigger a plugin call on every save action.

Carinacarinate answered 29/1, 2013 at 19:15 Comment(3)
As I mentioned you'll need to consider the currently selected format strategy, I'd guess this isn't really 'trivial' from within this plugin. I'd prefer consulting the developers forum 1st, why this seemingly 'trivial' option wasn't implemented yet.Theirs
basically the same feature is already implemented in the JDT. I also think that there might be a reason, but the java world already does everything. I would assume one possible reason is that CDT hasn't backported that from JDT and at the time of writing there wasn't a CDT formatter or there wasn't a JDT save action. It seems to me you have to write a save action listener and register that..Carinacarinate
the constant for the button setting is called FORMAT_SOURCE_CODE you will find the listener stuff close to it.Carinacarinate
H
0

Here is a plugin to fit the need: fos4cdt. Eclipse Mars may contain this feature, but many users will continue to use older versions for a while. So, it can be useful.

Huddersfield answered 18/3, 2015 at 20:22 Comment(0)
L
-3

You can trying to select all code,and use Alt + F8 to format. This is way of VC IDE,but I don't sure that Eclipse can work.

Leastways answered 21/6, 2011 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.