Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure out how to use Prettier Plugin in .prettierrc

I'm using Prettier for a Java project. Prettier doesn't format Java by default, so instead I installed this plugin here: https://github.com/jhipster/prettier-java

Following the directions in the README works fine, however, I'd prefer not to type a terminal command to reformat all my java files every time I modify anything. Instead I'd like to format a file anytime I save it. This is currently in my settings.json:

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

However, using Shift+Alt+F, the default command for formatting in VScode, results in the error "There is no formatter for "java" files installed". How do I configure my .prettierrc file to properly use the plugin to format java files?

This is my .prettierrc file right now:

{
  "plugins": [
    "prettier-plugin-java"
  ],
  "pluginSearchDirs": [
    "./node_modules"
  ],
  "overrides": [{
      "files": "**/*.java",
      "options": {
            plugins: [
                "prettier-plugin-java"
            ]
      }
  }]
}
like image 398
user4500882 Avatar asked Feb 18 '20 20:02

user4500882


1 Answers

Used same settings in .prettierrc and settings.json file except

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

and added default formatter for java files as follows

  1. Open command Palette... Ctrl + Shift + P (mac command + shift + P) and
  2. search Format Document With...
  3. select Configure Default Formatter...
  4. choose Language Support for Java(TM) by Red Hat

Working sample

enter image description here

Now if I add following in settings.json

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

got the same issue as of yours

enter image description here

So the solution will be to remove above settings from the settings.json file.

settings.json file

{
    "files.eol": "\n",
    "terminal.explorerKind": "external",
    "terminal.integrated.shell.osx": "/bin/zsh",
    "editor.minimap.enabled": true,
    "workbench.colorTheme": "Visual Studio Dark",
    "editor.fontSize": 14,
    "editor.fontFamily": "source code pro, Menlo, Monaco, 'Courier New', monospace",
    "terminal.external.osxExec": "iterm.app",
    "window.zoomLevel": 1,
    "go.useLanguageServer": true,
    "editor.formatOnSave": true,
    "editor.suggestSelection": "first",
    "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
    "breadcrumbs.enabled": false 
}

P.S. Make sure Java Extension Pack vscjava.vscode-java-pack is installed, used v0.12.1

Ref:

  1. https://github.com/redhat-developer/vscode-java/issues/220
  2. Beautify / Format Java code in Visual Studio Code
  3. https://code.visualstudio.com/docs/java/java-linting
  4. https://github.com/jhipster/prettier-java
  5. https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
like image 132
dkb Avatar answered Sep 30 '22 19:09

dkb