Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Preserve delimiters when folding expressions.#24999

Merged
lerebear merged 2 commits into
mainfrom
lerebear/push-lxkvomkorolv
May 19, 2026
Merged

[ty] Preserve delimiters when folding expressions.#24999
lerebear merged 2 commits into
mainfrom
lerebear/push-lxkvomkorolv

Conversation

@lerebear

@lerebear lerebear commented May 5, 2026

Copy link
Copy Markdown
Contributor

Summary

This makes it so that we preserve outer delimiters when folding expressions. For instance:

x = [
    1,
    2,
    3,
]

now folds like so in a character-precise LSP client:

x = [...]

rather than like so:

x = ...

The same behavior applies to parethesized callables, call args, sets, tuples, comprehensions, type parameter lists, and subscripts.

I recommend reviewing this diff commit-by-commit.

Closes astral-sh/ty#3427.

Test Plan

Please see updated tests.

@astral-sh-bot astral-sh-bot Bot added server Related to the LSP server ty Multi-file analysis & type inference labels May 5, 2026
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch from 1f19b61 to 9dfa6dd Compare May 7, 2026 03:23
@lerebear lerebear force-pushed the lerebear/push-pwlqvypsrllr branch from 5c640b9 to 757f62a Compare May 7, 2026 03:23
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch from 9dfa6dd to 4b69534 Compare May 7, 2026 03:41
@lerebear lerebear force-pushed the lerebear/push-pwlqvypsrllr branch 2 times, most recently from d3ce232 to 0c8981e Compare May 7, 2026 05:23
Base automatically changed from lerebear/push-pwlqvypsrllr to main May 7, 2026 06:20
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch 5 times, most recently from 478b0c9 to fc5374c Compare May 8, 2026 02:58
@lerebear lerebear marked this pull request as ready for review May 8, 2026 03:11
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch from fc5374c to f481476 Compare May 8, 2026 03:43
@lerebear

lerebear commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Back to draft briefly while I handle a few edge cases that I've discovered.

@lerebear lerebear marked this pull request as draft May 8, 2026 04:08
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch 4 times, most recently from fdc9314 to 92b6ee9 Compare May 8, 2026 06:47
@AlexWaygood AlexWaygood removed their request for review May 11, 2026 21:22
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch 20 times, most recently from 38d6634 to 6eef6eb Compare May 15, 2026 22:33
@lerebear lerebear marked this pull request as ready for review May 15, 2026 22:43
Comment thread crates/ty_ide/src/folding_range.rs
Comment thread crates/ty_ide/src/folding_range.rs
Comment thread crates/ty_ide/src/folding_range.rs
Comment thread crates/ty_ide/src/folding_range.rs
}

/// Adds a folding range for an expression if it is enclosed in parentheses.
fn add_parenthesized_expression_range(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call this for every expression, because expressions can be parenthesized in many places:

if a * (
    b
    +
    c
): ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is unaddressed in the current implementation. I do however I think it slightly different in spirit. My goal here was primarily to improve folds that we already emit, whereas this points at the fact that we don't emit folds for every expression that we might. I think the point is well made, but safe to defer pending further feedback.

Comment thread crates/ty_ide/src/folding_range.rs
@lerebear lerebear force-pushed the lerebear/push-lxkvomkorolv branch from 6eef6eb to e0beb6c Compare May 19, 2026 05:41
@lerebear lerebear merged commit 18a5dd2 into main May 19, 2026
51 checks passed
@lerebear lerebear deleted the lerebear/push-lxkvomkorolv branch May 19, 2026 05:53
thejchap pushed a commit to thejchap/ruff that referenced this pull request May 23, 2026
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
- Does this PR follow our AI policy
(https://github.com/astral-sh/.github/blob/main/AI_POLICY.md)?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

This makes it so that we preserve outer delimiters when folding
expressions. For instance:

```py
x = [
    1,
    2,
    3,
]
```

now folds like so in a character-precise LSP client:

```py
x = [...]
```

rather than like so:

```py
x = ...
```

The same behavior applies to parethesized callables, call args, sets,
tuples, comprehensions, type parameter lists, and subscripts.

**I recommend reviewing this diff commit-by-commit.**

Closes astral-sh/ty#3427.

## Test Plan

Please see updated tests.
<!-- How was it tested? -->
anishgirianish pushed a commit to anishgirianish/ruff that referenced this pull request May 28, 2026
<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
- Does this PR follow our AI policy
(https://github.com/astral-sh/.github/blob/main/AI_POLICY.md)?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->

This makes it so that we preserve outer delimiters when folding
expressions. For instance:

```py
x = [
    1,
    2,
    3,
]
```

now folds like so in a character-precise LSP client:

```py
x = [...]
```

rather than like so:

```py
x = ...
```

The same behavior applies to parethesized callables, call args, sets,
tuples, comprehensions, type parameter lists, and subscripts.

**I recommend reviewing this diff commit-by-commit.**

Closes astral-sh/ty#3427.

## Test Plan

Please see updated tests.
<!-- How was it tested? -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Language server should emit delimiter-preserving folding ranges for collections

2 participants