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
Extensionswindow in VSCode.- You can open the Extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X
Post Installation, from the required page with JAVA code
Right Clickand chooseFormat Document With..- In the command pallette that opens up on top, select
Language Support for Java(TM) by Red Hatoption
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.
IfLanguage Support for Java(TM) by Red Hatis 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:
- Pressing Ctrl+, to open your
Settings - From Top Right Corner, select
Open Settings(JSON)either for User or Workspace settings. - Append the below JSON property to it and save:
"[java]": {
"editor.defaultFormatter": "redhat.java",
},
Answer from RICHARD ABRAHAM on Stack OverflowFormatting 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
Extensionswindow in VSCode.- You can open the Extensions window from the left aligned sidebar or simply by pressing Ctrl + Shift + X
Post Installation, from the required page with JAVA code
Right Clickand chooseFormat Document With..- In the command pallette that opens up on top, select
Language Support for Java(TM) by Red Hatoption
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.
IfLanguage Support for Java(TM) by Red Hatis 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:
- Pressing Ctrl+, to open your
Settings - From Top Right Corner, select
Open Settings(JSON)either for User or Workspace settings. - Append the below JSON property to it and save:
"[java]": {
"editor.defaultFormatter": "redhat.java",
},
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.
IntelliJ vs VSCode Java formatter differences are driving me nuts
Java Formatter not working properly
There is no formatter for 'java' files installed
visual studio code - Preserving line breaks in VSCode Java formatter - Stack Overflow
Hey folks,
some of my teammates are using IntelliJ and I really want to stick with VSCode. It’s lighter, I’ve got a nice Vim setup there, it works for pretty much everything, and most importantly it’s smooth with WSL (unlike IntelliJ, which is a dealbreaker for me).
The problem is Java code formatting. Ideally, we’d all use the same formatter so we don’t end up with messy diffs. I’m using the Red Hat Language Support for Java extension in VSCode. I tried exporting the IntelliJ formatting settings (Eclipse XML) and importing them into VSCode, but there are still differences. For example, how annotations in record parameter wrapping get formatted, and a few other spots.
Has anyone dealt with this before? Is there a reliable way to get IntelliJ and VSCode (with Red Hat Java support) to format identically, or am I chasing something impossible? Any advice would be appreciated.
how can i reset my java formatter? I tried uninstalling the prettier extension and reinstalling it. but nothing seems to work. Every time I use the formatter it messes up my code and makes it very hard to read. It wasn't like this before and actually worked. I would appreciate any help. ty!
In your format settings (e.g. eclipse-java-google-style.xml) use this:
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
- You can use some special comment to let the extension skip format in a range:
//@formatter:off
someClass.someMethod(
veryLongParameterOne,
veryLongParameteTwo,
veryLongParameterThree
);
//@formatter:on
- Or you can trigger a command
Java: Open Java Formatter Settings with Previewto view some basic format settings in UI. Meanwhile it will generate a .xml format profile in .vscode folder, in which you can find all format settings there.