on master help sign in
Fix range offsets in multi-selection paste (#4608)
48a3965Michael Davis
helix-term/src/commands.rs+5−1
@@ -3476,6 +3476,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
3476 let text = doc.text();
3476 let text = doc.text();
3477 let selection = doc.selection(view.id);
3477 let selection = doc.selection(view.id);
3478
3478
3479+ let mut offset = 0;
3479 let mut ranges = SmallVec::with_capacity(selection.len());
3480 let mut ranges = SmallVec::with_capacity(selection.len());
3480
3481
3481 let transaction = Transaction::change_by_selection(text, selection, |range| {
3482 let transaction = Transaction::change_by_selection(text, selection, |range| {
@@ -3501,8 +3502,11 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
3501 .as_ref()
3502 .as_ref()
3502 .map(|content| content.chars().count())
3503 .map(|content| content.chars().count())
3503 .unwrap_or_default();
3504 .unwrap_or_default();
3505+ let anchor = offset + pos;
3504
3506
3505− ranges.push(Range::new(pos, pos + value_len));
3507+ let new_range = Range::new(anchor, anchor + value_len).with_direction(range.direction());
3508+ ranges.push(new_range);
3509+ offset += value_len;
3506
3510
3507 (pos, pos, value)
3511 (pos, pos, value)
3508 });
3512 });
helix-term/tests/test/commands.rs+22
@@ -193,3 +193,25 @@ async fn test_goto_file_impl() -> anyhow::Result<()> {
193
193
194 Ok(())
194 Ok(())
195}
195}
196+
197+#[tokio::test(flavor = "multi_thread")]
198+async fn test_multi_selection_paste() -> anyhow::Result<()> {
199+ test((
200+ platform_line(indoc! {"\
201+ #[|lorem]#
202+ #(|ipsum)#
203+ #(|dolor)#
204+ "})
205+ .as_str(),
206+ "yp",
207+ platform_line(indoc! {"\
208+ lorem#[|lorem]#
209+ ipsum#(|ipsum)#
210+ dolor#(|dolor)#
211+ "})
212+ .as_str(),
213+ ))
214+ .await?;
215+
216+ Ok(())
217+}