From 746f4b1daf1a3ae7778373523dc154d1b232c169 Mon Sep 17 00:00:00 2001 From: Waleed Dahshan <58462210+wmstack@users.noreply.github.com> Date: Tue, 30 Jan 2024 08:58:33 +1100 Subject: [PATCH] Use range positions to determine insert_newline motion (#9448) * use anchor and head positions to determine motion * use range cursor to decide extending or shifting * add condition to cursor moving back on normal --- helix-term/src/commands.rs | 2 +- helix-view/src/editor.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 912a7f1a94f96..278c7e3f50ebe 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3664,7 +3664,7 @@ pub mod insert { (pos, pos, local_offs) }; - let new_range = if doc.restore_cursor { + let new_range = if range.cursor(text) > range.anchor { // when appending, extend the range by local_offs Range::new( range.anchor + global_offs, diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index deaad79167bc2..2f83e32b12fe4 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1993,10 +1993,12 @@ impl Editor { if doc.restore_cursor { let text = doc.text().slice(..); let selection = doc.selection(view.id).clone().transform(|range| { - Range::new( - range.from(), - graphemes::prev_grapheme_boundary(text, range.to()), - ) + let mut head = range.to(); + if range.head > range.anchor { + head = graphemes::prev_grapheme_boundary(text, head); + } + + Range::new(range.from(), head) }); doc.set_selection(view.id, selection);