From 8926787bd1fca487c3d26a054a3da6d5116aa181 Mon Sep 17 00:00:00 2001 From: Tastaturtaste Date: Thu, 28 Dec 2023 22:09:54 +0100 Subject: [PATCH] Add SelectMoveWord command on Shift + Ctrl + Arrow --- src/core_editor/editor.rs | 22 +++++++++++++++++++++- src/edit_mode/keybindings.rs | 10 ++++++++++ src/enums.rs | 10 ++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/core_editor/editor.rs b/src/core_editor/editor.rs index 3075dcfb..a34366bf 100644 --- a/src/core_editor/editor.rs +++ b/src/core_editor/editor.rs @@ -102,6 +102,8 @@ impl Editor { EditCommand::SelectAll => self.select_all(), EditCommand::CutSelection => self.cut_selection(), EditCommand::CopySelection => self.copy_selection(), + EditCommand::SelectMoveWordLeft => self.select_move_word_left(), + EditCommand::SelectMoveWordRight => self.select_move_word_right(), } let new_undo_behavior = match (command, command.edit_type()) { @@ -120,7 +122,11 @@ impl Editor { }; if !matches!( command, - EditCommand::SelectMoveLeft | EditCommand::SelectMoveRight | EditCommand::SelectAll + EditCommand::SelectMoveLeft + | EditCommand::SelectMoveRight + | EditCommand::SelectMoveWordLeft + | EditCommand::SelectMoveWordRight + | EditCommand::SelectAll ) { self.reset_selection(); } @@ -545,6 +551,20 @@ impl Editor { } }) } + + fn select_move_word_left(&mut self) { + if self.selection_anchor.is_none() { + self.selection_anchor = Some(self.insertion_point()); + } + self.line_buffer.move_word_left(); + } + + fn select_move_word_right(&mut self) { + if self.selection_anchor.is_none() { + self.selection_anchor = Some(self.insertion_point()); + } + self.line_buffer.move_word_right(); + } } #[cfg(test)] diff --git a/src/edit_mode/keybindings.rs b/src/edit_mode/keybindings.rs index 7cd3bae1..99559de2 100644 --- a/src/edit_mode/keybindings.rs +++ b/src/edit_mode/keybindings.rs @@ -210,5 +210,15 @@ pub fn add_common_selection_bindings(kb: &mut Keybindings) { kb.add_binding(KM::SHIFT, KC::Left, edit_bind(EC::SelectMoveLeft)); kb.add_binding(KM::SHIFT, KC::Right, edit_bind(EC::SelectMoveRight)); + kb.add_binding( + KM::SHIFT | KM::CONTROL, + KC::Left, + edit_bind(EC::SelectMoveWordLeft), + ); + kb.add_binding( + KM::SHIFT | KM::CONTROL, + KC::Right, + edit_bind(EC::SelectMoveWordRight), + ); kb.add_binding(KM::CONTROL, KC::Char('a'), edit_bind(EC::SelectAll)); } diff --git a/src/enums.rs b/src/enums.rs index 188cea39..6c90d85e 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -196,6 +196,12 @@ pub enum EditCommand { /// Select and move right SelectMoveRight, + /// Select and move whole word left + SelectMoveWordLeft, + + /// Select and move whole word right + SelectMoveWordRight, + /// Select whole input buffer SelectAll, @@ -270,6 +276,8 @@ impl Display for EditCommand { EditCommand::SelectAll => write!(f, "SelectAll"), EditCommand::CutSelection => write!(f, "CutSelection"), EditCommand::CopySelection => write!(f, "CopySelection"), + EditCommand::SelectMoveWordLeft => write!(f, "SelectMoveWordLeft"), + EditCommand::SelectMoveWordRight => write!(f, "SelectMoveWordRight"), } } } @@ -300,6 +308,8 @@ impl EditCommand { | EditCommand::MoveLeftBefore(_) | EditCommand::SelectMoveLeft | EditCommand::SelectMoveRight + | EditCommand::SelectMoveWordLeft + | EditCommand::SelectMoveWordRight | EditCommand::SelectAll => EditType::MoveCursor, // Text edits