Skip to content

Commit 0b509ae

Browse files
the-mikedavisShekhinah Memmel
authored andcommitted
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 3840cfd commit 0b509ae

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
@@ -3511,6 +3511,8 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
35113511
let text = doc.text();
35123512
let selection = doc.selection(view.id);
35133513

3514+
let mut ranges = SmallVec::with_capacity(selection.len());
3515+
35143516
let transaction = Transaction::change_by_selection(text, selection, |range| {
35153517
let pos = match (action, linewise) {
35163518
// paste linewise before
@@ -3527,8 +3529,21 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
35273529
// paste at cursor
35283530
(Paste::Cursor, _) => range.cursor(text.slice(..)),
35293531
};
3530-
(pos, pos, values.next())
3532+
3533+
let value = values.next();
3534+
3535+
let value_len = value
3536+
.as_ref()
3537+
.map(|content| content.chars().count())
3538+
.unwrap_or_default();
3539+
3540+
ranges.push(Range::new(pos, pos + value_len));
3541+
3542+
(pos, pos, value)
35313543
});
3544+
3545+
let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3546+
35323547
apply_transaction(&transaction, doc, view);
35333548
}
35343549

@@ -4782,6 +4797,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
47824797
let selection = doc.selection(view.id);
47834798

47844799
let mut changes = Vec::with_capacity(selection.len());
4800+
let mut ranges = SmallVec::with_capacity(selection.len());
47854801
let text = doc.text().slice(..);
47864802

47874803
for range in selection.ranges() {
@@ -4805,11 +4821,13 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) {
48054821
ShellBehavior::Append => (range.to(), range.to()),
48064822
_ => (range.from(), range.from()),
48074823
};
4824+
ranges.push(Range::new(to, to + output.chars().count()));
48084825
changes.push((from, to, Some(output)));
48094826
}
48104827

48114828
if behavior != &ShellBehavior::Ignore {
4812-
let transaction = Transaction::change(doc.text(), changes.into_iter());
4829+
let transaction = Transaction::change(doc.text(), changes.into_iter())
4830+
.with_selection(Selection::new(ranges, selection.primary_index()));
48134831
apply_transaction(&transaction, doc, view);
48144832
doc.append_changes_to_history(view.id);
48154833
}

0 commit comments

Comments
 (0)