tui: fix crash when alt+bksp past unicode nbsp (#5016)
notably, screenshot filenames on macOS by default contain U+202F right before the "AM/PM" part of the filename.
This commit is contained in:
@@ -799,16 +799,22 @@ impl TextArea {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn beginning_of_previous_word(&self) -> usize {
|
pub(crate) fn beginning_of_previous_word(&self) -> usize {
|
||||||
if let Some(first_non_ws) = self.text[..self.cursor_pos].rfind(|c: char| !c.is_whitespace())
|
let prefix = &self.text[..self.cursor_pos];
|
||||||
{
|
let Some((first_non_ws_idx, _)) = prefix
|
||||||
let candidate = self.text[..first_non_ws]
|
.char_indices()
|
||||||
.rfind(|c: char| c.is_whitespace())
|
.rev()
|
||||||
.map(|i| i + 1)
|
.find(|&(_, ch)| !ch.is_whitespace())
|
||||||
.unwrap_or(0);
|
else {
|
||||||
self.adjust_pos_out_of_elements(candidate, true)
|
return 0;
|
||||||
} else {
|
};
|
||||||
0
|
let before = &prefix[..first_non_ws_idx];
|
||||||
}
|
let candidate = before
|
||||||
|
.char_indices()
|
||||||
|
.rev()
|
||||||
|
.find(|&(_, ch)| ch.is_whitespace())
|
||||||
|
.map(|(idx, ch)| idx + ch.len_utf8())
|
||||||
|
.unwrap_or(0);
|
||||||
|
self.adjust_pos_out_of_elements(candidate, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn end_of_next_word(&self) -> usize {
|
pub(crate) fn end_of_next_word(&self) -> usize {
|
||||||
@@ -1262,6 +1268,15 @@ mod tests {
|
|||||||
assert_eq!(t.cursor(), 6);
|
assert_eq!(t.cursor(), 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn delete_backward_word_handles_narrow_no_break_space() {
|
||||||
|
let mut t = ta_with("32\u{202F}AM");
|
||||||
|
t.set_cursor(t.text().len());
|
||||||
|
t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::ALT));
|
||||||
|
pretty_assertions::assert_eq!(t.text(), "32\u{202F}");
|
||||||
|
pretty_assertions::assert_eq!(t.cursor(), t.text().len());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn delete_forward_word_with_without_alt_modifier() {
|
fn delete_forward_word_with_without_alt_modifier() {
|
||||||
let mut t = ta_with("hello world");
|
let mut t = ta_with("hello world");
|
||||||
|
|||||||
Reference in New Issue
Block a user