From 69bd7a4344c658fba27f4032c9e66d5bbe156bcb Mon Sep 17 00:00:00 2001 From: Cole Kissane Date: Tue, 18 Sep 2018 10:18:43 -0700 Subject: [PATCH 1/3] Buffer Selection Clipping --- xray_core/src/buffer.rs | 4 ++++ xray_core/src/buffer_view.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) 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() From df21a9b03a7807792e8e333d510a404f304eb2b3 Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 16 Apr 2019 10:22:36 +0200 Subject: [PATCH 2/3] Shadowing variables name --- xray_core/src/buffer_view.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xray_core/src/buffer_view.rs b/xray_core/src/buffer_view.rs index c0ce12ca..e4432728 100644 --- a/xray_core/src/buffer_view.rs +++ b/xray_core/src/buffer_view.rs @@ -271,11 +271,12 @@ impl BufferView { } pub fn set_cursor_position(&mut self, position: Point, autoscroll: bool) { - let clipped_point = self.buffer.borrow().clip_point(position); + let position = self.buffer.borrow().clip_point(position); + self.buffer .borrow_mut() .mutate_selections(self.selection_set_id, |buffer, selections| { - let anchor = buffer.anchor_before_point(clipped_point).unwrap(); + let anchor = buffer.anchor_before_point(position).unwrap(); selections.clear(); selections.push(Selection { start: anchor.clone(), @@ -292,13 +293,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); + let start = self.buffer.borrow().clip_point(start); + let end = self.buffer.borrow().clip_point(end); self.buffer .borrow_mut() .mutate_selections(self.selection_set_id, |buffer, selections| { - let start_anchor = buffer.anchor_before_point(clipped_start).unwrap(); - let end_anchor = buffer.anchor_before_point(clipped_end).unwrap(); + let start_anchor = buffer.anchor_before_point(start).unwrap(); + let end_anchor = buffer.anchor_before_point(end).unwrap(); let index = match selections.binary_search_by(|probe| { buffer.cmp_anchors(&probe.start, &start_anchor).unwrap() From d6fbdd649666f16a474f5b451824c5aadaebf1c9 Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 16 Apr 2019 10:22:57 +0200 Subject: [PATCH 3/3] Add `clip_point` tests --- xray_core/src/buffer.rs | 20 +++++++++++++++++++- xray_core/src/buffer_view.rs | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/xray_core/src/buffer.rs b/xray_core/src/buffer.rs index 2d6322bb..597006b0 100644 --- a/xray_core/src/buffer.rs +++ b/xray_core/src/buffer.rs @@ -736,7 +736,7 @@ impl Buffer { } pub fn clip_point(&self, original: Point) -> Point { - return cmp::max(cmp::min(original,self.max_point()),Point::new(0,0)); + return cmp::max(cmp::min(original, self.max_point()), Point::new(0, 0)); } pub fn line(&self, row: u32) -> Result, Error> { @@ -3074,6 +3074,24 @@ mod tests { assert_eq!(buffer.offset_for_anchor(&after_end_anchor).unwrap(), 9); } + #[test] + fn test_clip_point() { + let mut buffer = Buffer::new(0); + buffer.edit(&[0..0], "abcdefghi"); + + let point = buffer.clip_point(Point::new(0, 0)); + assert_eq!(point.row, 0); + assert_eq!(point.column, 0); + + let point = buffer.clip_point(Point::new(0, 2)); + assert_eq!(point.row, 0); + assert_eq!(point.column, 2); + + let point = buffer.clip_point(Point::new(1, 12)); + assert_eq!(point.row, 0); + assert_eq!(point.column, 9); + } + #[test] fn test_snapshot() { let mut buffer = Buffer::new(0); diff --git a/xray_core/src/buffer_view.rs b/xray_core/src/buffer_view.rs index e4432728..a25df43d 100644 --- a/xray_core/src/buffer_view.rs +++ b/xray_core/src/buffer_view.rs @@ -293,6 +293,7 @@ impl BufferView { pub fn add_selection(&mut self, start: Point, end: Point) { debug_assert!(start <= end); // TODO: Reverse selection if end < start + let start = self.buffer.borrow().clip_point(start); let end = self.buffer.borrow().clip_point(end); self.buffer