editor: Fix clipboard selection range for multi-line copy-and-trim#48977
editor: Fix clipboard selection range for multi-line copy-and-trim#48977
Conversation
When copying multiple selections with copy-and-trim, create a single clipboard selection spanning the original buffer range rather than one selection per trimmed line. This preserves correct paste behavior in vim mode when pasting trimmed content.
dinocosta
left a comment
There was a problem hiding this comment.
Thank you tackling this one @yeskunall ! 🙌
There's a couple of points I'd like to explore before merging this:
- Removing the test from the
vimcrate, as the fix is actually on theeditorside of things, so we can easily assert that a singleClipboardSelectionis created - Refactoring a bit the
do_copymethod as it's already quite big and these changes add more complexity to it
Let me know if you want me to take a quick look at these or if you'd like to pair 🙂
Co-authored-by: Kunall Banerjee <hey@kimchiii.space>
* Avoid multiple `clipboard_selections.push`, moving it to out of the
inner loop.
* Remove unnecessary block `{ ... }` before iterating over selections,
reducing the indentation level.
* Assign to `prev_selection_was_entire_line` once as the value never
changed inside the inner loop.
Co-authored-by: Kunall Banerjee <hey@kimchiii.space>
|
@yeskunall I just noticed that it seems the behavior reported in #48869 has changed in Nightly, it's still not working as it should but it's now preserving the newlines, so I do wonder if #49134 also affected this behavior ▼ CleanShot.2026-02-23.at.17.44.22.mp4Will take a closer look tomorrow to ensure we actually ship the correct behavior to Preview on Wednesday ✅ |
|
@dinocosta interesting -- if I get the chance today I’ll take a look as well. Ping me tomorrow again if you think we need to pair! |
In `main`, the `editor: copy and trim` behavior is currently broken when
pasting with vim's `p`, as the last character is ignored. For example,
when copying the `values` field from this JSON object:
```
{
data: {
values: {
high: 1,
mid: 0.5,
low: 0
}
}
}
```
The pasted contents, using `editor: copy and trim`, would end up being:
```
values: {
high: 1,
mid: 0.5,
low: 0
```
Where the closing `}` is not pasted. The changes in this branch fix this
but we were missing a test to actually prevent a regression.
Move the test that was introduced in vim's crated, namely to assert that copy and trim, then pasting, would work as expected, to a test in the editor crate since the fix was done in that crate and we can assert the fix by ensuring that a multiline selection ends up creating a single `ClipboardSelection`.
@yeskunall I've confirmed that the changes in this branch fix the issue with Also pushed two commits, one adding a test case for catching a regression that bug, as well as moving the test that was added in this PR to the |
|
Thanks @dinocosta! Really appreciate you seeing this all the way through. |
…ed-industries#48977) When copying multiple selections with copy-and-trim, create a single clipboard selection spanning the original buffer range rather than one selection per trimmed line. This preserves correct paste behavior in Vim mode when pasting trimmed content. Closes zed-industries#48869. Release Notes: - Fixed clipboard selection range for multi-line copy-and-trim --------- Co-authored-by: dino <dinojoaocosta@gmail.com>
When copying multiple selections with copy-and-trim, create a single clipboard selection spanning the original buffer range rather than one selection per trimmed line. This preserves correct paste behavior in Vim mode when pasting trimmed content.
Closes #48869.
Release Notes: