Improve safety of Cursor::selection
This commit is contained in:
parent
855c0faa59
commit
a1210c9dae
@ -329,7 +329,7 @@ where
|
|||||||
}
|
}
|
||||||
keyboard::KeyCode::Backspace => {
|
keyboard::KeyCode::Backspace => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
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 {
|
if self.is_secure {
|
||||||
let cursor_pos = self.state.cursor.end(&self.value);
|
let cursor_pos = self.state.cursor.end(&self.value);
|
||||||
@ -349,7 +349,7 @@ where
|
|||||||
}
|
}
|
||||||
keyboard::KeyCode::Delete => {
|
keyboard::KeyCode::Delete => {
|
||||||
if platform::is_jump_modifier_pressed(modifiers)
|
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 {
|
if self.is_secure {
|
||||||
let cursor_pos = self.state.cursor.end(&self.value);
|
let cursor_pos = self.state.cursor.end(&self.value);
|
||||||
|
@ -166,8 +166,8 @@ impl Cursor {
|
|||||||
end.min(value.len())
|
end.min(value.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn selection(&self) -> Option<(usize, usize)> {
|
pub(crate) fn selection(&self, value: &Value) -> Option<(usize, usize)> {
|
||||||
match self.state {
|
match self.state(value) {
|
||||||
State::Selection { start, end } => {
|
State::Selection { start, end } => {
|
||||||
Some((start.min(end), start.max(end)))
|
Some((start.min(end), start.max(end)))
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,10 @@ impl<'a> Editor<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, character: char) {
|
pub fn insert(&mut self, character: char) {
|
||||||
match self.cursor.selection() {
|
match self.cursor.selection(self.value) {
|
||||||
Some((left, right)) => {
|
Some((left, right)) => {
|
||||||
if left < self.value.len() {
|
self.cursor.move_left(self.value);
|
||||||
self.cursor.move_left(self.value);
|
self.value.remove_many(left, right.min(self.value.len()));
|
||||||
self.value.remove_many(left, right.min(self.value.len()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
@ -32,12 +30,10 @@ impl<'a> Editor<'a> {
|
|||||||
pub fn paste(&mut self, content: Value) {
|
pub fn paste(&mut self, content: Value) {
|
||||||
let length = content.len();
|
let length = content.len();
|
||||||
|
|
||||||
match self.cursor.selection() {
|
match self.cursor.selection(self.value) {
|
||||||
Some((left, right)) => {
|
Some((left, right)) => {
|
||||||
if left < self.value.len() {
|
self.cursor.move_left(self.value);
|
||||||
self.cursor.move_left(self.value);
|
self.value.remove_many(left, right.min(self.value.len()));
|
||||||
self.value.remove_many(left, right.min(self.value.len()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
@ -48,12 +44,10 @@ impl<'a> Editor<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn backspace(&mut self) {
|
pub fn backspace(&mut self) {
|
||||||
match self.cursor.selection() {
|
match self.cursor.selection(self.value) {
|
||||||
Some((start, end)) => {
|
Some((start, end)) => {
|
||||||
if start < self.value.len() {
|
self.cursor.move_left(self.value);
|
||||||
self.cursor.move_left(self.value);
|
self.value.remove_many(start, end.min(self.value.len()));
|
||||||
self.value.remove_many(start, end.min(self.value.len()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let start = self.cursor.start(self.value);
|
let start = self.cursor.start(self.value);
|
||||||
@ -67,7 +61,7 @@ impl<'a> Editor<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&mut self) {
|
pub fn delete(&mut self) {
|
||||||
match self.cursor.selection() {
|
match self.cursor.selection(self.value) {
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
self.backspace();
|
self.backspace();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user