prettier-java icon indicating copy to clipboard operation
prettier-java copied to clipboard

How to enable in vscode

Open stephan-klein opened this issue 1 year ago • 3 comments

I read all the guides still im puzzled how to enable the plugin in vscode.

I execute

npm install --save-dev --save-exact prettier prettier-plugin-java

I have the prettier extension installed.

and now ? how can i enable the formatting ? Still my other java formatter is executed by default. Format Document with... action does not detect it.

when i put

"[java]": {
        "editor.defaultFormatter": "esbenp.prettier-vscode"
    },

in settings.json

i get

image

How to link the plugin to formatting action ?

stephan-klein avatar Dec 18 '24 10:12 stephan-klein

Hello @stephan-klein !

Do you have a Prettier configuration file ? If not, can you try creating one like this one (you can convert this one from yaml to any other format) and see if if fixes your issue ?

# Prettier configuration
plugins:
  - prettier-plugin-java
overrides:
  - files:
      - "*.java"
    options:
      printWidth: 100
      tabWidth: 4
      useTabs: false
      trailingComma: "none"

clementdessoude avatar Dec 21 '24 18:12 clementdessoude

Also note that there is a regression in the latest version 2.6.6 that prevents Vscode to load the prettier-plugin-java plugin: https://github.com/jhipster/prettier-java/issues/711.

You either need to downgrade to 2.6.5 or update node_modules/prettier-plugin-java/package.json with the code mentioned in the linked issue.

furti avatar Jan 16 '25 06:01 furti

I'm seeing the same error as OP. I usually run this via Spotless Maven plugin when committing but would be great to be able to format in the editor too.

I have a .prettier-java.json file similar to the yaml above in my project/backend dir and have the same VSCode settings config.

The prettier formatter works fine on other file formats, just not Java. Any help would be much appreciated, thanks!

bottlerocketjonny avatar Sep 16 '25 10:09 bottlerocketjonny