Skip to content

language: Fix indent suggestions for significant indented languages like Python #29625

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

Merged
merged 8 commits into from
May 7, 2025

Conversation

smitbarmase
Copy link
Member

@smitbarmase smitbarmase commented Apr 29, 2025

Closes #26157

This fixes multiple cases where Python indentation breaks:

  • Adding a new line after if, try, etc. correctly indents in that scope
  • Multi-cursor tabs correctly preserve relative indents
  • Adding a new line after else, finally, etc. correctly outdents them
  • Existing Tests

Future Todo: I need to add new tests for all the above cases.

Before/After:

  1. Multi-cursor tabs correctly preserve relative indents
python-1.mp4
  1. Adding a new line after if, try, etc. correctly indents in that scope
python-2.mp4

Release Notes:

  • Fixes indentation-related issues involving tab, newline, etc for Python.

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Apr 29, 2025
@smitbarmase smitbarmase marked this pull request as draft April 29, 2025 21:21
@smitbarmase smitbarmase changed the title language: Improve suggest autoindent for indentation-based languages language: Fix indentation suggestions for off-side languages like Python May 6, 2025
@smitbarmase smitbarmase force-pushed the fix-indent-issues-for-python branch from f1feee2 to 38effc6 Compare May 7, 2025 06:36
@smitbarmase smitbarmase marked this pull request as ready for review May 7, 2025 15:47
@smitbarmase smitbarmase force-pushed the fix-indent-issues-for-python branch from ac1622d to 1dcbbf4 Compare May 7, 2025 17:08
@smitbarmase smitbarmase changed the title language: Fix indentation suggestions for off-side languages like Python language: Fix indentation suggestions for significant indented languages like Python May 7, 2025
@smitbarmase smitbarmase changed the title language: Fix indentation suggestions for significant indented languages like Python language: Fix indent suggestions for significant indented languages like Python May 7, 2025
@smitbarmase smitbarmase merged commit 7c76cee into main May 7, 2025
21 checks passed
@smitbarmase smitbarmase deleted the fix-indent-issues-for-python branch May 7, 2025 17:35
lj3954 pushed a commit to lj3954/zed that referenced this pull request May 9, 2025
…ike Python (zed-industries#29625)

Closes zed-industries#26157

This fixes multiple cases where Python indentation breaks:
- [x] Adding a new line after `if`, `try`, etc. correctly indents in
that scope
- [x] Multi-cursor tabs correctly preserve relative indents
- [x] Adding a new line after `else`, `finally`, etc. correctly outdents
them
- [x] Existing Tests

Future Todo: I need to add new tests for all the above cases.

Before/After:

1. Multi-cursor tabs correctly preserve relative indents


https://github.com/user-attachments/assets/08a46ddf-5371-4e26-ae7d-f8aa0b31c4a2

2. Adding a new line after `if`, `try`, etc. correctly indents in that
scope


https://github.com/user-attachments/assets/9affae97-1a50-43c9-9e9f-c1ea3a747813

Release Notes:

- Fixes indentation-related issues involving tab, newline, etc for
Python.
smitbarmase added a commit that referenced this pull request May 18, 2025
This PR add tests for a recent PR: [language: Fix indent suggestions for
significant indented languages like
Python](#29625)

It also covers cases from past related issues so that we don't end up
circling back to them on future fixes.

- [Python incorrect auto-indentation for
except:](#10832)
- [Python for/while...else indention overridden by if statement
](#30795)
- [Python: erroneous indent on newline when comment ends in
:](#25416)
- [Newline in Python file does not indent
](#16288)
- [Tab Indentation works incorrectly when there are multiple
cursors](#26157)

Release Notes:

- N/A
smitbarmase pushed a commit that referenced this pull request May 24, 2025
#31260)

Follow-up to #29625 and #30902

This PR reintroduces auto-intents for brackets in Python and fixes some
cases where an indentation would be triggered if it should not. For
example, upon typing

```python
a = []
```
and inserting a newline after, the next line would be indented although
it shoud not be.

Bracket auto-indentation was tested prior to #29625 but removed there
and the test updated accordingly. #30902 reintroduced this for all
brackets but `()`. I reintroduced this here, reverted the changes to the
test so that indents also happen after typing `()`. This is frequently
used for tuples and multiline statements in Python.

Release Notes:

- Improved auto-indentation when using round brackets in Python.
smitbarmase pushed a commit that referenced this pull request May 27, 2025
#31260)

Follow-up to #29625 and #30902

This PR reintroduces auto-intents for brackets in Python and fixes some
cases where an indentation would be triggered if it should not. For
example, upon typing

```python
a = []
```
and inserting a newline after, the next line would be indented although
it shoud not be.

Bracket auto-indentation was tested prior to #29625 but removed there
and the test updated accordingly. #30902 reintroduced this for all
brackets but `()`. I reintroduced this here, reverted the changes to the
test so that indents also happen after typing `()`. This is frequently
used for tuples and multiline statements in Python.

Release Notes:

- Improved auto-indentation when using round brackets in Python.
smitbarmase added a commit that referenced this pull request Jun 26, 2025
Closes #33238, follow-up to
#29625.

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:

```py
try:
    if a == 1:
        try:
             b = 2
             ^  # <-- typing "except:" here would indent it to inner try block
```

but,

```py
try:
    if a == 1:
        try:
             b = 2
    ^  # <-- typing "except:" here would indent it to outer try block
```

2. 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.
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jun 28, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 1, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 4, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
aviatesk added a commit to aviatesk/zed-julia that referenced this pull request Jul 4, 2025
This commit adds both regex-based and Tree-sitter-based indentation
rules to provide a smooth editing experience for Julia coding in Zed.

Motivation - this didn't work properly before:
```julia
if x
elseif y
    else    # <- wouldn't auto-outdent
end
```

Changes:
- Add `increase_indent_pattern` for Julia block keywords
- Add `decrease_indent_patterns` with context-aware `valid_after` rules
- Add `@start.suffix` captures in indents.scm for tracking block beginnings
  (required for `decrease_indent_patterns` to be functional)
- Update `@outdent` patterns to work with specific keywords

The dual approach (regex + Tree-sitter) is intentional, following Zed's
design pattern established for Python (see zed-industries/zed#29625,
zed-industries/zed#33370).
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 tab indentation works incorrectly when there are multiple cursors
1 participant