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.
The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.
Prettier is an opinionated code formatter which ensures one unified code format. It can be used in VS Code by installing it from the VS Code Marketplace. Once you have integrated it in VS Code, you can configure Prettier to format your files when saving them or committing them to a version control system (e.g. Git).
VS Code has great support for source code formatting. The editor has two explicit format actions: Format Document (Ctrl+Shift+I) - Format the entire active file. Format Selection (Ctrl+K Ctrl+F) - Format the selected text.
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",
},
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With