From e2395739032dc028ed2c8b801f9072ca2befd618 Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Mon, 17 Oct 2022 01:27:22 -0400 Subject: [PATCH] Shift Arrow Selection Fixed issues where shift + arrow keys would not select text. This creates an issue with cursor movements after using shift + arrow keys. Further cursor movements retain the selection mode and allow selecting. Cursor movements should clear the active selection. However, this would invalidate the ability for selecting via marking selection (v). Selection marking will be refactored, which will fix this inconsistency. --- abacus-ui/src/data/editor_data.rs | 40 +++++++++++-------------------- abacus-ui/src/editor.rs | 1 - 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/abacus-ui/src/data/editor_data.rs b/abacus-ui/src/data/editor_data.rs index 09a1e53..eb69629 100644 --- a/abacus-ui/src/data/editor_data.rs +++ b/abacus-ui/src/data/editor_data.rs @@ -201,15 +201,8 @@ impl EditorData { } pub fn select_up(&mut self) { - let line_idx = self.content.char_to_line(self.cursor_pos); - - if line_idx > 0 { - let start_of_current_line = self.content.line_to_char(line_idx); - let line_pos = self.cursor_pos - start_of_current_line; - let up_line_start = self.content.line_to_char(line_idx - 1); - let up_line = self.content.line(line_idx - 1); - self.cursor_pos = up_line_start + line_pos.min(up_line.len_chars() - 1); - } + self.start_selection(); + self.cursor_up(); } pub fn cursor_down(&mut self) { @@ -225,17 +218,8 @@ impl EditorData { } pub fn select_down(&mut self) { - //12 -> 11 - let line_idx = self.content.char_to_line(self.cursor_pos); - if line_idx < self.content.len_lines() - 1 { - let start_of_current_line = self.content.line_to_char(line_idx); - let line_pos = self.cursor_pos - start_of_current_line; - let start_of_next_line = self.content.line_to_char(line_idx + 1) + 1; - let next_line_len = self.content.line(line_idx + 1).len_chars(); - - self.cursor_pos = - (start_of_next_line + line_pos.min(next_line_len)).min(start_of_next_line); - } + self.start_selection(); + self.cursor_down(); } pub fn cursor_right(&mut self) { @@ -287,15 +271,13 @@ impl EditorData { } pub fn select_left(&mut self) { - if self.cursor_pos > 0 { - self.cursor_pos -= 1 - } + self.start_selection(); + self.cursor_left(); } pub fn select_right(&mut self) { - if self.cursor_pos < self.content.len_chars() { - self.cursor_pos += 1; - } + self.start_selection(); + self.cursor_right(); } pub fn current_line(&self) -> ropey::RopeSlice { @@ -310,6 +292,12 @@ impl EditorData { .unwrap() } + pub fn start_selection(&mut self) { + if self.selection_pos.is_none() { + self.selection_pos = Some(self.cursor_pos); + } + } + pub fn current_line_index(&self) -> usize { self.content.char_to_line(self.cursor_pos) } diff --git a/abacus-ui/src/editor.rs b/abacus-ui/src/editor.rs index 5be335d..182c381 100644 --- a/abacus-ui/src/editor.rs +++ b/abacus-ui/src/editor.rs @@ -414,7 +414,6 @@ impl Widget for AbacusEditor { druid::keyboard_types::Key::ArrowUp if e.mods.shift() => { data.select_up(); - data.deselect(); } druid::keyboard_types::Key::ArrowDown if e.mods.shift() => { data.select_down();