This commit is contained in:
Xerxes-2 2024-07-02 23:40:17 +05:30 committed by GitHub
commit 2ce718311f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View File

@ -39,8 +39,11 @@ impl Input {
));
let (limit, snap) = (self.limit(), self.snap_mut());
if snap.offset > snap.cursor {
snap.offset = snap.cursor;
let offset = InputSnap::find_window_backward(&snap.value, snap.cursor, limit / 2)
.start
.min(InputSnap::find_window_backward(&snap.value, snap.value.chars().count(), limit).start);
if snap.offset != offset {
snap.offset = offset;
} else if snap.value.is_empty() {
snap.offset = 0;
} else {

View File

@ -87,4 +87,25 @@ impl InputSnap {
}
*v.first().unwrap()..v.last().unwrap() + 1
}
#[inline]
pub(super) fn find_window_backward(s: &str, offset: usize, limit: usize) -> Range<usize> {
let mut width = 0;
let len = s.chars().count();
let v: Vec<_> = s
.chars()
.rev()
.enumerate()
.skip(len.saturating_sub(offset + 1))
.map_while(|(i, c)| {
width += c.width().unwrap_or(0);
if width < limit { Some(len.saturating_sub(i + 1)) } else { None }
})
.collect();
if v.is_empty() {
return 0..0;
}
*v.last().unwrap()..v.first().unwrap() + 1
}
}