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"
]
}
}]
}
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
Format Document With...
Configure Default Formatter...
Language Support for Java(TM) by Red Hat
Working sample
Now if I add following in settings.json
"[java]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
got the same issue as of yours
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:
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