Beautify / Format Java code in Visual Studio Code
Asked Answered
C

3

29

I have tried some proposals on here and here, but none of them works. How do you accomplish it for Java code or is there a way for general? En passant, I've install XML formatter in order that it may work, but not.

enter image description here enter image description here

Calvary answered 21/3, 2017 at 23:27 Comment(0)
P
39

Formatting a JAVA Document in VSCode requires one to follow the below steps:

  1. Install the plugin: Language Support for Java(TM) by Red Hat from the Extensions window in VSCode.

    • You can open the Extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X
  2. Post Installation, from the required page with JAVA code

    • Right Click and choose Format Document With..
    • In the command pallette that opens up on top, select Language Support for Java(TM) by Red Hat option

The Java Document should be now properly formatted.

PS: Trying Alt+Shift+F may not work for users who have set their default formatter as Prettier - Code formatter or alike. By default, java formatting is not supported by it.
If Language Support for Java(TM) by Red Hat is the only formatter plugin installed, then Alt+Shift+F might work for you.

If you wish to bind Alt+Shift+F to format your JAVA Document,
then you can add the below configuration to your settings.json by:

  1. Pressing Ctrl+, to open your Settings
  2. From Top Right Corner, select Open Settings(JSON) either for User or Workspace settings.
  3. Append the below JSON property to it and save:
    "[java]": {
        "editor.defaultFormatter": "redhat.java",
      },
Polariscope answered 5/6, 2020 at 8:56 Comment(3)
any option for eclipse theia or open vscode?Euphemize
The RedHat java plugin is an absolute joke on VS Code. It's incredibly slow, it shows errors that don't exist, it's completely unusable with large projects, it's always running out of memory and/or crashing, it never works right with submodules, it often doesn't pick up generated sources, it often mysteriously puts generated sources in the wrong folder, I can go on and on. It was an incredible waste of time trying to get it to work. To anyone reading this: Do yourselves a favor and instead use the NetBeans java plugin. Even though it's technically an alpha, It's far better in every respect.Assentation
@AlvinThompson I installed the NetBeans plugin. This is what I found: 1) It won't run or debug individual junit tests without adding the surefire maven plugin where RedHat didn't require any extras. 2) For me, it was vastly slower than RedHat. 3) When editing a single java file it has no context, e.g., auto-add imports, where RedHat works (I edit single files to use with jshell). 4) Most importantly, like RedHat, it wouldn't load a custom settings formatter file. I could see it giving errors, but didn't bother investigating further given all the other issues.Philbrick
D
8

For proper formatting, you need to install a VSCode extension that provides Java formatting. Try this one from Redhat: https://marketplace.visualstudio.com/items?itemName=redhat.java

After the extension is installed, that standard code format commands / shotcuts should work for Java code.

Deposal answered 22/3, 2017 at 1:11 Comment(5)
You can also set a formatter configuration file as it is shown here github.com/redhat-developer/vscode-java/wiki/Formatter-settingsFagaly
Before installing this extension, I had to install OpenJDK: jdk.java.net/archiveTautologism
The problem with this one is that it's insane bloatware if all you want is source formatting.Ramose
@Mike'Pomax'Kamermans which source formatter is without bloatware? Maybe the solution further down with eclipse-java-google-style?Ortolan
This doesn't help a very new person when you don't say what the "standard format commands / shortcuts" areBarmen
M
4

I feel it's a good idea to store a configuration with each project (and not globally).

  1. open workspace settings json file. Following steps work on my laptop -
    1. press F1
    2. select Preferences: Open Workspace Settings (JSON)
  2. paste the below there -
{
  "java.format.settings.url": "./eclipse-java-google-style.xml"
}
  1. Now you can create a file in the root folder eclipse-java-google-style.xml which has the settings.
  2. There, you can paste the contents of eclipse-java-google-style.xml. Optionally set org.eclipse.jdt.core.formatter.join_wrapped_lines to false.
Metaplasia answered 28/10, 2021 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.