Skip to content

language: Add context-aware decrease indent for Python #33370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

smitbarmase
Copy link
Member

@smitbarmase smitbarmase commented Jun 25, 2025

Closes #33238, follow-up to #29625. Blocked on tree-sitter/tree-sitter-python#304

Changes:

  • Removed significant_indentation, which was the way to introduce indentation scoping in languages like Python. However, it turned out to be unnecessarily complicated to define and maintain.
  • Introduced decrease_indent_patterns, which takes a pattern keyword to automatically outdent and valid_after keywords to treat as valid code points to snap to. The outdent happens to the most recent valid_after keyword that also has less or equal indentation than the currently typed keyword.

Fixes:

  1. In Python, typing except, finally, else, and so on now automatically indents intelligently based on the context in which it appears. For instance:
try:
    if a == 1:
        try:
             b = 2
             ^  # <-- typing "except:" here would indent it to inner try block

but,

try:
    if a == 1:
        try:
             b = 2
    ^  # <-- typing "except:" here would indent it to outer try block
  1. Fixes comments not maintaining indent.

Release Notes:

  • Improved auto outdent for Python while typing keywords like except, else, finally, etc.
  • Fixed the issue where comments in Python would not maintain their indentation.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

python: Only comment in if statement auto outdents it
1 participant