yapf

yapf是一个Python代码的格式化工具。

安装

1
$ pip install -U yapf

或者在使用快捷键(command + K + F)进行格式化时会有如下图所示的提醒.

然后选择安装Use yapf

在VS Code中配置

我们可以在VS Code的全局Settings中配置,也可以在当前工程的.vscode/setttings.json中配置。

下面演示的是在当前工程中配置。

.vscode/settings.json

1
2
3
4
5
6
{ 
"[python]": {
"editor.formatOnSave": true
},
"python.formatting.provider": "yapf"
}

其中editor.formatOnSave是指在保存时便会自动格式化。

配置yapf

在项目的根目录中添加一个yapf的自定义配置文件.style.yapf来进行style定制。

.style.yapf

1
2
3
4
5
[style]
based_on_style = pep8
INDENT_WIDTH = 2
CONTINUATION_INDENT_WIDTH = 2
COLUMN_LIMIT = 120

更多参数可以在这里查阅。

配置文件用法的详细说明:

1
2
3
4
5
YAPF will search for the formatting style in the following manner:
Specified on the command line
In the [style] section of a .style.yapf file in either the current directory or one of its parent directories.
In the [yapf] section of a setup.cfg file in either the current directory or one of its parent directories.
In the [style] section of a ~/.config/yapf/style file in your home directory.

Reference