Black does not support "Format Selection" command
Asked Answered
M

5

49

I need to indent my python file in VS Code. I followed the normal procedure:

On Windows Shift + Alt + F
On Mac Shift + Option + F
On Linux Ctrl + Shift + I

But my question is every time when I try to format python file it it says

Black does not support "Format Selection"

So someone can explain what's going wrong here?

My python version is Python 3.7.6.
VS Code details:

Version: 1.46.0 (user setup)
Commit: a5d1cc28bb5da32ec67e86cc50f84c67cc690321
Date: 2020-06-10T09:03:20.462Z
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0

OS: Windows_NT x64 10.0.18363 
Merrymaking answered 17/6, 2020 at 11:23 Comment(1)
you can try autopep8. it supports format selectionChoochoo
G
17

It sounds like your keybindings are set to run "Format Selection" instead of "Format Document"; Black only supports the latter, not the former. If you run the "Format Document" command that should work without issue.

Gameto answered 18/6, 2020 at 20:34 Comment(3)
what formatter you suggest if I need to have "format selection" ?Choochoo
We have not added explicit support to any formatter for selection formatting, so I don't have a recommendation.Gameto
autopep8 does support Format Selection, as opposed to black. That's the only reason I use autopep8, I'd prefer black for the rest.Cr
C
72

In my situation (select black as the Python Formatting Provider in VS Code Settings), I encountered this warning everytime when I pasting some text into the editor.

And the official documentation of VS Code has a solution specifically for it:

When using the black formatter, VS Code issues the following warning when pasting source code into the editor: Black does not support the "Format Select" command.

To prevent this warning, add the following entry to your user or workspace settings to disable format on paste for Python files:

"[python]": {
    "editor.formatOnPaste": false
}
Conquistador answered 27/8, 2020 at 8:46 Comment(2)
As per the official documentation you must also have "[python]": { "editor.formatOnSaveMode": "file" }Nebulosity
This response fixed my problem!Calan
G
17

It sounds like your keybindings are set to run "Format Selection" instead of "Format Document"; Black only supports the latter, not the former. If you run the "Format Document" command that should work without issue.

Gameto answered 18/6, 2020 at 20:34 Comment(3)
what formatter you suggest if I need to have "format selection" ?Choochoo
We have not added explicit support to any formatter for selection formatting, so I don't have a recommendation.Gameto
autopep8 does support Format Selection, as opposed to black. That's the only reason I use autopep8, I'd prefer black for the rest.Cr
I
16

Make sure to Editor: Format On Save Mode > File enter image description here

Impersonalize answered 30/3, 2021 at 16:37 Comment(3)
Thanks for the clear explanation! that worked for me! for me it was sat on the user setting to be modificationsDicarlo
Bingo! Clearly does the job. In most dev shops CI/CD will require all files to be blackd anyway so it doesn't hurt in most cases that you're formatting the whole file. Nice.Wooded
Also set Format On Paste to False, because Black can't format paste chunks.Cahra
N
5

Black does not support the format selection option by design. In this bug you can find the reasoning.

If you want to use black you must configure to format all file and disable the format selection. Your settings.json must have this configuration:

 "[python]": { 
     "editor.formatOnSaveMode": "file",       
     "editor.formatOnPaste": false
 }

If you really want to use Black in an existing project, my suggestion is to try to use darker. I've been successfully using it in a code that I started to develop using black, but a coworker didn't use. Now I can edit it without breaking git blame info.

Nicias answered 23/2, 2022 at 20:29 Comment(0)
E
0

In my case I had an old setting

"[python]": {
  "editor.formatOnSaveMode": "modifications"
}

To fix the issue I've replaced it to be this:

"[python]": {
  "editor.formatOnSaveMode": "file"
}

And problem disappear.

Ethnography answered 10/5, 2023 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.