简体   繁体   English

在 VS Code 中,我不想让 Black 格式化我的设置。json

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

I want to use the Black formatter for my Python files, but not for my JSON settings.我想为我的 Python 文件使用黑色格式化程序,但不想为我的 JSON 设置使用黑色格式化程序。

I have these set in my settings.json:我在 settings.json 中设置了这些:

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

I have tried to use the --exclude tag by adding the following to settings.json:我试图通过将以下内容添加到 settings.json 来使用 --exclude 标签:

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

which is equivalent to a commandline call with black --exclude /*\.json/这相当于使用black --exclude /*\.json/的命令行调用

I also tried我也试过

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

based on this post: VS Code Python + Black formatter arguments - python.formatting.blackArgs .基于这篇文章: VS Code Python + Black formatter arguments - python.formatting.blackArgs

However, it is still formatting my settings.json.但是,它仍在格式化我的 settings.json。

Black doesn't format JSON.黑色不格式化 JSON。 What's happening is VS Code has it's own included JSON formatter and that's what is formatting your settings.json .发生的事情是 VS Code 有它自己包含的 JSON 格式化程序,这就是格式化您的settings.json Do you have a setting turned on like "editor.formatOnSave" turned on?您是否打开了"editor.formatOnSave"类的设置? If so then it sounds like you want to scope it to just Python files, eg:如果是这样,那么听起来您想将 scope 它仅转换为 Python 文件,例如:

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

You can also disable the formatting for JSON, by:您还可以通过以下方式禁用 JSON 的格式:

  • going to the preferences with [ctrl+,] and disabling JSON > Format: Enable .使用 [ctrl+,] 转到首选项并禁用JSON > Format: Enable
  • opening the settings.json file (open palette with [ctrl+shift+p] and search for "settings json") and add the following line:打开 settings.json 文件(使用 [ctrl+shift+p] 打开调色板并搜索“settings json”)并添加以下行:
     "json.format.enable": false

Or you can also limit the formatting to python files, by adding this setting in the settings.json file:或者您也可以通过在 settings.json 文件中添加此设置,将格式限制为 python 文件:

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

Black does format JSON and for me, broke it: Black 确实格式化了 JSON,对我来说,打破了它:

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

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

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

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