Skip to content

Commit

Permalink
Fix panic when moving over multicharacter graphemes (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterdd authored Jan 1, 2024
1 parent 7367927 commit deb40f5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,26 @@ impl TextInput {
fn move_cursor(&mut self, move_kind: Movement, direction: Direction) -> bool {
match (move_kind, direction) {
(Movement::Glyph, Direction::Left) => {
if self.cursor_glyph_idx >= 1 {
self.cursor_glyph_idx -= 1;
return true;
let untracked_buffer = self.buffer.get_untracked();
let mut grapheme_iter = untracked_buffer[..self.cursor_glyph_idx].graphemes(true);
match grapheme_iter.next_back() {
None => false,
Some(prev_character) => {
self.cursor_glyph_idx -= prev_character.len();
true
}
}
false
}
(Movement::Glyph, Direction::Right) => {
if self.cursor_glyph_idx < self.buffer.with_untracked(|buff| buff.len()) {
self.cursor_glyph_idx += 1;
return true;
let untracked_buffer = self.buffer.get_untracked();
let mut grapheme_iter = untracked_buffer[self.cursor_glyph_idx..].graphemes(true);
match grapheme_iter.next() {
None => false,
Some(next_character) => {
self.cursor_glyph_idx += next_character.len();
true
}
}
false
}
(Movement::Line, Direction::Right) => {
if self.cursor_glyph_idx < self.buffer.with_untracked(|buff| buff.len()) {
Expand Down

0 comments on commit deb40f5

Please sign in to comment.