Improve safety of Cursor::selection

This commit is contained in:
Héctor Ramón Jiménez 2020-07-10 23:59:49 +02:00
parent 855c0faa59
commit a1210c9dae
3 changed files with 14 additions and 20 deletions

View File

@ -329,7 +329,7 @@ where
}
keyboard::KeyCode::Backspace => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection().is_none()
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);
@ -349,7 +349,7 @@ where
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection().is_none()
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);

View File

@ -166,8 +166,8 @@ impl Cursor {
end.min(value.len())
}
pub(crate) fn selection(&self) -> Option<(usize, usize)> {
match self.state {
pub(crate) fn selection(&self, value: &Value) -> Option<(usize, usize)> {
match self.state(value) {
State::Selection { start, end } => {
Some((start.min(end), start.max(end)))
}

View File

@ -15,13 +15,11 @@ impl<'a> Editor<'a> {
}
pub fn insert(&mut self, character: char) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((left, right)) => {
if left < self.value.len() {
self.cursor.move_left(self.value);
self.value.remove_many(left, right.min(self.value.len()));
}
}
_ => (),
}
@ -32,13 +30,11 @@ impl<'a> Editor<'a> {
pub fn paste(&mut self, content: Value) {
let length = content.len();
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((left, right)) => {
if left < self.value.len() {
self.cursor.move_left(self.value);
self.value.remove_many(left, right.min(self.value.len()));
}
}
_ => (),
}
@ -48,13 +44,11 @@ impl<'a> Editor<'a> {
}
pub fn backspace(&mut self) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((start, end)) => {
if start < self.value.len() {
self.cursor.move_left(self.value);
self.value.remove_many(start, end.min(self.value.len()));
}
}
None => {
let start = self.cursor.start(self.value);
@ -67,7 +61,7 @@ impl<'a> Editor<'a> {
}
pub fn delete(&mut self) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some(_) => {
self.backspace();
}