From 6235716beb5d94a45c42a38eb5b5bc27c6c6e961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 16 Sep 2023 15:57:41 +0200 Subject: [PATCH 1/5] Make `Edit::copy_selection` immutable --- src/edit/editor.rs | 2 +- src/edit/mod.rs | 2 +- src/edit/syntect.rs | 2 +- src/edit/vi.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 2374ab8..4277ae1 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -109,7 +109,7 @@ impl Edit for Editor { } } - fn copy_selection(&mut self) -> Option { + fn copy_selection(&self) -> Option { let select = self.select_opt?; let (start, end) = match select.line.cmp(&self.cursor.line) { diff --git a/src/edit/mod.rs b/src/edit/mod.rs index 77e07e3..1c16746 100644 --- a/src/edit/mod.rs +++ b/src/edit/mod.rs @@ -115,7 +115,7 @@ pub trait Edit { fn shape_as_needed(&mut self, font_system: &mut FontSystem); /// Copy selection - fn copy_selection(&mut self) -> Option; + fn copy_selection(&self) -> Option; /// Delete selection, adjusting cursor and returning true if there was a selection // Also used by backspace, delete, insert, and enter when there is a selection diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 62e9352..8eda391 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -250,7 +250,7 @@ impl<'a> Edit for SyntaxEditor<'a> { self.editor.shape_as_needed(font_system); } - fn copy_selection(&mut self) -> Option { + fn copy_selection(&self) -> Option { self.editor.copy_selection() } diff --git a/src/edit/vi.rs b/src/edit/vi.rs index b0309aa..4d5fd89 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -81,7 +81,7 @@ impl<'a> Edit for ViEditor<'a> { self.editor.shape_as_needed(font_system); } - fn copy_selection(&mut self) -> Option { + fn copy_selection(&self) -> Option { self.editor.copy_selection() } From 797f1c7941566f7fafd928121b742b679be6d764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 16 Sep 2023 15:59:26 +0200 Subject: [PATCH 2/5] Set `cursor_moved` to `true` in `Editor::insert_string` --- src/edit/editor.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 4277ae1..f19f41b 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -288,6 +288,7 @@ impl Edit for Editor { // Append the text after insertion self.cursor.index = self.buffer.lines[self.cursor.line].text().len() - after_len; + self.cursor_moved = true; } fn action(&mut self, font_system: &mut FontSystem, action: Action) { From 4aacd436c3311f304d20f77efa32ebba1a77c94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 16 Sep 2023 16:07:26 +0200 Subject: [PATCH 3/5] Fix `NextWord` action in `Editor` when line ends with word boundaries --- src/edit/editor.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index f19f41b..9e36d30 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -613,14 +613,14 @@ impl Edit for Editor { Action::NextWord => { let line: &mut BufferLine = &mut self.buffer.lines[self.cursor.line]; if self.cursor.index < line.text().len() { - for (i, word) in line.text().unicode_word_indices() { - let i = i + word.len(); - if i > self.cursor.index { - self.cursor.index = i; - self.buffer.set_redraw(true); - break; - } - } + self.cursor.index = line + .text() + .unicode_word_indices() + .map(|(i, word)| i + word.len()) + .find(|&i| i > self.cursor.index) + .unwrap_or(line.text().len()); + + self.buffer.set_redraw(true); } else if self.cursor.line + 1 < self.buffer.lines.len() { self.cursor.line += 1; self.cursor.index = 0; From cb83458e7d0b84ef37c5beb72dda5046d7d343a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 16 Sep 2023 16:12:19 +0200 Subject: [PATCH 4/5] Rewrite `PreviousWord` logic in `Editor` with iterators --- src/edit/editor.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 9e36d30..31ebf31 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -592,16 +592,14 @@ impl Edit for Editor { Action::PreviousWord => { let line: &mut BufferLine = &mut self.buffer.lines[self.cursor.line]; if self.cursor.index > 0 { - let mut prev_index = 0; - for (i, _) in line.text().unicode_word_indices() { - if i < self.cursor.index { - prev_index = i; - } else { - break; - } - } + self.cursor.index = line + .text() + .unicode_word_indices() + .rev() + .map(|(i, _)| i) + .find(|&i| i < self.cursor.index) + .unwrap_or(0); - self.cursor.index = prev_index; self.buffer.set_redraw(true); } else if self.cursor.line > 0 { self.cursor.line -= 1; From 0bb02227debf79602027ffde870fc61762f887c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sun, 17 Sep 2023 22:35:41 +0200 Subject: [PATCH 5/5] Use attributes at cursor position for insertions in `Editor` --- src/edit/editor.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 31ebf31..274e05b 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -235,8 +235,12 @@ impl Edit for Editor { let after_len = after.text().len(); // Collect attributes - let mut final_attrs = attrs_list - .unwrap_or_else(|| AttrsList::new(line.attrs_list().get_span(line.text().len()))); + let mut final_attrs = attrs_list.unwrap_or_else(|| { + AttrsList::new( + line.attrs_list() + .get_span(self.cursor.index.saturating_sub(1)), + ) + }); // Append the inserted text, line by line // we want to see a blank entry if the string ends with a newline