Skip to content

Commit 2d9e3a9

Browse files
the-mikedavisFrederik Vestre
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 03d8f0c commit 2d9e3a9

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
@@ -3467,7 +3467,14 @@ enum Paste {
34673467
Cursor,
34683468
}
34693469

3470-
fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Paste, count: usize) {
3470+
fn paste_impl(
3471+
values: &[String],
3472+
doc: &mut Document,
3473+
view: &mut View,
3474+
action: Paste,
3475+
count: usize,
3476+
mode: Mode,
3477+
) {
34713478
if values.is_empty() {
34723479
return;
34733480
}
@@ -3499,7 +3506,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
34993506
let mut offset = 0;
35003507
let mut ranges = SmallVec::with_capacity(selection.len());
35013508

3502-
let transaction = Transaction::change_by_selection(text, selection, |range| {
3509+
let mut transaction = Transaction::change_by_selection(text, selection, |range| {
35033510
let pos = match (action, linewise) {
35043511
// paste linewise before
35053512
(Paste::Before, true) => text.line_to_char(text.char_to_line(range.from())),
@@ -3531,7 +3538,9 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
35313538
(pos, pos, value)
35323539
});
35333540

3534-
let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3541+
if mode == Mode::Normal {
3542+
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3543+
}
35353544

35363545
apply_transaction(&transaction, doc, view);
35373546
}
@@ -3543,7 +3552,7 @@ pub(crate) fn paste_bracketed_value(cx: &mut Context, contents: String) {
35433552
Mode::Normal => Paste::Before,
35443553
};
35453554
let (view, doc) = current!(cx.editor);
3546-
paste_impl(&[contents], doc, view, paste, count);
3555+
paste_impl(&[contents], doc, view, paste, count, cx.editor.mode);
35473556
}
35483557

35493558
fn paste_clipboard_impl(
@@ -3555,7 +3564,7 @@ fn paste_clipboard_impl(
35553564
let (view, doc) = current!(editor);
35563565
match editor.clipboard_provider.get_contents(clipboard_type) {
35573566
Ok(contents) => {
3558-
paste_impl(&[contents], doc, view, action, count);
3567+
paste_impl(&[contents], doc, view, action, count, editor.mode);
35593568
Ok(())
35603569
}
35613570
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
@@ -3674,7 +3683,7 @@ fn paste(cx: &mut Context, pos: Paste) {
36743683
let registers = &mut cx.editor.registers;
36753684

36763685
if let Some(values) = registers.read(reg_name) {
3677-
paste_impl(values, doc, view, pos, count);
3686+
paste_impl(values, doc, view, pos, count, cx.editor.mode);
36783687
}
36793688
}
36803689

0 commit comments

Comments
 (0)