microsoft/vscode-black-formatter

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

Closed this issue · 3 comments

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)

Do you have following settings in your settings.json?

  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true
  }

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?

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
    },