question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Indent case blocks within switch statements

See original GitHub issue

vscode-java does not honor Eclipse formatting preferences for indenting statements within ‘switch’ blocks

Environment
  • Operating System: Windows 10
  • JDK version: 1.8.0_171-b11
  • Visual Studio Code version: 1.26.1 493869ee8e8a846b0855873886fc79d480d342de x64
  • Java extension version: 0.29.0
Steps To Reproduce
  1. Create a new Java project in Eclipse
  2. Create a Main.java with a simple switch statement:
public static void main(String [] args) {
  int x = 0;
  switch (x) {
    case 0:
      return;
  }
}
  1. Change project-specific formatting settings: indent case statements as seen above (case is indented further than switch)
    • Indentation > Indented elements > Statements within ‘switch’ body [checked]
  2. Format the project in Eclipse, noting how the indentation is preserved
  3. Open the project in Visual Studio code, and note how the code is reformatted
Current Result

Code is reformatted so case statements align with switch statements:

switch (x) {
case 0:
  return;
}
Expected Result

Eclipse settings are honored, and VS Code formats the same as Eclipse:

switch (x) {
  case 0:
    return;
}
Additional Informations

The checkbox changes the following line in org.eclipse.jdt.core.prefs: org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true (true when checked, false when unchecked)

Here is a project that follows these steps

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
fbriconcommented, Feb 5, 2020
1reaction
tommai78101commented, Dec 11, 2019

@mark-wiemer Ah, you replied by email, so you didn’t see my answer to what eclipse-formatter.xml is doing. Just repeating what I said earlier. 👍

The eclipse-formatter.xml file is used for Visual Studio Code’s extension, Redhat’s vscode-java extension, to interface with and format your Java source files inside Visual Studio Code, based on Oracle’s Java standard implementation for the formatter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Why the strange indentation on switch statements?
In 4 words: no blocks, no indentation. Cases are not opening a block. ... case statements are not indented relative to the switch...
Read more >
Automatically Indent Switch Statement Cases in Xcode
Automatically Indent Switch Statement Cases in Xcode · Xcode menu · Preferences · Text Editing tab · Indentation sub-tab.
Read more >
D43183 clang-format: introduce `CaseBlockIndent` to ... - LLVM
CaseBlockIndent = Block : add an extra level of indent for the ... case blocks in switch statements can have curly braces or...
Read more >
Reason for non-indented case statements in switch (go fmt)
The general trend is: indent code within curly braces one level. But this isn't the case with the switch statement. Is there a...
Read more >
The switch Statement (The Java™ Tutorials > Learning the ...
A statement in the switch block can be labeled with one or more case or default labels. The switch statement evaluates its expression,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found