diff options
author | Michael Davis | 2022-11-04 16:02:19 +0000 |
---|---|---|
committer | GitHub | 2022-11-04 16:02:19 +0000 |
commit | d6323b7cbc21a9d3ba29738c76581dad93f9f415 (patch) | |
tree | 323a8c1015f8027364403d97bbf44ebab95ff8f2 /helix-term/src | |
parent | 9a898be95934bec46c5a77c61d77314ecb4d71fc (diff) |
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.
Diffstat (limited to 'helix-term/src')
-rw-r--r-- | helix-term/src/commands.rs | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index aae9979d..a6ee6263 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3471,6 +3471,8 @@ 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 ranges = SmallVec::with_capacity(selection.len()); + let transaction = Transaction::change_by_selection(text, selection, |range| { let pos = match (action, linewise) { // paste linewise before @@ -3487,8 +3489,21 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa // paste at cursor (Paste::Cursor, _) => range.cursor(text.slice(..)), }; - (pos, pos, values.next()) + + let value = values.next(); + + let value_len = value + .as_ref() + .map(|content| content.chars().count()) + .unwrap_or_default(); + + ranges.push(Range::new(pos, pos + value_len)); + + (pos, pos, value) }); + + let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index())); + apply_transaction(&transaction, doc, view); } @@ -4742,6 +4757,7 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { let selection = doc.selection(view.id); let mut changes = Vec::with_capacity(selection.len()); + let mut ranges = SmallVec::with_capacity(selection.len()); let text = doc.text().slice(..); for range in selection.ranges() { @@ -4765,11 +4781,13 @@ fn shell(cx: &mut compositor::Context, cmd: &str, behavior: &ShellBehavior) { ShellBehavior::Append => (range.to(), range.to()), _ => (range.from(), range.from()), }; + ranges.push(Range::new(to, to + output.chars().count())); changes.push((from, to, Some(output))); } if behavior != &ShellBehavior::Ignore { - let transaction = Transaction::change(doc.text(), changes.into_iter()); + let transaction = Transaction::change(doc.text(), changes.into_iter()) + .with_selection(Selection::new(ranges, selection.primary_index())); apply_transaction(&transaction, doc, view); doc.append_changes_to_history(view.id); } |