Editor Fixes #5

Merged
joe merged 7 commits from editor-fixes into main 2022-10-20 01:52:59 +00:00
2 changed files with 14 additions and 27 deletions
Showing only changes of commit e239573903 - Show all commits

View File

@ -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)
}

View File

@ -414,7 +414,6 @@ impl Widget<EditorData> 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();