Skip to content

Conversation

@anicioalexandre
Copy link
Contributor

Closes #36376

Problem:
Multi-cursor edits/selections in multi-buffers view were jumping to incorrect locations after toggling buffer folds. When users created multiple selections across different buffers in a multi-buffer view (like project search results) and then folded one of the buffers, subsequent text insertion would either:

  1. Insert text at wrong locations (like at the top of the first unfolded buffer)
  2. Replace the entire content in some buffers instead of inserting at the intended cursor positions
  3. Create orphaned selections that caused corruption in the editing experience

The issue seems to happen because when a buffer gets folded in a multi-buffer view, the existing selections associated with that buffer become invalid anchor points.

Solution:

  1. Selection Cleanup on Buffer Folding
  • Added remove_selections_from_buffer() method that filters out all selections from a buffer when it gets folded
  • This prevents invalid selections from corrupting subsequent editing operations
  • Includes edge case handling: if all selections are removed (all buffers folded), it creates a default selection at the start of the first buffer to prevent panics
  1. Unfolding buffers before editing
  • Added unfold_buffers_with_selections() call in handle_input() ensures buffers with active selections are automatically unfolded before editing
  • This helps in fixing an edge case (covered in the tests) where, if you fold all buffers in a multi-buffer view, and try to insert text in a selection, it gets unfolded before the edit happens. Without this, the inserted text would override the entire buffer content.
  • If we don't care about this edge case, we could remove this method. I find it ok to add since we already trigger buffer unfolding after edits with Event::ExcerptsEdited.

Release Notes:

  • Fixed multi-cursor edits jumping to incorrect locations after toggling buffer folds in multi-buffer views (e.g, project search)
    • Multi-cursor selections now properly handle buffer folding/unfolding operations
    • Text insertion no longer occurs at the wrong positions when buffers are folded during multi-cursor editing
    • Eliminated content replacement bugs where entire buffer contents were incorrectly overwritten
    • Added safe fallback behavior when all buffers in a multi-buffer view are folded
@cla-bot
Copy link

cla-bot bot commented Sep 11, 2025

We require contributors to sign our Contributor License Agreement, and we don't have @anicioalexandre on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'.

@anicioalexandre
Copy link
Contributor Author

@cla-bot check

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Sep 11, 2025
@cla-bot
Copy link

cla-bot bot commented Sep 11, 2025

The cla-bot has been summoned, and re-checked this pull request!

@anicioalexandre anicioalexandre force-pushed the fix/36376/multi-buffer-selections-folding branch from f063cec to 3b92dc0 Compare September 11, 2025 01:05
@anicioalexandre
Copy link
Contributor Author

anicioalexandre commented Sep 18, 2025

@maxbrunsfeld could you have a look when you have some time, please?

@anicioalexandre anicioalexandre force-pushed the fix/36376/multi-buffer-selections-folding branch from 3b92dc0 to ddd3033 Compare October 11, 2025 22:58
@anicioalexandre anicioalexandre force-pushed the fix/36376/multi-buffer-selections-folding branch 2 times, most recently from 15c5734 to 72cde7f Compare October 18, 2025 12:36
@anicioalexandre
Copy link
Contributor Author

@smitbarmase do you think we could move forward with this?

Copy link
Member

@smitbarmase smitbarmase left a comment

Choose a reason for hiding this comment

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

I added few more comments, I think we can merge after those are resolved.

@smitbarmase
Copy link
Member

Could you also rebase this on the latest main?

@smitbarmase smitbarmase force-pushed the fix/36376/multi-buffer-selections-folding branch from 6b38389 to 72b84ef Compare November 10, 2025 19:21
@smitbarmase smitbarmase changed the title Fix multi-buffer selections and edits during folding Nov 11, 2025
@smitbarmase smitbarmase merged commit dc372e8 into zed-industries:main Nov 11, 2025
24 checks passed
@smitbarmase
Copy link
Member

Thank you!

11happy pushed a commit to 11happy/zed that referenced this pull request Dec 1, 2025
… buffer fold (zed-industries#37953)

Closes zed-industries#36376 

Problem:
Multi-cursor edits/selections in multi-buffers view were jumping to
incorrect locations after toggling buffer folds. When users created
multiple selections across different buffers in a multi-buffer view
(like project search results) and then folded one of the buffers,
subsequent text insertion would either:

1. Insert text at wrong locations (like at the top of the first unfolded
buffer)
2. Replace the entire content in some buffers instead of inserting at
the intended cursor positions
3. Create orphaned selections that caused corruption in the editing
experience

The issue seems to happen because when a buffer gets folded in a
multi-buffer view, the existing selections associated with that buffer
become invalid anchor points.

Solution:
1. Selection Cleanup on Buffer Folding
- Added `remove_selections_from_buffer()` method that filters out all
selections from a buffer when it gets folded
- This prevents invalid selections from corrupting subsequent editing
operations
- Includes edge case handling: if all selections are removed (all
buffers folded), it creates a default selection at the start of the
first buffer to prevent panics

2. Unfolding buffers before editing  
- Added `unfold_buffers_with_selections()` call in `handle_input()`
ensures buffers with active selections are automatically unfolded before
editing
- This helps in fixing an edge case (covered in the tests) where, if you
fold all buffers in a multi-buffer view, and try to insert text in a
selection, it gets unfolded before the edit happens. Without this, the
inserted text would override the entire buffer content.
- If we don't care about this edge case, we could remove this method. I
find it ok to add since we already trigger buffer unfolding after edits
with `Event::ExcerptsEdited`.

Release Notes:

- Fixed multi-cursor edits jumping to incorrect locations after toggling
buffer folds in multi-buffer views (e.g, project search)
- Multi-cursor selections now properly handle buffer folding/unfolding
operations
- Text insertion no longer occurs at the wrong positions when buffers
are folded during multi-cursor editing
- Eliminated content replacement bugs where entire buffer contents were
incorrectly overwritten
- Added safe fallback behavior when all buffers in a multi-buffer view
are folded

---------

Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
smitbarmase added a commit that referenced this pull request Dec 8, 2025
…of the toggled one (#44394)

Closes #43870

Regressed in #37953

Release Notes:

- Fixed issue where toggling buffer fold focuses first buffer in
multi-buffer instead of the toggled one.
CherryWorm pushed a commit to CherryWorm/zed that referenced this pull request Dec 16, 2025
…of the toggled one (zed-industries#44394)

Closes zed-industries#43870

Regressed in zed-industries#37953

Release Notes:

- Fixed issue where toggling buffer fold focuses first buffer in
multi-buffer instead of the toggled one.
someone13574 pushed a commit to someone13574/zed that referenced this pull request Dec 16, 2025
…of the toggled one (zed-industries#44394)

Closes zed-industries#43870

Regressed in zed-industries#37953

Release Notes:

- Fixed issue where toggling buffer fold focuses first buffer in
multi-buffer instead of the toggled one.
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

2 participants