39

Eclipse keeps auto formatting upon save, for example: it is insisting the following code should be in a single line once I save, despite breaking it up the way I want to.

@Transactional(rollbackFor = DataAccessException.class, readOnly = false, timeout = 30, propagation = Propagation.SUPPORTS, isolation = Isolation.DEFAULT)
abhi
  • 1,760
  • 1
  • 24
  • 40
Oliver
  • 3,592
  • 8
  • 34
  • 37

5 Answers5

68

Window > Preferences > Java > Editor > Save Actions.

Can also be configured at the project level. Project > Properties > Java Editor > Save Actions.

dty
  • 18,795
  • 6
  • 56
  • 82
  • Or, you can configure the code formatter to not reorganise your line-breaks for you, but still do the rest of its magic. – Matthew Gilliard Mar 04 '11 at 21:11
  • 3
    it appears I can disable formatting in "save action", but I don't see the option for organizing line-breaks in code formatter. – Oliver Mar 04 '11 at 21:29
  • 2
    Same problem persists for me, despite everything on that page being unchecked (Juno on mac). Something else? – SMBiggs Dec 21 '12 at 17:37
  • 3
    I have completely disabled the "perform the selected actions on save" but still eclipse format my code. Did I miss something? – Arashsoft Apr 12 '16 at 14:20
  • 3
    Have you disabled it both globally (Window > Preferences) AND for the project (Project > Properties)? If so, I have no idea, I'm afraid. I haven't used Eclipse for 3+ years. – dty Apr 13 '16 at 15:01
  • 1
    @Arashsoft This can be because you have disabled code formatting globally, but it exist for per project . Disable the save actions for each project if present. For this got to Window->Preferences> Java -> Editor->Save Actions-> click on "Configure project specific Settings ". On the bottom of the window select the check box "Show only project with project specific settings " . Then select a module/project -> Ok-> Un check "Enable project specific settings"--> Apply. You are done! – Dinoop paloli Jul 10 '17 at 06:56
4

This is a Late answer, But just for novice to understand it clearly I have a snapshot of the setting. Please follow the path on left.
And change highlighted setting. enter image description here

VedantK
  • 9,728
  • 7
  • 66
  • 71
  • 1
    I have completely disabled the "perform the selected actions on save" but still eclipse format my code. Did I miss something? – Arashsoft Apr 12 '16 at 14:20
  • me too and I can't figure a way, it seems I'd have to give up eclipse, too many features can become a problem. – Onza May 17 '17 at 06:21
  • @Onza which version of eclipse are you using ? – VedantK May 17 '17 at 07:08
  • @TmP I figured out the problem and it might be the reason that others are having that same problem, plugins, in my case it was AnyEdit tools which allow me to sort, you need to disable it there. – Onza May 17 '17 at 10:41
4

Absolutely maddening improvement. For if statements do the following: Window | Preferences | Java | Code Style | Formatter | Control statements tab Select Keep simple if on one line. You might also try the Line wrapping tab

Mike
  • 41
  • 1
  • This is where most of my problems lie! Damn complicated (like everything Eclipse), and almost thorough. – SMBiggs Dec 21 '12 at 17:41
3

If you wish to keep the auto-formatter, but don't want this behavior, you can also configure it in the fomatter :

right click on the project -> properties -> java code style -> formatter

(If you don't have a project specific formatter the global configuration can be accessed this way : Window -> Preference -> Java -> Code Style -> Formatter).

Edit... the active profile, open the Line Wrapping tab, and there check the Never join already wrapped lines option.

This way you can wrap lines wherever you want and eclipse won't remove your line breaks (which is a must if you wish to have readable lambda expressions), but keep the formatter active.

This option is there at least since eclipse Luna

Here is a screenshot where this option is : enter image description here

Thierry
  • 5,270
  • 33
  • 39
2

Just wanted to add a tip. If you want to avoid the line wrapping you can simply increase the "Maximum line width" value to something enormous. As long as that is sufficiently high eclipse won't do any wrapping.

Preferences > Java > Code Style > Formatter > Edit > Line Wrapping > Maximum line width

Strucker
  • 99
  • 4
  • In kepler this seems not to be applied on save-action. The save-action tab strangely overwrites all settings of the formatter and also hides this setting. In previous versions there was no problem. – djmj Mar 13 '14 at 01:21
  • Although many answers are helpful, only this actually answers to the question and works in the same time. – Poniros Mar 06 '21 at 16:39