diff --git a/src/edit/editor.rs b/src/edit/editor.rs index cd01fa8..fd468c3 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -73,6 +73,72 @@ impl Editor { self.buffer.set_redraw(true); } } +} + +impl Edit for Editor { + fn buffer(&self) -> &Buffer { + &self.buffer + } + + fn buffer_mut(&mut self) -> &mut Buffer { + &mut self.buffer + } + + fn cursor(&self) -> Cursor { + self.cursor + } + + fn set_cursor(&mut self, cursor: Cursor) { + if self.cursor != cursor { + self.cursor = cursor; + self.cursor_moved = true; + self.cursor_x_opt = None; + self.buffer.set_redraw(true); + } + } + + fn selection(&self) -> Selection { + self.selection + } + + fn set_selection(&mut self, selection: Selection) { + if self.selection != selection { + self.selection = selection; + self.buffer.set_redraw(true); + } + } + + fn auto_indent(&self) -> bool { + self.auto_indent + } + + fn set_auto_indent(&mut self, auto_indent: bool) { + self.auto_indent = auto_indent; + } + + fn tab_width(&self) -> u16 { + self.tab_width + } + + fn set_tab_width(&mut self, tab_width: u16) { + // A tab width of 0 is not allowed + if tab_width == 0 { + return; + } + if self.tab_width != tab_width { + self.tab_width = tab_width; + self.buffer.set_redraw(true); + } + } + + fn shape_as_needed(&mut self, font_system: &mut FontSystem) { + if self.cursor_moved { + self.buffer.shape_until_cursor(font_system, self.cursor); + self.cursor_moved = false; + } else { + self.buffer.shape_until_scroll(font_system); + } + } fn delete_range(&mut self, start: Cursor, end: Cursor) { // Collect removed data for change tracking @@ -222,72 +288,6 @@ impl Editor { cursor } -} - -impl Edit for Editor { - fn buffer(&self) -> &Buffer { - &self.buffer - } - - fn buffer_mut(&mut self) -> &mut Buffer { - &mut self.buffer - } - - fn cursor(&self) -> Cursor { - self.cursor - } - - fn set_cursor(&mut self, cursor: Cursor) { - if self.cursor != cursor { - self.cursor = cursor; - self.cursor_moved = true; - self.cursor_x_opt = None; - self.buffer.set_redraw(true); - } - } - - fn selection(&self) -> Selection { - self.selection - } - - fn set_selection(&mut self, selection: Selection) { - if self.selection != selection { - self.selection = selection; - self.buffer.set_redraw(true); - } - } - - fn auto_indent(&self) -> bool { - self.auto_indent - } - - fn set_auto_indent(&mut self, auto_indent: bool) { - self.auto_indent = auto_indent; - } - - fn tab_width(&self) -> u16 { - self.tab_width - } - - fn set_tab_width(&mut self, tab_width: u16) { - // A tab width of 0 is not allowed - if tab_width == 0 { - return; - } - if self.tab_width != tab_width { - self.tab_width = tab_width; - self.buffer.set_redraw(true); - } - } - - fn shape_as_needed(&mut self, font_system: &mut FontSystem) { - if self.cursor_moved { - self.buffer.shape_until_cursor(font_system, self.cursor); - self.cursor_moved = false; - } else { - self.buffer.shape_until_scroll(font_system); - } - } fn copy_selection(&self) -> Option { let (start, end) = self.selection_bounds()?; @@ -337,12 +337,6 @@ impl Edit for Editor { true } - fn insert_string(&mut self, data: &str, attrs_list: Option) { - self.delete_selection(); - let new_cursor = self.insert_at(self.cursor, data, attrs_list); - self.set_cursor(new_cursor); - } - fn apply_change(&mut self, change: &Change) -> bool { // Cannot apply changes if there is a pending change match self.change.take() { diff --git a/src/edit/mod.rs b/src/edit/mod.rs index 10b9502..89353cd 100644 --- a/src/edit/mod.rs +++ b/src/edit/mod.rs @@ -220,6 +220,12 @@ pub trait Edit { /// Shape lines until scroll, after adjusting scroll if the cursor moved fn shape_as_needed(&mut self, font_system: &mut FontSystem); + /// Delete text starting at start Cursor and ending at end Cursor + fn delete_range(&mut self, start: Cursor, end: Cursor); + + /// Insert text at specified cursor with specified attrs_list + fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option) -> Cursor; + /// Copy selection fn copy_selection(&self) -> Option; @@ -229,7 +235,11 @@ pub trait Edit { /// Insert a string at the current cursor or replacing the current selection with the given /// attributes, or with the previous character's attributes if None is given. - fn insert_string(&mut self, data: &str, attrs_list: Option); + fn insert_string(&mut self, data: &str, attrs_list: Option) { + self.delete_selection(); + let new_cursor = self.insert_at(self.cursor(), data, attrs_list); + self.set_cursor(new_cursor); + } /// Apply a change fn apply_change(&mut self, change: &Change) -> bool; diff --git a/src/edit/syntect.rs b/src/edit/syntect.rs index 350e4a6..f376d9f 100644 --- a/src/edit/syntect.rs +++ b/src/edit/syntect.rs @@ -300,6 +300,14 @@ impl<'a> Edit for SyntaxEditor<'a> { self.editor.shape_as_needed(font_system); } + fn delete_range(&mut self, start: Cursor, end: Cursor) { + self.editor.delete_range(start, end); + } + + fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option) -> Cursor { + self.editor.insert_at(cursor, data, attrs_list) + } + fn copy_selection(&self) -> Option { self.editor.copy_selection() } @@ -308,10 +316,6 @@ impl<'a> Edit for SyntaxEditor<'a> { self.editor.delete_selection() } - fn insert_string(&mut self, data: &str, attrs_list: Option) { - self.editor.insert_string(data, attrs_list); - } - fn apply_change(&mut self, change: &Change) -> bool { self.editor.apply_change(change) } diff --git a/src/edit/vi.rs b/src/edit/vi.rs index c033350..d757b68 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -294,6 +294,14 @@ impl<'a> Edit for ViEditor<'a> { self.editor.shape_as_needed(font_system); } + fn delete_range(&mut self, start: Cursor, end: Cursor) { + self.editor.delete_range(start, end); + } + + fn insert_at(&mut self, cursor: Cursor, data: &str, attrs_list: Option) -> Cursor { + self.editor.insert_at(cursor, data, attrs_list) + } + fn copy_selection(&self) -> Option { self.editor.copy_selection() } @@ -302,10 +310,6 @@ impl<'a> Edit for ViEditor<'a> { self.editor.delete_selection() } - fn insert_string(&mut self, data: &str, attrs_list: Option) { - self.editor.insert_string(data, attrs_list); - } - fn apply_change(&mut self, change: &Change) -> bool { self.editor.apply_change(change) }