diff --git a/xray_core/src/buffer.rs b/xray_core/src/buffer.rs index c9e3a4e7..2d6322bb 100644 --- a/xray_core/src/buffer.rs +++ b/xray_core/src/buffer.rs @@ -735,6 +735,10 @@ impl Buffer { self.fragments.len::() } + pub fn clip_point(&self, original: Point) -> Point { + return cmp::max(cmp::min(original,self.max_point()),Point::new(0,0)); + } + pub fn line(&self, row: u32) -> Result, Error> { let mut iterator = self.iter_starting_at_point(Point::new(row, 0)).peekable(); if iterator.peek().is_none() { diff --git a/xray_core/src/buffer_view.rs b/xray_core/src/buffer_view.rs index 5c365d82..c0ce12ca 100644 --- a/xray_core/src/buffer_view.rs +++ b/xray_core/src/buffer_view.rs @@ -271,11 +271,11 @@ impl BufferView { } pub fn set_cursor_position(&mut self, position: Point, autoscroll: bool) { + let clipped_point = self.buffer.borrow().clip_point(position); self.buffer .borrow_mut() .mutate_selections(self.selection_set_id, |buffer, selections| { - // TODO: Clip point or return a result. - let anchor = buffer.anchor_before_point(position).unwrap(); + let anchor = buffer.anchor_before_point(clipped_point).unwrap(); selections.clear(); selections.push(Selection { start: anchor.clone(), @@ -292,13 +292,13 @@ impl BufferView { pub fn add_selection(&mut self, start: Point, end: Point) { debug_assert!(start <= end); // TODO: Reverse selection if end < start - + let clipped_start = self.buffer.borrow().clip_point(start); + let clipped_end = self.buffer.borrow().clip_point(end); self.buffer .borrow_mut() .mutate_selections(self.selection_set_id, |buffer, selections| { - // TODO: Clip points or return a result. - let start_anchor = buffer.anchor_before_point(start).unwrap(); - let end_anchor = buffer.anchor_before_point(end).unwrap(); + let start_anchor = buffer.anchor_before_point(clipped_start).unwrap(); + let end_anchor = buffer.anchor_before_point(clipped_end).unwrap(); let index = match selections.binary_search_by(|probe| { buffer.cmp_anchors(&probe.start, &start_anchor).unwrap()