简体   繁体   English

不知道如何在 .prettierrc 中使用 Prettier Plugin

[英]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.prettierrcsettings.json文件中使用相同的设置,除了

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

and added default formatter for java files as follows并为 java 文件添加了默认格式化程序,如下所示

  1. Open command Palette... Ctrl + Shift + P (mac command + shift + P) and打开命令面板... Ctrl + Shift + P (mac command + shift + P) 和
  2. search Format Document With...搜索Format Document With...
  3. select Configure Default Formatter...选择Configure Default Formatter...
  4. choose 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:参考:

  1. https://github.com/redhat-developer/vscode-java/issues/220 https://github.com/redhat-developer/vscode-java/issues/220
  2. Beautify / Format Java code in Visual Studio Code在 Visual Studio Code 中美化/格式化 Java 代码
  3. https://code.visualstudio.com/docs/java/java-linting https://code.visualstudio.com/docs/java/java-linting
  4. https://github.com/jhipster/prettier-java https://github.com/jhipster/prettier-java
  5. https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM