Skip to content

Commit d6323b7

Browse files
Select text inserted by shell or paste (helix-editor#4458)
This follows changes in Kakoune to the same effects: * p/<space>p: mawww/kakoune@266d1c3 * !/<A-!>: mawww/kakoune@85b78dd Selecting the new data inserted by shell or pasting is often more useful than retaining a selection of the pre-paste/insert content.
1 parent 9a898be commit d6323b7

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

helix-term/src/commands.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)