3

I currently have Eclipse formatter set up to format an 'if-else' statement like so:

if(condition) {
  return foo;
}
else{
  return bar;
}

Note the space between the closing parenthesis of the condition, and the opening bracket of the true block - I'd like to remove this space.

In the formatter profile, under Whitespace -> Control statements -> 'if else', there is no option for 'after closing parenthesis'. In the Blocks section, there is an option for 'before opening brace', however this appears to only apply to the false block (and is turned off anyway).

I must be missing an option somewhere - how can I turn this whitespace off?

I am using Eclipse Mars 4.5.0.

Numeron
  • 8,723
  • 3
  • 21
  • 46
  • 1
    In Luna, unchecking the `before opening brace` in blocks removes the whitespace properly. Could very well be a bug in Mars. Not sure though. – Codebender Jul 09 '15 at 11:33
  • 1
    I know that source code layout is a delicate subject. But why do you want to format your code like this? –  Jul 09 '15 at 11:35
  • Ahh, yeah I booted up Luna, and it appears the 'before opening brace' option in blocks works there - so this must be a bug in Mars. – Numeron Jul 09 '15 at 11:37
  • @EricGärtner I want to format my code like that because that's how I prefer it? – Numeron Jul 09 '15 at 11:39
  • @Numeron So you work as a single developer, not in a team? –  Jul 09 '15 at 11:40
  • Can you check my updated answer. – Kalyan Chavali Jul 09 '15 at 11:41
  • @EricGärtner I use this profile for my personal work, but everyone has different preferences for code style, and there is no right or wrong. I like compact code, the guy who sits behind me at work likes spread out code. 6 of one, half a dozen of the other. – Numeron Jul 09 '15 at 11:45
  • But how to you share code in a team if every developer formats the code in a different way? How do you prevent endless merging of trivial changes in format? –  Jul 09 '15 at 11:47
  • IMO the only single golden rule of formatting is that the whole project should be consistent - so everyone needs to use the same format.. Perhaps I was a little unclear though - This is for my personal work, the formatter I use professionally is different. – Numeron Jul 09 '15 at 11:50

3 Answers3

1

You can do that in the Windows -> Preferences -> Java -> Code Style ->formatter -> Edit profile section

enter image description here


I think it does once you select the other option in drop down that you can see below

enter image description here

Kalyan Chavali
  • 1,330
  • 8
  • 24
0

Go to Windows > Preferences > Java > Code Style > Formatter > White Space and in Control statements > Blocks, unselect the "before opening brace" option :)

enter image description here

Mistalis
  • 17,793
  • 13
  • 73
  • 97
0

This is due to Eclipse Bug # 471145 (specific to Mars / 4.5.0), which was reported on 2015-06-26, fixed on 2015-08-01, and will be included in Eclipse 4.5.1 - which should be complete and ready for download by the end of September 2015 as part of the Mars coordinated service release.

ziesemer
  • 27,712
  • 8
  • 86
  • 94