简体   繁体   中英

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.

I have these set in my settings.json:

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

I have tried to use the --exclude tag by adding the following to settings.json:

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

which is equivalent to a commandline call with black --exclude /*\.json/

I also tried

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

based on this post: VS Code Python + Black formatter arguments - python.formatting.blackArgs .

However, it is still formatting my settings.json.

Black doesn't format JSON. What's happening is VS Code has it's own included JSON formatter and that's what is formatting your settings.json . Do you have a setting turned on like "editor.formatOnSave" turned on? If so then it sounds like you want to scope it to just Python files, eg:

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

You can also disable the formatting for JSON, by:

  • going to the preferences with [ctrl+,] and disabling JSON > Format: Enable .
  • opening the settings.json file (open palette with [ctrl+shift+p] and search for "settings json") and add the following line:
     "json.format.enable": false

Or you can also limit the formatting to python files, by adding this setting in the settings.json file:

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

Black does format JSON and for me, broke it:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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