Skip to content

Commit

Permalink
Avoid breaking from the document bound when handling a mouse click
Browse files Browse the repository at this point in the history
Fixes #26
  • Loading branch information
brouberol committed Aug 15, 2021
1 parent 0500dd6 commit 27eca13
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ impl Editor {
),
MouseEvent::Release(_, _) => {
if !self.mouse_event_buffer.is_empty() {
self.cursor_position = self.mouse_event_buffer.pop().unwrap();
// Make sure that we're moving to an x/y location in which we already
// have text, to avoid breaking out of the document bounds.
let cursor_position = self.mouse_event_buffer.pop().unwrap();
if cursor_position.y.saturating_add(1) <= self.document.num_rows() {
if let Some(target_row) = self.document.get_row(cursor_position.y) {
if cursor_position.x <= target_row.len() {
self.cursor_position = cursor_position;
}
}
}
}
}
_ => (),
Expand Down

0 comments on commit 27eca13

Please sign in to comment.