vscode格式不格式化

HMR*_*HMR 4 javascript visual-studio-code vscode-settings prettier

为 vscode安装了漂亮的插件,并有一个 .pretteirrc.js:

module.exports = {
  trailingComma: 'es5',
  tabWidth: 2,
  semi: true,
  singleQuote: true,
  printWidth: 60,
}
Run Code Online (Sandbox Code Playgroud)

在设置中,默认格式化程序设置为:esbenp.prettier-vscode 并检查保存时的格式,但保存时没有格式化任何内容,也没有给出任何错误的指示。

右键单击具有以下内容的 js 文件:

var test = [1, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)

选择格式文档并不会对其进行格式化,使用...格式化文档也不会格式化文档 => Prettier code formatter 也不会选择 typescript 和 javascript 语言功能。

奇怪的是,这种格式具有默认的打字稿和 javascript 语言功能,即使设置没有将其作为默认格式化程序。

我可以在扩展中看到更漂亮的插件并且它已启用。

vscode 是 1.41.0 版本

重新启动了几次并重新加载了 vscode 窗口。将尝试删除并重新安装 vscode,因为保存时自动格式化是我离不开的功能。

欢迎任何检查内容的建议,代码没有语法错误(请参阅上面的示例代码),因此不应阻止 vscode 格式化,也不会给出任何错误的迹象。

从项目目录中删除了 .vscode 目录,现在默认格式化程序更漂亮,但仍然没有格式化。

HMR*_*HMR 5

卸载并重新安装 vscode 并且格式化再次起作用。

我的 .vscode/settings.json 看起来像

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "prettier.configPath": "./personal.yml"
}
Run Code Online (Sandbox Code Playgroud)

因此,对于我使用个人格式的项目,但在签入文件之前,我创建了一个任务 .vscode/tasks.json,它将对所有修改过的.js 和 .json 文件进行标准格式。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Format",
      "command": "git status -s | grep '\\.js$\\|\\.json$' | cut -f3 -d' ' | xargs prettier --write --config ./.standard.yml;",
      "type": "shell"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

mac 上的正则表达式的工作方式不同,所以我不得不运行两次更漂亮:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "format",
      "type": "shell",
      "command": "git status -s | grep '\\.js$' | cut -f3 -d' ' | xargs prettier --write --config ./.prettierrc.yml && git status -s | grep '\\.json$' | cut -f3 -d' ' | xargs prettier --write --config ./.prettierrc.yml"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)