editor: Fix semantic tokens missing when opening buffer from multibuffer#53712
Conversation
A race condition between set_visible_line_count and ScrollPositionChanged caused the initial update_lsp_data call to be dropped for singleton buffers opened from multibuffer search results. The first-render task (scroll.rs:682) spawns update_lsp_data, but the autoscroll from open_buffers_in_workspace replaces post_scroll_update with update_data_on_scroll, which skips update_lsp_data for singletons. Add a needs_initial_lsp_data flag so update_data_on_scroll runs update_lsp_data at least once even for singleton buffers. Fixes zed-industries#53051
There was a problem hiding this comment.
Thank you for this very nice find.
Before it gets too late, a few notes:
- the new test passes on
mainhence does not test anything related really - we'd better rework the code to use
update_data_on_scrollin both cases then fold_creasesbetter re-set thepost_scroll_updatetask then, as it's now important to make all of them sequential
Thanks for the review! Here's what I changed:
On the test: it passes on |
|
Just to mention, I do not see those changes described.
The reset seems somewhat odd? I would expect all code paths to replace the old task with the new task, and, depending on the conditions, use the timeout or not.
That does not make any sense to me: I have taken the test only and applied to |
- Rename update_data_on_scroll to update_visible_data, which now sets post_scroll_update itself so all callers use one method. - Non-debounce path calls do_update_visible_data directly instead of spawning a task. - Rename needs_initial_lsp_data to needs_initial_data_update. - Rewrite test with a large file so autoscroll triggers the race condition. The test now fails on main and passes with the fix.
You're right, the test can't fail on main. This was wrong, and I apologize for the confusion. I rewrote the test and it now fails on main with no other code changes. The previous test used a 1-line file. Autoscroll had nothing to scroll, so ScrollPositionChanged never fired, and the initial set_visible_line_count task ran fine. The race never triggered. The new test uses a 100-line file with the multibuffer excerpt at lines 95-100. When the singleton opens and autoscrolls to that position, it actually changes the scroll position. That fires ScrollPositionChanged, whose handler replaces post_scroll_update with a debounced task that goes through update_data_on_scroll, which skips update_lsp_data for singletons. The initial task from set_visible_line_count gets dropped, tokens are never requested. Also pushed the other changes you asked for:
If you find any other thing that you think it's not good, please let me know |
The reviewer only requested renaming the field, not the function. Revert update_visible_data back to update_data_on_scroll.
|
Once everything is validated I'll squash everything into a single commit |
SomeoneToIgnore
left a comment
There was a problem hiding this comment.
Thank you, this looks quite good now — will wait until it turns into a ready PR, not a draft.
…fer (zed-industries#53712) Summary Semantic token highlighting was missing when opening a file from multibuffer search results (Ctrl+Shift+F). Which file got hit depended on window size and scroll offset. ## Root cause Two async tasks race to write `post_scroll_update`: 1. `set_visible_line_count` (scroll.rs:682) fires on first render and spawns a task that calls `register_visible_buffers` + `update_lsp_data` (requests semantic tokens). 2. `open_buffers_in_workspace` (editor.rs:25049) calls `change_selections` with autoscroll right after creating the editor. This emits `ScrollPositionChanged`, whose handler (editor.rs:2655) replaces `post_scroll_update` with a task calling `update_data_on_scroll`. 3. `update_data_on_scroll` (editor.rs:26099) has a singleton guard: `if !self.buffer().read(cx).is_singleton()` that skips `update_lsp_data` for single-file buffers. This is a scroll optimization, singleton buffers don't change their visible buffer set on scroll. 4. The initial task gets dropped, the replacement skips `update_lsp_data`, semantic tokens are never requested. ## Fix Added a `needs_initial_lsp_data` flag to the Editor struct, set to `true` on creation. `update_data_on_scroll` checks this flag alongside the singleton guard, so `update_lsp_data` runs at least once even for singletons. The flag flips to `false` right after, so subsequent scrolls behave exactly as before. No perf impact after the first render. ## Self-review checklist - [x] I've reviewed my own diff for quality, security, and reliability - [ ] Unsafe blocks (if any) have justifying comments - [ ] The content is consistent with the [UI/UX checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist) - [x] Tests cover the new/changed behavior - [x] Performance impact has been considered and is acceptable Closes zed-industries#53051 ## Demo Before: https://github.com/user-attachments/assets/77d07d95-cb4a-44ff-842d-1f7a46653ca9 After: https://github.com/user-attachments/assets/2c942f52-4ec3-459f-a97b-93919e4bfb3d ## Release notes - Fixed semantic token highlighting missing when opening a buffer from multibuffer search results
Summary
Semantic token highlighting was missing when opening a file from multibuffer search results (Ctrl+Shift+F). Which file got hit depended on window size and scroll offset.
Root cause
Two async tasks race to write
post_scroll_update:set_visible_line_count(scroll.rs:682) fires on first render and spawns a task that callsregister_visible_buffers+update_lsp_data(requests semantic tokens).open_buffers_in_workspace(editor.rs:25049) callschange_selectionswith autoscroll right after creating the editor. This emitsScrollPositionChanged, whose handler (editor.rs:2655) replacespost_scroll_updatewith a task callingupdate_data_on_scroll.update_data_on_scroll(editor.rs:26099) has a singleton guard:if !self.buffer().read(cx).is_singleton()that skipsupdate_lsp_datafor single-file buffers. This is a scroll optimization, singleton buffers don't change their visible buffer set on scroll.The initial task gets dropped, the replacement skips
update_lsp_data, semantic tokens are never requested.Fix
Added a
needs_initial_lsp_dataflag to the Editor struct, set totrueon creation.update_data_on_scrollchecks this flag alongside the singleton guard, soupdate_lsp_dataruns at least once even for singletons. The flag flips tofalseright after, so subsequent scrolls behave exactly as before. No perf impact after the first render.Self-review checklist
Closes #53051
Demo
Before:
https://github.com/user-attachments/assets/77d07d95-cb4a-44ff-842d-1f7a46653ca9
After:
semantic.mp4
Release notes