diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 305cc31e719d..36e8acf6ae54 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3516,6 +3516,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa let text = doc.text(); let selection = doc.selection(view.id); + let mut offset = 0; let mut ranges = SmallVec::with_capacity(selection.len()); let transaction = Transaction::change_by_selection(text, selection, |range| { @@ -3541,8 +3542,11 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa .as_ref() .map(|content| content.chars().count()) .unwrap_or_default(); + let anchor = offset + pos; - ranges.push(Range::new(pos, pos + value_len)); + let new_range = Range::new(anchor, anchor + value_len).with_direction(range.direction()); + ranges.push(new_range); + offset += value_len; (pos, pos, value) }); diff --git a/helix-term/tests/test/commands.rs b/helix-term/tests/test/commands.rs index aadf104bb427..e78e6c9f9964 100644 --- a/helix-term/tests/test/commands.rs +++ b/helix-term/tests/test/commands.rs @@ -193,3 +193,25 @@ async fn test_goto_file_impl() -> anyhow::Result<()> { Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +async fn test_multi_selection_paste() -> anyhow::Result<()> { + test(( + platform_line(indoc! {"\ + #[|lorem]# + #(|ipsum)# + #(|dolor)# + "}) + .as_str(), + "yp", + platform_line(indoc! {"\ + lorem#[|lorem]# + ipsum#(|ipsum)# + dolor#(|dolor)# + "}) + .as_str(), + )) + .await?; + + Ok(()) +}