1 Star 0 Fork 0

fight_cqh / vsc-python-indent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Python Indent

Correct python indentation in Visual Studio Code. See it on the VSCode Marketplace.

Build Status

How it works

Every time you press the Enter key in a python context, this extension will parse your python file up to the location of your cursor, and determine exactly how much the next line (or two in the case of hanging indents) should be indented. There are two main cases.

Between bracket pairs

In cases when you have your cursor between an open bracket (one of [({) and its closing bracket pair (the corresponding one of })]), this extension will keep subsequent lines indented just to the right of where it was opened:

data = {'a': 0,
        | # <- pressing enter should put your cursor at the "|"
| # <- This is where default VS Code puts your cursor.

Even heavily nested brackets are handled:

data = {'a': 0,
        'b': [[1, 2],
              | # <- match the more recently opened [ instead of the {
        | # <- default behavior of VS Code.
data = {'a': 0,
        'b': [[1, 2],
              [3, 4]],
        | # <- since the lists are all closed, go back to the { position
              | # <- default behavior of VS Code.
data = {'a': 0,
        'b': [[1, 2],
              [3, 4]],
        'c': 5}
| # <- go back to indentation level before any brackets were opened
        | # <- default behavior of VS Code.

In the full example below, default VS Code required nine extra key presses (three tab's, two space's, and four backspace's) to match the automatic indentation of this extension.

data = {'a': 0,
        'b': [[1, 2],
              [3, 4]],
        'c': 5}
done(data)

Hanging indents

When you have opened a bracket, but not yet inserted any content, pressing Enter will create a hanging indent, matching the base behavior of VS Code.

result = my_func(
    | # <- your cursor should end up here
) # <- the closing bracket should end up here

If there is other content, then this extension falls back on just indenting by your set tab size.

# The "|" is your cursor's location.
result = my_func(|x, y, z)
# and when you press enter...
result = my_func(
    |x, y, z)

Why is it needed?

There are many related issues on GitHub ([1], [2], [3], [4], [5]) asking for improved python indentation in VS Code. It seems like the maintainers of the python extension at microsoft are not prioritizing indentation, since there has been no progress in the years since it was first asked for.

Caveats

This extension is new, and may have problems. Some known caveats are listed below.

  • Using tabs (\t) for your indentation will likely not work.
  • If your python code is not correctly formatted, you may not get correct indentation.
  • The extension works by registering the Enter key as a keyboard shortcut. The conditions when the shortcut is triggered have been heavily restricted, but there may still be times this extension is unexpectedly overriding Enter behavior.

If you experience any problems, please submit an issue, or better yet a pull request.

Release Notes

See the change log.

Developing

See the developer docs for pointers on how to develop this extension.

MIT License Copyright (c) 2019 Kevin Rose Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/fight_cqh/vsc-python-indent.git
git@gitee.com:fight_cqh/vsc-python-indent.git
fight_cqh
vsc-python-indent
vsc-python-indent
master

搜索帮助