Coder Social home page Coder Social logo

whistlewhileyouwork / vsc-python-indent Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kbrose/vsc-python-indent

0.0 0.0 0.0 2.6 MB

Correctly indent python code on the fly, in Visual Studio Code.

Home Page: https://marketplace.visualstudio.com/items?itemName=KevinRose.vsc-python-indent

License: MIT License

TypeScript 100.00%

vsc-python-indent's Introduction

Python Indent

Correct python indentation in Visual Studio Code. See it on the VSCode Marketplace. Fork the source code on GitHub.

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 and how much nearby lines should be un-indented. There are three 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)

It's not often used, but a backslash to continue a line will result in the next line being indented.

my_long_calculation = 1234 + \
    5678

Keywords

Some keywords in python imply certain indentation behaviors. For example, if there is a return statement, then we know the next line can be un-indented (or dedented) since no statements can follow a return in the same code block. Other keywords that follow the same pattern are pass, break, continue, and raise

Similarly, if there is an else: on the current line, that the current line needs to be dedented, and the next line needs to be indented relative to the new position of the else:. Other keywords that follow the same pattern are elif <stuff>:, except <stuff>:, and finally:. Some examples are shown below.

if True:
    pass
    else:|
# and when you press enter...
if True:
    pass
else:
    |

But if you have manually changed the indentation, then the extension should not change it for you:

if True:
    if True:
        pass
    else:|
# and when you press enter, do NOT dedent!
if True:
    if True:
        pass
    else:
        |

# Or even more nested

if True:
    if True:
        if True:
            pass
    else:|
# and when you press enter, still do NOT dedent
if True:
    if True:
        if True:
            pass
    else:
        |

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 relatively 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.