[英]Can't figure out how to use Prettier Plugin in .prettierrc
I'm using Prettier for a Java project.我在 Java 项目中使用 Prettier。 Prettier doesn't format Java by default, so instead I installed this plugin here: https://github.com/jhipster/prettier-java
Prettier 默认不格式化 Java,所以我在这里安装了这个插件: 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.按照自述文件中的说明工作正常,但是,我不希望每次修改任何内容时都键入终端命令来重新格式化所有 java 文件。 Instead I'd like to format a file anytime I save it.
相反,我想在保存文件时对其进行格式化。 This is currently in my
settings.json
:这目前在我的
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".但是,使用
Shift+Alt+F
(VScode 中用于格式化的默认命令)会导致错误“安装的“java”文件没有格式化程序”。 How do I configure my .prettierrc
file to properly use the plugin to format java files?如何配置我的
.prettierrc
文件以正确使用插件来格式化 java 文件?
This is my .prettierrc file right now:这是我现在的 .prettierrc 文件:
{
"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在
.prettierrc
和settings.json
文件中使用相同的设置,除了
"[java]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
and added default formatter for java files as follows并为 java 文件添加了默认格式化程序,如下所示
Format Document With...
Format Document With...
Configure Default Formatter...
Configure Default Formatter...
Language Support for Java(TM) by Red Hat
Language Support for Java(TM) by Red Hat
Working sample工作样本
Now if I add following in settings.json
现在,如果我在
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
文件中删除上述设置。
settings.json file settings.json 文件
{
"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
}
PS Make sure Java Extension Pack vscjava.vscode-java-pack
is installed, used v0.12.1 PS确保安装了
Java Extension Pack vscjava.vscode-java-pack
,使用v0.12.1
Ref:参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.