简体   繁体   English

为 python 函数更改 VS 代码中的缩进

[英]change indentation in VS code for python functions

I am using VS code to write python code.我正在使用 VS 代码编写 python 代码。

When writing functions I get:编写函数时,我得到: 在此处输入图像描述

What I would like to have when I hit return after every variable of the method is:当我在方法的每个变量之后点击返回时,我想要的是: 在此处输入图像描述

But after hitting return after the first argument the next line starts just under "def".但是在第一个参数之后点击返回后,下一行就在“def”下面开始。

After looking for solutions in inte.net I read somewhere that adding this to settings.json would solve it:在 inte.net 中寻找解决方案后,我在某处读到将其添加到 settings.json 将解决它:

"editor.autoIndent": true, "editor.indentAfterOpenBracket": "control" } "editor.autoIndent": true, "editor.indentAfterOpenBracket": "control" }

But this is not the case and the behavior remains the same.但事实并非如此,行为保持不变。

How and what should be added in settings.json to get this behavior.如何以及应该在 settings.json 中添加什么来获得这种行为。

To change the indentation of Python functions in VS Code, you can use YAPF , you can install the "YAPF Formatter" extension and then configure it to format your code using the desired indentation style.要在 VS Code 中更改 Python 函数的缩进,您可以使用YAPF ,您可以安装“YAPF Formatter”扩展,然后将其配置为使用所需的缩进样式来格式化代码。 Here are the steps to do this:以下是执行此操作的步骤:

  • Install the " YAPF Formatter " extension in VS Code by searching for it in the Extensions marketplace or by visiting the following link: https://marketplace.visualstudio.com/items?itemName=mr-konn.yapf通过在扩展市场中搜索或访问以下链接,在 VS Code 中安装“ YAPF Formatter ”扩展: https://marketplace.visualstudio.com/items?itemName=mr-konn.yapf

  • Open the settings.json file in VS Code by going to File > Preferences > Settings or by pressing Ctrl +,通过转到“文件”>“首选项”>“设置”或按Ctrl +,

  • In the settings.json file, add the following lines to configure YAPF to use the desired indentation style:settings.json文件中,添加以下行以配置YAPF以使用所需的缩进样式:

    "python.formatting.yapfArgs": [
        "--style={based_on_style: pep8, indent_width: 4}"
    ],
  • Save the settings.json file and then you can format your python file by going to Edit > Format Document or by using the shortcut key Ctrl+Shift+I保存 settings.json 文件,然后您可以通过转到“编辑”>“格式化文档”或使用快捷键Ctrl+Shift+I来格式化 python 文件

Note: You can also configure YAPF to use different indentation style other than the one mentioned in the above example by modifying the indent_width and based_on_style fields in the yapfArgs settings注意:您还可以通过修改 yapfArgs 设置中的 indent_width 和 based_on_style 字段,将 YAPF 配置为使用与上例中提到的不同的缩进样式

I don't think there is such setting in VSCode.我认为 VSCode 中没有这样的设置。 To do that you need to use a formatter.为此,您需要使用格式化程序。 You can configure your VSCode to use Black .您可以将 VSCode 配置为使用Black

It doesn't matter how long the length of the line is, if you put a , at the end of your parameters, it puts them in separate lines:不管行的长度有多长,如果你在参数的末尾加上, ,它会将它们放在不同的行中:

# in
def short(a, b,):
    pass

# out
def short(
    a,
    b,
):
    pass

But the parenthesis are problem here(unless you don't care).但是括号在这里是问题(除非你不在乎)。 Black doesn't put them along parameters like how you showed.布莱克不会像你展示的那样把它们放在参数上。

I use balck, you can run command pip install black and add the following codes to your settings.json:我使用 balck,你可以运行命令pip install black并将以下代码添加到你的设置中。json:

  "python.formatting.provider": "black",

You can read black document for more details.您可以阅读黑色文档以获取更多详细信息。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM