能否通过.editorconfig控制PyCharm中Python Docstring的缩进格式?
如何通过.editorconfig控制PyCharm中Python Docstring的缩进样式
我正在使用PyCharm IDE,希望通过.editorconfig文件实现Python docstring缩进的自动化配置。目前我已通过以下配置实现了几乎所有代码格式的管控(供参考):
[{*.py,*.pyw}] ij_python_align_collections_and_comprehensions = true ij_python_align_multiline_imports = true ij_python_align_multiline_parameters = true ij_python_align_multiline_parameters_in_calls = true ij_python_blank_line_at_file_end = true ij_python_blank_lines_after_imports = 1 ij_python_blank_lines_after_local_imports = 0 ij_python_blank_lines_around_class = 1 ij_python_blank_lines_around_method = 1 ij_python_blank_lines_around_top_level_classes_functions = 2 ij_python_blank_lines_before_first_method = 0 ij_python_call_parameters_new_line_after_left_paren = false ij_python_call_parameters_right_paren_on_new_line = false ij_python_call_parameters_wrap = normal ij_python_dict_alignment = 0 ij_python_dict_new_line_after_left_brace = false ij_python_dict_new_line_before_right_brace = false ij_python_dict_wrapping = 1 ij_python_from_import_new_line_after_left_parenthesis = false ij_python_from_import_new_line_before_right_parenthesis = false ij_python_from_import_parentheses_force_if_multiline = false ij_python_from_import_trailing_comma_if_multiline = false ij_python_from_import_wrapping = 1 ij_python_hang_closing_brackets = false ij_python_keep_blank_lines_in_code = 1 ij_python_keep_blank_lines_in_declarations = 1 ij_python_keep_indents_on_empty_lines = false ij_python_keep_line_breaks = true ij_python_method_parameters_new_line_after_left_paren = false ij_python_method_parameters_right_paren_on_new_line = false ij_python_method_parameters_wrap = normal ij_python_new_line_after_colon = false ij_python_new_line_after_colon_multi_clause = true ij_python_optimize_imports_always_split_from_imports = false ij_python_optimize_imports_case_insensitive_order = false ij_python_optimize_imports_join_from_imports_with_same_source = false ij_python_optimize_imports_sort_by_type_first = true ij_python_optimize_imports_sort_imports = true ij_python_optimize_imports_sort_names_in_from_imports = false ij_python_space_after_comma = true ij_python_space_after_number_sign = true ij_python_space_after_py_colon = true ij_python_space_before_backslash = true ij_python_space_before_comma = false ij_python_space_before_for_semicolon = false ij_python_space_before_lbracket = false ij_python_space_before_method_call_parentheses = false ij_python_space_before_method_parentheses = false ij_python_space_before_number_sign = true ij_python_space_before_py_colon = false ij_python_space_within_empty_method_call_parentheses = false ij_python_space_within_empty_method_parentheses = false ij_python_spaces_around_additive_operators = true ij_python_spaces_around_assignment_operators = true ij_python_spaces_around_bitwise_operators = true ij_python_spaces_around_eq_in_keyword_argument = false ij_python_spaces_around_eq_in_named_parameter = false ij_python_spaces_around_equality_operators = true ij_python_spaces_around_multiplicative_operators = true ij_python_spaces_around_power_operator = true ij_python_spaces_around_relational_operators = true ij_python_spaces_around_shift_operators = true ij_python_spaces_within_braces = false ij_python_spaces_within_brackets = false ij_python_spaces_within_method_call_parentheses = false ij_python_spaces_within_method_parentheses = false ij_python_use_continuation_indent_for_arguments = false ij_python_use_continuation_indent_for_collection_and_comprehensions = false ij_python_use_continuation_indent_for_parameters = true ij_python_wrap_long_lines = false若能在同一文件中管理所有缩进配置,即添加docstring相关配置会非常实用,请问是否可以通过.editorconfig控制docstring的样式?
可以通过.editorconfig配置PyCharm中的Python docstring缩进及样式,只需在现有的[{*.py,*.pyw}]区块中添加以下配置项即可:
# Docstring相关配置 ij_python_docstring_indentation = 4 # 设置docstring内部的缩进空格数,建议与代码缩进保持一致 ij_python_docstring_wrap_long_lines = true # 自动换行过长的docstring行 ij_python_docstring_use_continuation_indent = true # 对docstring的续行使用续行缩进 ij_python_docstring_blank_lines_after_summary = 1 # 在docstring的摘要描述后保留1个空行 ij_python_docstring_align_parameter_comments = true # 对齐docstring中的参数注释
配置说明
ij_python_docstring_indentation:控制docstring内部内容的缩进量,比如设置为4时,docstring内的每一行会缩进4个空格,和常规代码缩进统一。ij_python_docstring_wrap_long_lines:开启后,PyCharm会自动将超过指定长度的docstring行进行换行,保持格式整洁。ij_python_docstring_use_continuation_indent:启用后,docstring的续行部分会使用续行缩进(通常比基础缩进多4个空格),而非基础缩进。ij_python_docstring_blank_lines_after_summary:规范docstring结构,在开头的摘要描述后添加一个空行,分隔摘要和详细说明/参数列表。ij_python_docstring_align_parameter_comments:让docstring中类似:param xxx:的参数注释自动对齐,提升可读性。
添加这些配置后,PyCharm会自动应用docstring的格式规则,和你已有的代码格式配置统一管理。
内容的提问来源于stack exchange,提问作者A.Casanova
相关产品推荐
相关产品推荐

