editor: Fix block cursor offset when selecting text#42837
editor: Fix block cursor offset when selecting text#42837dinocosta merged 3 commits intozed-industries:mainfrom
Conversation
82080f4 to
3e6799c
Compare
|
Hey @lennartkloock , thank you for opening this Pull Request 👋 I'm wondering if I'm doing something wrong or testing the wrong cgghanges, but seems like these changes don't actually fix the reproduction steps shared in #36677 . Here's a screen recording of me comparing both the current Zed Nightly with your changes ▼ CleanShot.2025-11-25.at.12.37.06.mp4In both cases I still see the issue with using Also, not sure if it helps but we also have the |
The issue doesn't happen in Vim mode. To reproduce the problem you have to disable Vim mode and enable the block or hollow cursor. Thanks for pointing me to that function, I will look if it helps in this case. Edit: I don't know if I'm missing something but I think both Edit: nvm I just saw the other issue #38104 but seems like it's unrelated because it only happens in Vim mode |
|
Unfortunately, the That said, what we could do is use |
3e6799c to
84ed3ad
Compare
Would you be able to share reproduction steps? I'm still having a hard time reproducing the issue that this Pull Request is attempting to fix 🙂 |
This comment was marked as spam.
This comment was marked as spam.
@aurexav If possible, can you share a small reproduction of the issue? Wanted to confirm that these changes actually fixed the issue but seems I was unable to reproduce the actual issue to check if it fixed it. |
Zed Zed Underline VSCode And you can continue press The memory level selection is correct but the visual is completely wrong. |
|
Thanks for sharing the reproduction steps, I didn't have time yet. You can also see this when selecting text using your mouse. |
|
@aurexav Thank you for sharing those steps! When you state "1. Use block cursor", do yo mean the following settings? {
"cursor_shape": "block",
}If so, I just tested these changes, comparing how it behaves against the latest stable release and it appears the behavior for CleanShot.2025-12-05.at.10.28.57.mp4The behavior of
If you have vim mode enabled, I believe this is meant to replicate vim's behavior, where if you're at the end of the line, pressing |
I don't know if there is a misunderstanding here but it seems like you are still in Vim mode in your video. The issue that is fixed here only appears if you're NOT in Vim mode. Please disable Vim mode for the reproduction steps to work. (set |
Yes.
I'm a VIM hater. I never use that mode. |
|
Here is a video I recorded demonstrating the issue. I hope that helps to further clarify it. Settings are: {
"vim_mode": false,
"cursor_shape": "block",
}Screencast.From.2025-12-09.12-12-03.mp4 |
|
Thank you for sharing @lennartkloock , was probably confusing what the real issue was but I've been able to reproduce it too! Will take a look at these changes shortly, likely during next week's Quality Week 🙂 |
Since we're already updating the vim's editor in that function, we can avoid an extre call to `self.update_editor`.
…Settings (#44889) In a previous Pull Request, a new field was added to `editor::Editor`, namely `cursor_offset_on_selection`, in order to control whether the cursor representing the head of a selection should be positioned in the last selected character, as we have on Vim mode, or after, like we have when Vim mode is disabled. This field would then be set by the `vim` crate, depending on the current vim mode. However, it was noted that `vim_mode_setting::VimModeSetting` already exsits and allows other crates to determine whether Vim mode is enabled or not. Since we're already checking `!range.is_empty()` in `editor::element::SelectionLayout::new` we can then rely on simply determining whether Vim mode is enabled to decide whether tho shift the cursor one position to the left when making a selection. As such, this commit removes the `cursor_offset_on_selection` field, as well as any related methods in favor of a new `Editor.vim_mode_enabled` method, which can be used to achieve the same behavior. Relates to #42837 Release Notes: - N/A
|
Will this be included in Thursday's preview version? |
…2837) Vim visual mode and Helix selection mode both require the cursor to be on the last character of the selection. Until now, this was implemented by offsetting the cursor one character to the left whenever a block cursor is used. (Since the visual modes use a block cursor.) However, this oversees the problem that **some users might want to use the block cursor without being in visual mode**. Meaning that the cursor is offset by one character to the left even though Vim/Helix mode isn't even activated. Since the Vim mode implementation is separate from the `editor` crate the solution is not as straightforward as just checking the current vim mode. Therefore this PR introduces a new `Editor` struct field called `cursor_offset_on_selection`. This field replaces the previous check condition and is set to `true` whenever the Vim mode is changed to a visual mode, and `false` otherwise. Closes zed-industries#36677 and zed-industries#20121 Release Notes: - Fixes block and hollow cursor being offset when selecting text --------- Co-authored-by: dino <dinojoaocosta@gmail.com>
…Settings (zed-industries#44889) In a previous Pull Request, a new field was added to `editor::Editor`, namely `cursor_offset_on_selection`, in order to control whether the cursor representing the head of a selection should be positioned in the last selected character, as we have on Vim mode, or after, like we have when Vim mode is disabled. This field would then be set by the `vim` crate, depending on the current vim mode. However, it was noted that `vim_mode_setting::VimModeSetting` already exsits and allows other crates to determine whether Vim mode is enabled or not. Since we're already checking `!range.is_empty()` in `editor::element::SelectionLayout::new` we can then rely on simply determining whether Vim mode is enabled to decide whether tho shift the cursor one position to the left when making a selection. As such, this commit removes the `cursor_offset_on_selection` field, as well as any related methods in favor of a new `Editor.vim_mode_enabled` method, which can be used to achieve the same behavior. Relates to zed-industries#42837 Release Notes: - N/A
I believe so. We're planning on making new releases a little bit sooner this week, seeing as next week a lot of folks are going to be out of office 🙂 |
Closes #36677 and #20121
Problem
Vim visual mode and Helix selection mode both require the cursor to be on the last character of the selection. Until now, this was implemented by offsetting the cursor one character to the left whenever a block cursor is used. (Since the visual modes use a block cursor.)
zed/crates/editor/src/element.rs
Line 153 in 1683052
However, this oversees the problem that some users might want to use the block cursor without being in visual mode. Meaning that the cursor is offset by one character to the left even though Vim/Helix mode isn't even activated.
Solution
Since the Vim mode implementation is separate from the
editorcrate the solution is not as straightforward as just checking the current vim mode. Therefore this PR introduces a newEditorstruct field calledcursor_offset_on_selection. This field replaces the condition above and is set totruewhenever the Vim mode is changed to a visual mode, andfalseotherwise.If there is a better solution without having to add a new
Editorfield, let me know.Release Notes: