on master help sign in
Select text inserted by shell or paste (#4458)
d6323b7Michael Davis
helix-term/src/commands.rs+20−2
@@ -3471,6 +3471,8 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
3471 let text = doc.text();
3471 let text = doc.text();
3472 let selection = doc.selection(view.id);
3472 let selection = doc.selection(view.id);
3473
3473
3474+ let mut ranges = SmallVec::with_capacity(selection.len());
3475+
3474 let transaction = Transaction::change_by_selection(text, selection, |range| {
3476 let transaction = Transaction::change_by_selection(text, selection, |range| {
3475 let pos = match (action, linewise) {
3477 let pos = match (action, linewise) {
3476 // paste linewise before
3478 // paste linewise before
@@ -3487,8 +3489,21 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
3487 // paste at cursor
3489 // paste at cursor
3488 (Paste::Cursor, _) => range.cursor(text.slice(..)),
3490 (Paste::Cursor, _) => range.cursor(text.slice(..)),
3489 };
3491 };
3492+
3490− (pos, pos, values.next())
3493+ let value = values.next();
3494+
3495+ let value_len = value
3496+ .as_ref()
3497+ .map(|content| content.chars().count())
3498+ .unwrap_or_default();
3499+
3500+ ranges.push(Range::new(pos, pos + value_len));
3501+
3502+ (pos, pos, value)
3491 });
3503 });
3504+
3505+ let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3506+
3492 apply_transaction(&transaction, doc, view);
3507 apply_transaction(&transaction, doc, view);
3493}
3508}
3494
3509
@@ -4742,6 +4757,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
4742 let selection = doc.selection(view.id);
4757 let selection = doc.selection(view.id);
4743
4758
4744 let mut changes = Vec::with_capacity(selection.len());
4759 let mut changes = Vec::with_capacity(selection.len());
4760+ let mut ranges = SmallVec::with_capacity(selection.len());
4745 let text = doc.text().slice(..);
4761 let text = doc.text().slice(..);
4746
4762
4747 for range in selection.ranges() {
4763 for range in selection.ranges() {
@@ -4765,11 +4781,13 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
4765 ShellBehavior::Append => (range.to(), range.to()),
4781 ShellBehavior::Append => (range.to(), range.to()),
4766 _ => (range.from(), range.from()),
4782 _ => (range.from(), range.from()),
4767 };
4783 };
4784+ ranges.push(Range::new(to, to + output.chars().count()));
4768 changes.push((from, to, Some(output)));
4785 changes.push((from, to, Some(output)));
4769 }
4786 }
4770
4787
4771 if behavior != &ShellBehavior::Ignore {
4788 if behavior != &ShellBehavior::Ignore {
4772− let transaction = Transaction::change(doc.text(), changes.into_iter());
4789+ let transaction = Transaction::change(doc.text(), changes.into_iter())
4790+ .with_selection(Selection::new(ranges, selection.primary_index()));
4773 apply_transaction(&transaction, doc, view);
4791 apply_transaction(&transaction, doc, view);
4774 doc.append_changes_to_history(view.id);
4792 doc.append_changes_to_history(view.id);
4775 }
4793 }