Improve safety of Cursor::selection
This commit is contained in:
parent
855c0faa59
commit
a1210c9dae
@ -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);
|
||||
|
@ -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)))
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user