Skip to content

Commit

Permalink
Select new pasted text in normal mode only (helix-editor#4824)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
the-mikedavis authored and Frederik Vestre committed Feb 6, 2023
1 parent 03d8f0c commit 2d9e3a9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3467,7 +3467,14 @@ enum Paste {
Cursor,
}

fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Paste, count: usize) {
fn paste_impl(
values: &[String],
doc: &mut Document,
view: &mut View,
action: Paste,
count: usize,
mode: Mode,
) {
if values.is_empty() {
return;
}
Expand Down Expand Up @@ -3499,7 +3506,7 @@ fn paste_impl(values: &[String], doc: &mut Document, view: &mut View, action: Pa
let mut offset = 0;
let mut ranges = SmallVec::with_capacity(selection.len());

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

let transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
if mode == Mode::Normal {
transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
}

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

fn paste_clipboard_impl(
Expand All @@ -3555,7 +3564,7 @@ fn paste_clipboard_impl(
let (view, doc) = current!(editor);
match editor.clipboard_provider.get_contents(clipboard_type) {
Ok(contents) => {
paste_impl(&[contents], doc, view, action, count);
paste_impl(&[contents], doc, view, action, count, editor.mode);
Ok(())
}
Err(e) => Err(e.context("Couldn't get system clipboard contents")),
Expand Down Expand Up @@ -3674,7 +3683,7 @@ fn paste(cx: &mut Context, pos: Paste) {
let registers = &mut cx.editor.registers;

if let Some(values) = registers.read(reg_name) {
paste_impl(values, doc, view, pos, count);
paste_impl(values, doc, view, pos, count, cx.editor.mode);
}
}

Expand Down

0 comments on commit 2d9e3a9

Please sign in to comment.