From 3009ad3f3a0fa58e0152ebde8b3ec6dfecb9f402 Mon Sep 17 00:00:00 2001 From: usagi-flow <2804556+usagi-flow@users.noreply.github.com> Date: Sun, 11 Feb 2024 15:44:20 +0100 Subject: [PATCH] feat(commands): ensure the last character remains selected when pressing 'b' in select mode --- helix-term/src/commands.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 153f8d34d..975517dd5 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5622,12 +5622,16 @@ where let text = doc.text().slice(..); let selection = doc.selection(view.id).clone().transform(|range| { + let old_head = range.head; let old_anchor = range.anchor; let mut new_range = move_fn(text, range, count); new_range.anchor = match cx.editor.mode { - // In select mode, use a sticky anchor and move the head only - Mode::Select => old_anchor, + // In select mode, use a sticky anchor and move the head only; + // keeping in mind that in select mode, with a single char selected, + // the anchor typically points *before* the character. + Mode::Select if new_range.head < old_head => old_head.max(old_anchor), + Mode::Select => old_head.min(old_anchor), // When not in select mode, just move the cursor and do not select _ => new_range.head, };