Vscode-java: Format Switch Statement

Created on 4 Feb 2020  ·  9Comments  ·  Source: redhat-developer/vscode-java

Environment
  • Operating System: WSL 1 on Win 10
  • JDK version: 11.0.5 2019-10-15
  • Visual Studio Code version: 1.41.1
  • Java extension version: 0.55.1
Steps To Reproduce
  1. Add a switch statement format the document

sample project

Current Result

image

Expected Result

image

Additional Informations

Is this configurable? This is the extension that is formatting the code, right?

enhancement formatter

Most helpful comment

There's a rationale for not indenting cases: https://stackoverflow.com/questions/4509039/why-the-strange-indentation-on-switch-statements

But I still find it hurts my eyes. Also Oracle's doc on switch shows indentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html.

The google formatter does the same:
https://google.github.io/styleguide/javaguide.html#s4.8.4-switch

IntelliJ IDEA and Netbeans indent cases by default too.

All 9 comments

@spottedmahn You can try to set the following property:

org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true

to /.settings/org.eclipse.jdt.core.prefs or your formatter file.
See https://github.com/redhat-developer/vscode-java/wiki/Formatter-settings

thanks for the reply!

No luck though 🤷‍♂️. I also closed and reopened VSCode just incase.

image

@spottedmahn could you use

<your_project>/.settings/org.eclipse.jdt.core.prefs 

instead of

<your_project>/.settings/org.eclipse.buildship.core.prefs 

Oh, I should pay closer attention to the details 😜

GTG 🎉, thanks! 🤝

image

image

@snjeza can you please make it the default setting in jdt.ls. Current formatting of switch cases makes no sense at all.

Current formatting of switch cases makes no sense at all

Glad I don't stand alone! 😀

There's a rationale for not indenting cases: https://stackoverflow.com/questions/4509039/why-the-strange-indentation-on-switch-statements

But I still find it hurts my eyes. Also Oracle's doc on switch shows indentation: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html.

The google formatter does the same:
https://google.github.io/styleguide/javaguide.html#s4.8.4-switch

IntelliJ IDEA and Netbeans indent cases by default too.

Current formatting of switch cases makes no sense at all

After reviewing the PR it makes more sense. It's not that someone said "default it this way". It's that it was missed.

Thanks for fixing the default!

someone said "default it this way" -> that someone is Eclipse JDT. Our fix overrides JDT's default

Was this page helpful?
0 / 5 - 0 ratings