在 VS Code 中,我不想让 Black 格式化我的 settings.json

In VS Code I don't want Black to format my settings.json

我想为我的 Python 文件使用黑色格式化程序,但不为我的 JSON 设置。

我在 settings.json 中设置了这些:

    "python.formatting.provider": "black",
    "editor.formatOnSave": true,

我尝试通过将以下内容添加到 settings.json 来使用 --exclude 标签:

    "python.formatting.blackArgs": [
        "--exclude /*\.json/"
    ],

相当于使用 black --exclude /*\.json/

的命令行调用

我也试过了

    "python.formatting.blackArgs": [
        "--exclude /*\.json/"
    ],

基于此 post:

但是,它仍在格式化我的 settings.json。

黑色不格式化JSON。发生的事情是 VS Code 有它自己包含的 JSON 格式化程序,这就是格式化你的 settings.json。您是否开启了 "editor.formatOnSave" 之类的设置?如果是这样,那么听起来您只想将其范围限定为 Python 个文件,例如:

"[python]": {
  "editor.formatOnSave": true
}

Black 确实格式化 JSON,对我来说,打破了它:

╰─➤  black proj/
reformatted proj/schema.json
All done! ✨  ✨
1 file reformatted.

╰─➤  git diff proj/schema.json | wc -l
299

您还可以通过以下方式禁用 JSON 的格式:

  • 使用 [ctrl+,] 进入首选项并禁用 JSON > Format: Enable。
  • 打开 settings.json 文件(使用 [ctrl+shift+p] 打开调色板并搜索“设置 json”)并添加以下行:
    "json.format.enable": false
    

或者您也可以将格式限制为 python 个文件,方法是在 settings.json 文件中添加此设置:

"[python]": {
   "editor.formatOnSave": true
 }