Add Editor::insert_text
This commit is contained in:
parent
c5a5913981
commit
39d3219df9
2 changed files with 30 additions and 19 deletions
|
|
@ -202,6 +202,27 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
true
|
||||
}
|
||||
|
||||
fn insert_string(&mut self, data: &str, attrs_list: Option<AttrsList>) {
|
||||
let len = data.len();
|
||||
|
||||
self.delete_selection();
|
||||
|
||||
let line: &mut BufferLine = &mut self.buffer.lines[self.cursor.line];
|
||||
|
||||
// Collect text after insertion as a line
|
||||
let after: BufferLine = line.split_off(self.cursor.index);
|
||||
|
||||
// Collect attributes
|
||||
let final_attrs = attrs_list.unwrap_or_else(|| AttrsList::new(line.attrs_list().get_span(line.text().len())));
|
||||
|
||||
// Append the inserted text
|
||||
line.append(BufferLine::new(data, final_attrs));
|
||||
|
||||
// Append the text after insertion
|
||||
line.append(after);
|
||||
self.cursor.index += len;
|
||||
}
|
||||
|
||||
fn action(&mut self, action: Action) {
|
||||
let old_cursor = self.cursor;
|
||||
|
||||
|
|
@ -344,28 +365,14 @@ impl<'a> Edit<'a> for Editor<'a> {
|
|||
},
|
||||
Action::Insert(character) => {
|
||||
if character.is_control()
|
||||
&& !['\t', '\u{92}'].contains(&character)
|
||||
&& !['\t', '\u{92}'].contains(&character)
|
||||
{
|
||||
// Filter out special chars (except for tab), use Action instead
|
||||
log::debug!("Refusing to insert control character {:?}", character);
|
||||
} else {
|
||||
self.delete_selection();
|
||||
|
||||
let line = &mut self.buffer.lines[self.cursor.line];
|
||||
|
||||
// Collect text after insertion as a line
|
||||
let after = line.split_off(self.cursor.index);
|
||||
|
||||
// Append the inserted text
|
||||
line.append(BufferLine::new(
|
||||
character.to_string(),
|
||||
AttrsList::new(line.attrs_list().defaults() /*TODO: provide attrs?*/)
|
||||
));
|
||||
|
||||
// Append the text after insertion
|
||||
line.append(after);
|
||||
|
||||
self.cursor.index += character.len_utf8();
|
||||
let mut str_buf = [0u8; 8];
|
||||
let str_ref = character.encode_utf8(&mut str_buf);
|
||||
self.insert_string(str_ref, None);
|
||||
}
|
||||
},
|
||||
Action::Enter => {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#[cfg(not(feature = "std"))]
|
||||
use alloc::string::String;
|
||||
|
||||
use crate::{Buffer, Cursor};
|
||||
use crate::{AttrsList, Buffer, Cursor};
|
||||
#[cfg(feature = "swash")]
|
||||
use crate::Color;
|
||||
|
||||
|
|
@ -86,6 +86,10 @@ pub trait Edit<'a> {
|
|||
// Also used by backspace, delete, insert, and enter when there is a selection
|
||||
fn delete_selection(&mut self) -> bool;
|
||||
|
||||
/// 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>);
|
||||
|
||||
/// Perform an [Action] on the editor
|
||||
fn action(&mut self, action: Action);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue