Skip to content

Commit 8c3907e

Browse files
the-mikedavisShekhinah Memmel
authored andcommitted
Select new pasted text in normal mode only (helix-editor#4824)
d6323b7 changed the behavior of paste to select the newly inserted text. This is preferrable in normal mode because it's useful to be able to act on the new text. This behavior is worse for insert or select mode though: * In insert mode, the cursor ends up on the last character of the newly selected text, so further typing inserts text before the last character. * In select mode, the current selection is replaced with the new text selection which doesn't extend the current selection. With this change, the selection is extended to include the new text. This aligns the behavior more closely with Kakoune, but it's coincidental instead of intentional: Kakoune doesn't implement bracketed paste (AFAIK) which causes this behavior in insert mode, and Kakoune doesn't have a select mode.
1 parent 06c5c8c commit 8c3907e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

helix-term/src/commands.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,14 @@ enum Paste {
35073507
Cursor,
35083508
}
35093509

3510-
fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Paste, count: usize) {
3510+
fn paste_impl(
3511+
values: &[String],
3512+
doc: &mut Document,
3513+
view: &mut View,
3514+
action: Paste,
3515+
count: usize,
3516+
mode: Mode,
3517+
) {
35113518
if values.is_empty() {
35123519
return;
35133520
}
@@ -3539,7 +3546,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
35393546
let mut offset = 0;
35403547
let mut ranges = SmallVec::with_capacity(selection.len());
35413548

3542-
let transaction = Transaction::change_by_selection(text, selection, |range| {
3549+
let mut transaction = Transaction::change_by_selection(text, selection, |range| {
35433550
let pos = match (action, linewise) {
35443551
// paste linewise before
35453552
(Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())),
@@ -3571,7 +3578,9 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
35713578
(pos, pos, value)
35723579
});
35733580

3574-
let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3581+
if mode == Mode::Normal {
3582+
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3583+
}
35753584

35763585
apply_transaction(&transaction, doc, view);
35773586
}
@@ -3583,7 +3592,7 @@ pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
35833592
Mode::Normal => Paste::Before,
35843593
};
35853594
let (view, doc) = current!(cx.editor);
3586-
paste_impl(&[contents], doc, view, paste, count);
3595+
paste_impl(&[contents], doc, view, paste, count, cx.editor.mode);
35873596
}
35883597

35893598
fn paste_clipboard_impl(
@@ -3595,7 +3604,7 @@ fn paste_clipboard_impl(
35953604
let (view, doc) = current!(editor);
35963605
match editor.clipboard_provider.get_contents(clipboard_type) {
35973606
Ok(contents) => {
3598-
paste_impl(&[contents], doc, view, action, count);
3607+
paste_impl(&[contents], doc, view, action, count, editor.mode);
35993608
Ok(())
36003609
}
36013610
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
@@ -3714,7 +3723,7 @@ fn paste(cx: &mut Context, pos: Paste) {
37143723
let registers = &mut cx.editor.registers;
37153724

37163725
if let Some(values) = registers.read(reg_name) {
3717-
paste_impl(values, doc, view, pos, count);
3726+
paste_impl(values, doc, view, pos, count, cx.editor.mode);
37183727
}
37193728
}
37203729

0 commit comments

Comments
 (0)