diff --git a/src/ui_model/cell.rs b/src/ui_model/cell.rs index e121981..b2a5a57 100644 --- a/src/ui_model/cell.rs +++ b/src/ui_model/cell.rs @@ -63,4 +63,3 @@ impl Cell { self.dirty = true; } } - diff --git a/src/ui_model/line.rs b/src/ui_model/line.rs index 07b6452..e358b84 100644 --- a/src/ui_model/line.rs +++ b/src/ui_model/line.rs @@ -70,9 +70,12 @@ impl Line { fn set_cell_to_item(&mut self, new_item: &PangoItemPosition) -> bool { let start_item_idx = self.cell_to_item(new_item.start_cell); let start_item_cells_count = if start_item_idx >= 0 { - self.item_line[start_item_idx as usize] - .as_ref() - .map_or(-1, |item| item.cells_count as i32) + self.item_line[start_item_idx as usize].as_ref().map_or( + -1, + |item| { + item.cells_count as i32 + }, + ) } else { -1 }; @@ -83,8 +86,8 @@ impl Line { // in case different item length was in previous iteration // mark all item as dirty if start_item_idx != new_item.start_cell as i32 || - new_item.cells_count() != start_item_cells_count || start_item_idx == -1 || - end_item_idx == -1 + new_item.cells_count() != start_item_cells_count || + start_item_idx == -1 || end_item_idx == -1 { self.initialize_cell_item(new_item.start_cell, new_item.end_cell, new_item.item); true @@ -173,7 +176,12 @@ impl Line { } pub fn item_len_from_idx(&self, start_idx: usize) -> usize { - debug_assert!(start_idx < self.line.len()); + debug_assert!( + start_idx < self.line.len(), + "idx={}, len={}", + start_idx, + self.line.len() + ); let item_idx = self.cell_to_item(start_idx); diff --git a/src/ui_model/mod.rs b/src/ui_model/mod.rs index 4b4a472..ec71f26 100644 --- a/src/ui_model/mod.rs +++ b/src/ui_model/mod.rs @@ -70,6 +70,12 @@ impl UiModel { } pub fn set_cursor(&mut self, row: usize, col: usize) -> ModelRectVec { + // it is possible in some cases that cursor moved out of visible rect + // see https://github.com/daa84/neovim-gtk/issues/20 + if row >= self.model.len() || col >= self.model[row].line.len() { + return ModelRectVec::empty(); + } + let mut changed_region = ModelRectVec::new(self.cur_point()); self.cur_row = row; diff --git a/src/ui_model/model_rect.rs b/src/ui_model/model_rect.rs index c2117b4..8fad371 100644 --- a/src/ui_model/model_rect.rs +++ b/src/ui_model/model_rect.rs @@ -9,6 +9,10 @@ pub struct ModelRectVec { } impl ModelRectVec { + pub fn empty() -> ModelRectVec { + ModelRectVec { list: vec![] } + } + pub fn new(first: ModelRect) -> ModelRectVec { ModelRectVec { list: vec![first] } } @@ -257,13 +261,7 @@ impl ModelRect { ) } - pub fn from_area( - cell_metrics: &CellMetrics, - x1: f64, - y1: f64, - x2: f64, - y2: f64, - ) -> ModelRect { + pub fn from_area(cell_metrics: &CellMetrics, x1: f64, y1: f64, x2: f64, y2: f64) -> ModelRect { let &CellMetrics { char_width, line_height,