Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautify / Format Java code in Visual Studio Code

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

like image 458
snr Avatar asked Mar 21 '17 23:03

snr


People also ask

How do I beautify my code in VS Code?

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.

What is Prettier code formatter in VS Code?

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).

How do I change the format code in Visual Studio?

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.


2 Answers

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",
      },
like image 181
RICHARD ABRAHAM Avatar answered Nov 04 '22 23:11

RICHARD ABRAHAM


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.

like image 34
Matt Bierner Avatar answered Nov 05 '22 00:11

Matt Bierner