on master help sign in
Select text inserted by shell or paste (#4458)

This follows changes in Kakoune to the same effects: * p/<space>p: https://github.com/mawww/kakoune/commit/266d1c37d0d970a7eff747f5e6a5773a3cea39d8 * !/<A-!>: https://github.com/mawww/kakoune/commit/85b78dda2e29d70b620836b04224b104426bdbae Selecting the new data inserted by shell or pasting is often more useful than retaining a selection of the pre-paste/insert content.

commitd6323b7cbc21a9d3ba29738c76581dad93f9f415 authorMichael Davis <mcarsondavis@gmail.com> dateFri 4 Nov 2022, 11:02 (4 years ago) parent9a898be
1 file changed+20−2
helix-term/src/commands.rs+20−2
helix-term/src/commands.rs+20−2
@@ -3471,6 +3471,8 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
34713471 let text = doc.text();
34723472 let selection = doc.selection(view.id);
34733473
3474+ let mut ranges = SmallVec::with_capacity(selection.len());
3475+
34743476 let transaction = Transaction::change_by_selection(text, selection, |range| {
34753477 let pos = match (action, linewise) {
34763478 // paste linewise before
@@ -3487,8 +3489,21 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
34873489 // paste at cursor
34883490 (Paste::Cursor, _) => range.cursor(text.slice(..)),
34893491 };
3490− (pos, pos, values.next())
3492+
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)
34913503 });
3504+
3505+ let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3506+
34923507 apply_transaction(&transaction, doc, view);
34933508}
34943509
@@ -4742,6 +4757,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
47424757 let selection = doc.selection(view.id);
47434758
47444759 let mut changes = Vec::with_capacity(selection.len());
4760+ let mut ranges = SmallVec::with_capacity(selection.len());
47454761 let text = doc.text().slice(..);
47464762
47474763 for range in selection.ranges() {
@@ -4765,11 +4781,13 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
47654781 ShellBehavior::Append => (range.to(), range.to()),
47664782 _ => (range.from(), range.from()),
47674783 };
4784+ ranges.push(Range::new(to, to + output.chars().count()));
47684785 changes.push((from, to, Some(output)));
47694786 }
47704787
47714788 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()));
47734791 apply_transaction(&transaction, doc, view);
47744792 doc.append_changes_to_history(view.id);
47754793 }