This commit is contained in:
sxyazi 2023-11-15 07:30:38 +08:00
parent 0a494038c9
commit c5ebdbecad
No known key found for this signature in database
2 changed files with 16 additions and 22 deletions

View file

@ -52,7 +52,7 @@ impl Input {
let snap = self.snap_mut(); let snap = self.snap_mut();
snap.cursor = match range.start_bound() { snap.cursor = match range.start_bound() {
std::ops::Bound::Included(i) => *i, std::ops::Bound::Included(i) => *i,
std::ops::Bound::Excluded(i) => i + 1, std::ops::Bound::Excluded(_) => unreachable!(),
std::ops::Bound::Unbounded => 0, std::ops::Bound::Unbounded => 0,
}; };
if snap.value.drain(range).next().is_some() { if snap.value.drain(range).next().is_some() {
@ -62,15 +62,22 @@ impl Input {
false false
} }
fn forward_delete(&mut self) -> bool { fn backspace(&mut self, under: bool) -> bool {
let snap = self.snaps.current_mut(); let snap = self.snaps.current_mut();
if snap.cursor >= snap.value.len() { if !under && snap.cursor < 1 {
return false;
} else if under && snap.cursor >= snap.value.len() {
return false; return false;
} else {
snap.value.remove(snap.idx(snap.cursor).unwrap());
} }
if under {
snap.value.remove(snap.idx(snap.cursor).unwrap());
self.move_(0); self.move_(0);
} else {
snap.value.remove(snap.idx(snap.cursor - 1).unwrap());
self.move_(-1);
}
self.flush_value(); self.flush_value();
true true
} }
@ -99,10 +106,10 @@ impl Input {
Key { code: C('f'), shift: false, ctrl: true, alt: false } => self.move_(1), Key { code: C('f'), shift: false, ctrl: true, alt: false } => self.move_(1),
// Delete the character before the cursor // Delete the character before the cursor
Key { code: Backspace, shift: false, ctrl: false, alt: false } => self.backspace(), Key { code: Backspace, shift: false, ctrl: false, alt: false } => self.backspace(false),
Key { code: C('h'), shift: false, ctrl: true, alt: false } => self.backspace(), Key { code: C('h'), shift: false, ctrl: true, alt: false } => self.backspace(false),
// Delete the character under the cursor // Delete the character under the cursor
Key { code: C('d'), shift: false, ctrl: true, alt: false } => self.forward_delete(), Key { code: C('d'), shift: false, ctrl: true, alt: false } => self.backspace(true),
// Move back to the start of the current or previous word // Move back to the start of the current or previous word
Key { code: C('b'), shift: false, ctrl: false, alt: true } => { Key { code: C('b'), shift: false, ctrl: false, alt: true } => {

View file

@ -56,19 +56,6 @@ impl Input {
true true
} }
pub fn backspace(&mut self) -> bool {
let snap = self.snaps.current_mut();
if snap.cursor < 1 {
return false;
} else {
snap.value.remove(snap.idx(snap.cursor - 1).unwrap());
}
self.move_(-1);
self.flush_value();
true
}
pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool { pub(super) fn handle_op(&mut self, cursor: usize, include: bool) -> bool {
let old = self.snap().clone(); let old = self.snap().clone();
let snap = self.snaps.current_mut(); let snap = self.snaps.current_mut();