Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Buffer Selection Clipping #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions xray_core/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,10 @@ impl Buffer {
self.fragments.len::<Point>()
}

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<Vec<u16>, Error> {
let mut iterator = self.iter_starting_at_point(Point::new(row, 0)).peekable();
if iterator.peek().is_none() {
Expand Down
12 changes: 6 additions & 6 deletions xray_core/src/buffer_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be cool just shadowing the original name position?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what test coverage should be added?

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(),
Expand All @@ -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()
Expand Down