Eclipse Statement - Formatting

the moment I

    switch (numberString) {
       case "FOUR": number = 4; break;
       case "FIVE": number = 5; break;
    }

      

It's a simple switch statement, but if I use the eclipse format key, ctrl shift F, it suddenly turns into something 10 lines longer, breaking the case, value = and break; all on different lines. Is there a way to change the formatter to ignore switch statements? Thanks to

+5


source to share


3 answers


I struggled with this too. The only option seems to be to use comment tags for formatting on / off.

Select the "Off / On" tab in the formatting preferences and check the box next to Enable tags off / on.



Then in your code add //@formatter:off

before and //@formatter:on

after the switch statement .

Pretty sure that's the only workaround for now.

+1


source


Eclipse automatic format breaks newlines for each half colon. It will also split long single lines to fit in the side scrolling viewport - adding a new line indented. Your example example seems too short to call this, but if it is just an example then I guess. I haven't found a way to localize the effect of autoformats. (for mac Cmnd + Shift + F)



0


source


Consider (4 years later) Eclipse 4.12 (2019-06) combined with Java 12 (Mar 2019).

It now has:

Since Java 12 introduced some new functionality to the radio button design, there are some new settings in the formatting profile.
The settings allow you to control the spaces around the arrow operator (separately for case and by default) and around commas in case with multiple values.

The settings can be found in the profile editor (" Preferences > Java > Code Style > Formatter > Edit...

) in the" White space > Control statements >

" switch

" subsection .

https://www.eclipse.org/eclipse/news/4.12/images/formatter-switch.png

Since Java 12 introduced the ability to group multiple switch register labels into a single register expression, a quick help has been provided that allows you to split these grouped labels into separate register instructions.

https://www.eclipse.org/eclipse/news/4.12/images/split-switch-case-labels.png

As you can see, these switching expressions remain fairly compact.

0


source







All Articles