Force "same line" on curly braces in Eclipse formatter
Asked Answered
S

2

6

In my java code formatter, in my profile I have (actually it's the eclipse's default) all choices in the "braces" tab set to "same line". Nevertheless when I have a piece of code like this:

interface TestI
    {
}

and I invoke the formatter (key binding, context menu, whatever) all I get is

interface TestI
{
}

it doesn't move the opening braces to the same line, the same applies to methods, control statements etc. then when I manually change it to

interface TestI      {
}

it then formats it to:

interface TestI {
}

So it generally respects the format I did manually but only formats the spaces between the braces and other elements.

Other formatting options in my formatter work as as specified. I'm using the latest Eclipse Indigo release 3.7.1, I had the same with 3.7, and my friend tried it with a 3.5 release, so it makes me think that this is on purpose, but then again why is there this "braces" tab at all if I can't reformat the code using this rule ...

I hope I'm missing something and that you will make me fell stupid in a moment and the problem will go away, because I have a gazillion of classes to cleanup and I don't want to do it by hand .. :)

UPDATE: when I set the rule not to "same line" but "next line" then formatting works as expected, it forces the rule regardless of how I've written the code, as soon as I change it back to "same line" the the formatter doesn't force this rule.

Symploce answered 19/10, 2011 at 10:58 Comment(0)
S
14

Found it... it was caused by:

<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/> 

which in the Formatter setting GUI is under "Line Wrapping/Never join already wrapped lines"

yes now I remember setting it, but I never thought that this would override the braces setting ...

a nice-to-have feature of the formatter would be to have warnings if you set options that are no-ops because of other options that you've set override them.

Symploce answered 21/10, 2011 at 10:30 Comment(1)
Unfortunately, it then also takes my meticulously-aligned pretty wrapped function calls and squashes them together on 2.5 lines of arbitrarily-line-broken mess. I wish it would do just braces.Terrilynterrine
P
0

For those wondering how to make this apply to array initializers, the settings are

  • New Lines > After opening brace of array initializer
  • New Lines > Before closing brace of array initializer
Passus answered 17/7, 2018 at 3:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.