Eclipse: Can you format code on save?
Asked Answered
F

4

138

In Eclipse, under Windows -> Preference -> Java -> Code Style, you can define code templates for comments and code, and you can setup a code formatter.

I'm wondering if it is possible in Eclipse to have these setting take affect every time I save a source file. Basically, instead of me highlighting everything and pressing Ctrl+Shift+F, I want Eclipse to be responsible for making sure my code is formatted properly.

Is this possible and how do you set it up?

Note On Using the Auto-Format: It's probably best to choose "Format Edited Lines" as merging changes becomes very difficult when you reformat a whole file that is in source control already that was not formatted properly. Already did this to a co-worker.

Farfetched answered 24/10, 2008 at 17:47 Comment(7)
Great question, but I disagree with enabling "Format Edited Lines". If the whole team has access to the same format XML descriptor via SCM then the team should expect their files to be reformatted. In other words, the short term pain of merging is worth the benefit of consistent formatting.Yeeyegg
@HDave agree. The code should at any time be robust against a "Format Source" on all projects.Furring
@ThorbjørnRavnAndersen If everyone is using these settings, you can select "Format Edited Lines". It will save some time if your files are large. I agree with you about necessity of the code being robust against a "Format Source", but it's not the task of eclipse to make sure that this is the case, I think.Shark
@Shark a political decision. Personally I believe that the closer you stay to default settings the smoother things will run.Furring
@ThorbjørnRavnAndersen That would mean the auto-format option being turned off... ;) I mean, I always save by Ctrl+Shift+F followed by Ctrl+S, so it would be fine by me, but there are other guys who don't care about formating. This feature makes our life so much easier...Shark
Gah. Eclipse formats trivial behaviour-less getter/setter properties out to multiple-line functions. Since studies have shown code comprehension & productivity are closely linked to vertical-space usage, I recommend keeping trivial properties compact; 3-4 lines including comment & both getter/setter. Auto-format is for low-performing teams, not highly competent ones.Jerald
this has to be the worst idea everConsideration
E
219
  1. In the Toolbar, navigate to: WindowPreferences
  2. In Preferences, navigate to: JavaEditorSave Actions
  3. Check the following:
    • Perform the selected actions on save
    • Format source code

This may or may not be available in previous versions of Eclipse. I know it works in:

Version: 3.3.3.r33x_r20080129-_19UEl7Ezk_gXF1kouft<br>
Build id: M20080221-1800
Ergosterol answered 24/10, 2008 at 17:53 Comment(5)
This looks very useful -- is there a way to achieve the same for PHP code?Reluctance
Fine, but how do you do the same for non-Java files?Teleost
@PéterVarga if you're using PyDev, check thisAcupuncture
This looks very useful -- is there a way to achieve the same for C++ code?Ossification
"Preferences" is in the "Window" menu, that first step is missing from this answer.Oval
D
16

I have a snapshot of the setting. Please follow the path:

In the Preferences window, choose Java --> Editor --> Save Actions.

Check the Perform the selected actions on save, and check the Format source code box.

enter image description here

Descendant answered 26/6, 2015 at 6:14 Comment(1)
"Preferences" is in the "Window" menu, that first step is missing from this answer.Oval
P
14

I strongly recommend checking your eclipse format xml descriptor into source control. That way all members of the team can use it and you don't get to and fro reformatting battles.

Preestablish answered 26/3, 2009 at 10:49 Comment(2)
Can this be done so that the XML file is installed by default?Yeeyegg
Interesting idea, but it's incomplete and also doesn't answer the question.Oval
L
1

If you find that you do not have a Save Actions preference under Java--> Editor, it may be because you are using an older version of Eclipse. In that case you can install the Format on save plugin from here.

Then, under Preferences, choose Java --> Format on save. Select the Run Format option under Select a code formatting action

Laurilaurianne answered 10/8, 2009 at 17:14 Comment(1)
"Preferences" is in the "Window" menu, that first step is missing from this answer.Oval

© 2022 - 2024 — McMap. All rights reserved.