2

I looked in the eclipse Java formatter (Java / Code Style / Formatter) but could not find a way to not have a new line after case. Instead of

switch (a) {
    case 0:
        Other.doFoo();
        break;
    case 1:
        Other.doBaz();
}

I want

switch (a) {
    case 0: Other.doFoo();
            break;
    case 1: Other.doBaz();
}

Is there a way to do it and if so what is it?

user1803551
  • 12,965
  • 5
  • 47
  • 74

1 Answers1

3

I found no way to resolve this. There is no way of refractoring existing code as you expected.

But you can avoid writing such switch statement now onwards by enabling off/on tags as explained here Eclipse Formatter Allow Multi Line ; and then changing the switch template in Windows > Preferences > Java > Editors > Templates as mentioned below

    //@formatter:off
        switch (${key}) {
            case ${value}:${cursor}
                          break;

            default:break;
        }
    //@formatter:on
Community
  • 1
  • 1
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • So this template will be the initial form of the statement, and if I just `ctrl+shift+F` then it will revert to my formatter's structure? By the way, this is **formatting existing code**, not **refactoring existing code**. – user1803551 Apr 25 '14 at 20:19
  • On pressing `Ctrl+Shift+F` will **NOT** format the lines between `@formatter` tags. Test it. – Chandrayya G K Apr 26 '14 at 06:01
  • Tested, the switch is still formatted when I put the tags exactly as you said. – user1803551 Apr 26 '14 at 14:32
  • 2
    OK, the `Off/On Tags` need to be enabled in the formatter customization. – user1803551 Apr 26 '14 at 17:30