VSCode 安装配置 yapf

目录

  • yapf简介
  • 安装
  • 启用
  • 更改行最大字数
  • 另一种方法


yapf简介

yapf是一款由Google开源的Python代码自动格式化工具,它根据PEP 8规范可以帮我们自动格式化我们的代码,让代码更规范、更漂亮。

本文介绍VSCODE下面安装配置yapf

安装

pip install yapf

启用

  1. File -> Preferences -> Setting
  2. 搜索框输入:formatting.provider
  3. 下拉菜单选择 yapf

更改行最大字数

其中最大列宽被限制为80,如果超过80,在格式化时就会被yapf换行,随着现在人们的显示器越来越大,分辨率也越来越高,我们可以将这个限制适当修改。

找到对应项目里面的文件

\.venv\Lib\site-packages\yapf\yapflib\style.py

找到下面的函数

def CreatePEP8Style():
    return dict(
        ALIGN_CLOSING_BRACKET_WITH_VISUAL_INDENT=True,
        ALLOW_MULTILINE_LAMBDAS=False,
        ALLOW_MULTILINE_DICTIONARY_KEYS=False,
        ALLOW_SPLIT_BEFORE_DEFAULT_OR_NAMED_ASSIGNS=True,
        ALLOW_SPLIT_BEFORE_DICT_VALUE=True,
        ARITHMETIC_PRECEDENCE_INDICATION=False,
        BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF=False,
        BLANK_LINE_BEFORE_CLASS_DOCSTRING=False,
        BLANK_LINE_BEFORE_MODULE_DOCSTRING=False,
        BLANK_LINES_AROUND_TOP_LEVEL_DEFINITION=2,
        COALESCE_BRACKETS=False,
        COLUMN_LIMIT=79,
        CONTINUATION_ALIGN_STYLE='SPACE',
        CONTINUATION_INDENT_WIDTH=4,
        DEDENT_CLOSING_BRACKETS=False,
        INDENT_CLOSING_BRACKETS=False,
        DISABLE_ENDING_COMMA_HEURISTIC=False,
        EACH_DICT_ENTRY_ON_SEPARATE_LINE=True,
        I18N_COMMENT='',
        I18N_FUNCTION_CALL='',
        INDENT_DICTIONARY_VALUE=False,
        INDENT_WIDTH=4,
        INDENT_BLANK_LINES=False,
        JOIN_MULTIPLE_LINES=True,
        NO_SPACES_AROUND_SELECTED_BINARY_OPERATORS=set(),
        SPACE_BETWEEN_ENDING_COMMA_AND_CLOSING_BRACKET=True,
        SPACES_AROUND_POWER_OPERATOR=False,
        SPACES_AROUND_DEFAULT_OR_NAMED_ASSIGN=False,
        SPACES_BEFORE_COMMENT=2,
        SPLIT_ARGUMENTS_WHEN_COMMA_TERMINATED=False,
        SPLIT_ALL_COMMA_SEPARATED_VALUES=False,
        SPLIT_ALL_TOP_LEVEL_COMMA_SEPARATED_VALUES=False,
        SPLIT_BEFORE_ARITHMETIC_OPERATOR=False,
        SPLIT_BEFORE_BITWISE_OPERATOR=True,
        SPLIT_BEFORE_CLOSING_BRACKET=True,
        SPLIT_BEFORE_DICT_SET_GENERATOR=True,
        SPLIT_BEFORE_DOT=False,
        SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN=False,
        SPLIT_BEFORE_FIRST_ARGUMENT=False,
        SPLIT_BEFORE_LOGICAL_OPERATOR=True,
        SPLIT_BEFORE_NAMED_ASSIGNS=True,
        SPLIT_COMPLEX_COMPREHENSION=False,
        SPLIT_PENALTY_AFTER_OPENING_BRACKET=300,
        SPLIT_PENALTY_AFTER_UNARY_OPERATOR=10000,
        SPLIT_PENALTY_ARITHMETIC_OPERATOR=300,
        SPLIT_PENALTY_BEFORE_IF_EXPR=0,
        SPLIT_PENALTY_BITWISE_OPERATOR=300,
        SPLIT_PENALTY_COMPREHENSION=80,
        SPLIT_PENALTY_EXCESS_CHARACTER=7000,
        SPLIT_PENALTY_FOR_ADDED_LINE_SPLIT=30,
        SPLIT_PENALTY_IMPORT_NAMES=0,
        SPLIT_PENALTY_LOGICAL_OPERATOR=300,
        USE_TABS=False,
    )


COLUMN_LIMIT=79,
修改为
COLUMN_LIMIT=120,
这样你的列宽就修改为120了。

另一种方法

vscode的settings.json做如下修改,输入想要的字符数:

{
    "python.formatting.provider": "yapf",
    "python.formatting.yapfArgs": ["--style", "{column_limit: 79}"],
}

你可能感兴趣的:(python学习笔记)