Reputation: 20891
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.
Upvotes: 30
Views: 94325
Reputation: 998
I feel it's a good idea to store a configuration with each project (and not globally).
F1
Preferences: Open Workspace Settings (JSON)
{
"java.format.settings.url": "./eclipse-java-google-style.xml"
}
eclipse-java-google-style.xml
which has the settings.org.eclipse.jdt.core.formatter.join_wrapped_lines
to false.Upvotes: 4
Reputation: 2528
Formatting a JAVA Document in VSCode requires one to follow the below steps:
Install the plugin: Language Support for Java(TM) by Red Hat from the Extensions
window in VSCode.
Post Installation, from the required page with JAVA code
Right Click
and choose Format Document With..
Language Support for Java(TM) by Red Hat
optionThe 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.
IfLanguage 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:
Settings
Open Settings(JSON)
either for User or Workspace settings. "[java]": {
"editor.defaultFormatter": "redhat.java",
},
Upvotes: 40
Reputation: 65195
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.
Upvotes: 8