microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
149 stars 35 forks source link

How to Merge Code Within Parentheses into a Single Line Using Black Formatter in VSCode? #528

Closed Qizhi697 closed 2 months ago

Qizhi697 commented 2 months ago

How can I use the black-formatter plugin in VSCode to merge code within the same parentheses into a single line?

For example, I want the following two lines of code:

nccl_kernel = NcclKernel(kernel_id, node_proc, nccl_comm, nccl_rank,
                         func_index, launch_order, channel_count)

to automatically merge into one line after pressing Ctrl+S:

nccl_kernel = NcclKernel(kernel_id, node_proc, nccl_comm, nccl_rank, func_index, launch_order, channel_count)
karthiknadig commented 2 months ago

Do you have following settings in your settings.json?

  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true
  }
Qizhi697 commented 2 months ago

Thank you, it's working now. Additionally, when I press Ctrl+S, the single quotes in Python (' ') automatically change to double quotes (" "). How can I disable this automatic change?

karthiknadig commented 2 months ago

Additionally, when I press Ctrl+S, the single quotes in Python (' ') automatically change to double quotes (" "). How can I disable this automatic change?

This extension just runs black for you. You will need to see if black has a command line option for things like this. You can add —skip-string-normalization. Like this:


"black-formatter.args": [
        "--line-length", "300",
        "--skip-magic-trailing-comma",
        “— skip-string-normalization“
    ],
    "[python]": {
        "editor.defaultFormatter": "ms-python.black-formatter",
        "editor.formatOnSave": true
    },