黑色不支持 "Format Selection" 命令 - VS Code 错误

Black does not support "Format Selection" command - VS Code error

我需要在 Vs Code 中缩进我的 python 文件。我按照正常程序,

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

但我的问题是,每次我尝试格式化 python 文件时,它都会显示

Black does not support "Format Selection"

所以有人可以解释这里出了什么问题吗? 我的 python 版本是 Python 3.7.6

VS 代码详情:

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 

听起来您的键绑定设置为 运行 "Format Selection" 而不是 "Format Document";黑方只支持后者,不支持前者。如果您 运行 "Format Document" 命令应该可以正常工作。

在我的情况下(select black 作为 VS 代码设置中的 Python 格式化提供程序),我每次将一些文本粘贴到 时都会遇到此警告编辑.

而VS Code的官方文档has a solution专门针对它:

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
}

确保 编辑器:在保存模式下格式化 > 文件

黑色在设计上不支持格式选择选项。在此错误中,您可以 find the reasoning.

如果您想使用黑色,您必须配置为格式化所有文件并禁用格式选择。您的 settings.json 必须具有此配置:

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

如果你真的想在现有项目中使用Black,我的建议是尝试使用darker。我已经成功地在我开始使用黑色开发的代码中使用它,但同事没有使用。现在我可以在不破坏 git blame 信息的情况下对其进行编辑。