Editor: make delete_range and insert_at methods public

This commit is contained in:
Jeremy Soller 2023-12-05 10:04:50 -07:00
parent afc5b525f1
commit de6f2c7fbe
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
4 changed files with 93 additions and 81 deletions

View file

@ -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<String> {
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<AttrsList>) {
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() {

View file

@ -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<AttrsList>) -> Cursor;
/// Copy selection
fn copy_selection(&self) -> Option<String>;
@ -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<AttrsList>);
fn insert_string(&mut self, data: &str, attrs_list: Option<AttrsList>) {
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;

View file

@ -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<AttrsList>) -> Cursor {
self.editor.insert_at(cursor, data, attrs_list)
}
fn copy_selection(&self) -> Option<String> {
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<AttrsList>) {
self.editor.insert_string(data, attrs_list);
}
fn apply_change(&mut self, change: &Change) -> bool {
self.editor.apply_change(change)
}

View file

@ -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<AttrsList>) -> Cursor {
self.editor.insert_at(cursor, data, attrs_list)
}
fn copy_selection(&self) -> Option<String> {
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<AttrsList>) {
self.editor.insert_string(data, attrs_list);
}
fn apply_change(&mut self, change: &Change) -> bool {
self.editor.apply_change(change)
}