Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Merged
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
22 changes: 22 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 Expand Up @@ -3070,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);
Expand Down
6 changes: 4 additions & 2 deletions xray_core/src/buffer_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,11 @@ impl BufferView {
}

pub fn set_cursor_position(&mut self, position: Point, autoscroll: bool) {
let position = 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();
selections.clear();
selections.push(Selection {
Expand All @@ -293,10 +294,11 @@ 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
.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();

Expand Down