2020-05-05 18:50:23 -04:00
|
|
|
//! State management for a selection in the grid.
|
2016-12-22 13:43:06 -05:00
|
|
|
//!
|
|
|
|
//! A selection should start when the mouse is clicked, and it should be
|
|
|
|
//! finalized when the button is released. The selection should be cleared
|
|
|
|
//! when text is added/removed/scrolled on the screen. The selection should
|
|
|
|
//! also be cleared if the user clicks off of the selection.
|
2020-06-06 14:49:14 -04:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
use std::mem;
|
2020-06-26 12:04:55 -04:00
|
|
|
use std::ops::{Bound, Range, RangeBounds};
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-24 17:57:22 -05:00
|
|
|
use crate::index::{Column, Line, Point, Side};
|
2019-04-29 10:33:25 -04:00
|
|
|
use crate::term::{Search, Term};
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// A Point and side within that point.
|
2020-01-24 17:57:22 -05:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2020-03-17 22:35:08 -04:00
|
|
|
struct Anchor {
|
2020-01-20 18:56:10 -05:00
|
|
|
point: Point<usize>,
|
|
|
|
side: Side,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Anchor {
|
|
|
|
fn new(point: Point<usize>, side: Side) -> Anchor {
|
|
|
|
Anchor { point, side }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a range of selected cells.
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
|
|
pub struct SelectionRange<L = usize> {
|
|
|
|
/// Start point, top left of the selection.
|
|
|
|
pub start: Point<L>,
|
|
|
|
/// End point, bottom right of the selection.
|
|
|
|
pub end: Point<L>,
|
|
|
|
/// Whether this selection is a block selection.
|
|
|
|
pub is_block: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<L> SelectionRange<L> {
|
|
|
|
pub fn new(start: Point<L>, end: Point<L>, is_block: bool) -> Self {
|
|
|
|
Self { start, end, is_block }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, col: Column, line: L) -> bool
|
|
|
|
where
|
|
|
|
L: PartialEq + PartialOrd,
|
|
|
|
{
|
|
|
|
self.start.line <= line
|
|
|
|
&& self.end.line >= line
|
|
|
|
&& (self.start.col <= col || (self.start.line != line && !self.is_block))
|
|
|
|
&& (self.end.col >= col || (self.end.line != line && !self.is_block))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 17:57:22 -05:00
|
|
|
/// Different kinds of selection.
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
2020-03-17 22:35:08 -04:00
|
|
|
pub enum SelectionType {
|
2020-01-24 17:57:22 -05:00
|
|
|
Simple,
|
|
|
|
Block,
|
|
|
|
Semantic,
|
|
|
|
Lines,
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Describes a region of a 2-dimensional area.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
2020-01-24 17:57:22 -05:00
|
|
|
/// Used to track a text selection. There are four supported modes, each with its own constructor:
|
|
|
|
/// [`simple`], [`block`], [`semantic`], and [`lines`]. The [`simple`] mode precisely tracks which
|
|
|
|
/// cells are selected without any expansion. [`block`] will select rectangular regions.
|
|
|
|
/// [`semantic`] mode expands the initial selection to the nearest semantic escape char in either
|
|
|
|
/// direction. [`lines`] will always select entire lines.
|
2017-06-16 00:43:28 -04:00
|
|
|
///
|
2020-01-24 17:57:22 -05:00
|
|
|
/// Calls to [`update`] operate different based on the selection kind. The [`simple`] and [`block`]
|
|
|
|
/// mode do nothing special, simply track points and sides. [`semantic`] will continue to expand
|
|
|
|
/// out to semantic boundaries as the selection point changes. Similarly, [`lines`] will always
|
|
|
|
/// expand the new point to encompass entire lines.
|
2017-06-16 00:43:28 -04:00
|
|
|
///
|
|
|
|
/// [`simple`]: enum.Selection.html#method.simple
|
2020-01-24 17:57:22 -05:00
|
|
|
/// [`block`]: enum.Selection.html#method.block
|
2017-06-16 00:43:28 -04:00
|
|
|
/// [`semantic`]: enum.Selection.html#method.semantic
|
|
|
|
/// [`lines`]: enum.Selection.html#method.lines
|
2019-10-04 20:29:26 -04:00
|
|
|
/// [`update`]: enum.Selection.html#method.update
|
2018-04-28 10:15:21 -04:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2020-01-24 17:57:22 -05:00
|
|
|
pub struct Selection {
|
2020-03-17 22:35:08 -04:00
|
|
|
pub ty: SelectionType,
|
2020-01-24 17:57:22 -05:00
|
|
|
region: Range<Anchor>,
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
|
|
|
|
2016-12-22 13:43:06 -05:00
|
|
|
impl Selection {
|
2020-03-17 22:35:08 -04:00
|
|
|
pub fn new(ty: SelectionType, location: Point<usize>, side: Side) -> Selection {
|
2020-01-24 17:57:22 -05:00
|
|
|
Self {
|
2020-01-20 18:56:10 -05:00
|
|
|
region: Range { start: Anchor::new(location, side), end: Anchor::new(location, side) },
|
2020-03-17 22:35:08 -04:00
|
|
|
ty,
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
/// Update the end of the selection.
|
2020-03-17 22:35:08 -04:00
|
|
|
pub fn update(&mut self, point: Point<usize>, side: Side) {
|
|
|
|
self.region.end = Anchor::new(point, side);
|
2020-01-24 17:57:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rotate(
|
|
|
|
mut self,
|
2020-05-30 16:45:44 -04:00
|
|
|
num_lines: Line,
|
|
|
|
num_cols: Column,
|
|
|
|
range: &Range<Line>,
|
|
|
|
delta: isize,
|
2020-01-24 17:57:22 -05:00
|
|
|
) -> Option<Selection> {
|
2020-05-30 16:45:44 -04:00
|
|
|
let range_bottom = range.start.0;
|
|
|
|
let range_top = range.end.0;
|
|
|
|
let num_lines = num_lines.0;
|
|
|
|
let num_cols = num_cols.0;
|
2020-01-24 17:57:22 -05:00
|
|
|
|
|
|
|
let (mut start, mut end) = (&mut self.region.start, &mut self.region.end);
|
|
|
|
if Self::points_need_swap(start.point, end.point) {
|
|
|
|
mem::swap(&mut start, &mut end);
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Rotate start of selection.
|
2020-05-30 16:45:44 -04:00
|
|
|
if (start.point.line < range_top || range_top == num_lines)
|
|
|
|
&& start.point.line >= range_bottom
|
2020-01-24 17:57:22 -05:00
|
|
|
{
|
2020-05-30 16:45:44 -04:00
|
|
|
start.point.line = usize::try_from(start.point.line as isize + delta).unwrap_or(0);
|
2020-01-20 18:56:10 -05:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// If end is within the same region, delete selection once start rotates out.
|
2020-05-30 16:45:44 -04:00
|
|
|
if start.point.line < range_bottom && end.point.line >= range_bottom {
|
2020-01-24 17:57:22 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Clamp selection to start of region.
|
2020-05-30 16:45:44 -04:00
|
|
|
if start.point.line >= range_top && range_top != num_lines {
|
2020-01-24 17:57:22 -05:00
|
|
|
if self.ty != SelectionType::Block {
|
|
|
|
start.point.col = Column(0);
|
|
|
|
start.side = Side::Left;
|
|
|
|
}
|
2020-05-30 16:45:44 -04:00
|
|
|
start.point.line = range_top - 1;
|
2020-01-24 17:57:22 -05:00
|
|
|
}
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Rotate end of selection.
|
2020-05-30 16:45:44 -04:00
|
|
|
if (end.point.line < range_top || range_top == num_lines) && end.point.line >= range_bottom
|
2020-01-24 17:57:22 -05:00
|
|
|
{
|
2020-05-30 16:45:44 -04:00
|
|
|
end.point.line = usize::try_from(end.point.line as isize + delta).unwrap_or(0);
|
2020-01-24 17:57:22 -05:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Delete selection if end has overtaken the start.
|
2020-01-24 17:57:22 -05:00
|
|
|
if end.point.line > start.point.line {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Clamp selection to end of region.
|
2020-05-30 16:45:44 -04:00
|
|
|
if end.point.line < range_bottom {
|
2020-01-24 17:57:22 -05:00
|
|
|
if self.ty != SelectionType::Block {
|
|
|
|
end.point.col = Column(num_cols - 1);
|
|
|
|
end.side = Side::Right;
|
|
|
|
}
|
2020-05-30 16:45:44 -04:00
|
|
|
end.point.line = range_bottom;
|
2020-01-24 17:57:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(self)
|
2020-01-20 18:56:10 -05:00
|
|
|
}
|
|
|
|
|
2019-06-20 11:56:09 -04:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2020-01-24 17:57:22 -05:00
|
|
|
match self.ty {
|
|
|
|
SelectionType::Simple => {
|
|
|
|
let (mut start, mut end) = (self.region.start, self.region.end);
|
2020-05-30 16:45:44 -04:00
|
|
|
if Self::points_need_swap(start.point, end.point) {
|
2020-01-24 17:57:22 -05:00
|
|
|
mem::swap(&mut start, &mut end);
|
|
|
|
}
|
2019-06-20 11:56:09 -04:00
|
|
|
|
2019-09-13 19:51:14 -04:00
|
|
|
// Simple selection is empty when the points are identical
|
2020-05-05 18:50:23 -04:00
|
|
|
// or two adjacent cells have the sides right -> left.
|
2019-06-20 11:56:09 -04:00
|
|
|
start == end
|
2019-11-03 15:59:28 -05:00
|
|
|
|| (start.side == Side::Right
|
|
|
|
&& end.side == Side::Left
|
2019-09-13 19:51:14 -04:00
|
|
|
&& (start.point.line == end.point.line)
|
2019-11-03 15:59:28 -05:00
|
|
|
&& start.point.col + 1 == end.point.col)
|
2019-06-20 11:56:09 -04:00
|
|
|
},
|
2020-01-24 17:57:22 -05:00
|
|
|
SelectionType::Block => {
|
|
|
|
let (start, end) = (self.region.start, self.region.end);
|
|
|
|
|
2019-09-13 19:51:14 -04:00
|
|
|
// Block selection is empty when the points' columns and sides are identical
|
|
|
|
// or two cells with adjacent columns have the sides right -> left,
|
|
|
|
// regardless of their lines
|
|
|
|
(start.point.col == end.point.col && start.side == end.side)
|
2019-11-03 15:59:28 -05:00
|
|
|
|| (start.point.col + 1 == end.point.col
|
|
|
|
&& start.side == Side::Right
|
|
|
|
&& end.side == Side::Left)
|
|
|
|
|| (end.point.col + 1 == start.point.col
|
2019-09-13 19:51:14 -04:00
|
|
|
&& start.side == Side::Left
|
|
|
|
&& end.side == Side::Right)
|
|
|
|
},
|
2020-01-24 17:57:22 -05:00
|
|
|
SelectionType::Semantic | SelectionType::Lines => false,
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 12:04:55 -04:00
|
|
|
/// Check whether selection contains any point in a given range.
|
|
|
|
pub fn intersects_range<R: RangeBounds<usize>>(&self, range: R) -> bool {
|
|
|
|
let mut start = self.region.start.point.line;
|
|
|
|
let mut end = self.region.end.point.line;
|
|
|
|
|
|
|
|
if Self::points_need_swap(self.region.start.point, self.region.end.point) {
|
|
|
|
mem::swap(&mut start, &mut end);
|
|
|
|
}
|
|
|
|
|
|
|
|
let range_start = match range.start_bound() {
|
|
|
|
Bound::Included(&range_start) => range_start,
|
|
|
|
Bound::Excluded(&range_start) => range_start.saturating_add(1),
|
|
|
|
Bound::Unbounded => 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let range_end = match range.end_bound() {
|
|
|
|
Bound::Included(&range_end) => range_end,
|
|
|
|
Bound::Excluded(&range_end) => range_end.saturating_sub(1),
|
|
|
|
Bound::Unbounded => usize::max_value(),
|
|
|
|
};
|
|
|
|
|
|
|
|
range_start <= start && range_end >= end
|
|
|
|
}
|
|
|
|
|
2020-03-17 22:35:08 -04:00
|
|
|
/// Expand selection sides to include all cells.
|
|
|
|
pub fn include_all(&mut self) {
|
|
|
|
let (start, end) = (self.region.start.point, self.region.end.point);
|
|
|
|
let (start_side, end_side) = match self.ty {
|
|
|
|
SelectionType::Block
|
|
|
|
if start.col > end.col || (start.col == end.col && start.line < end.line) =>
|
|
|
|
{
|
|
|
|
(Side::Right, Side::Left)
|
|
|
|
},
|
|
|
|
SelectionType::Block => (Side::Left, Side::Right),
|
|
|
|
_ if Self::points_need_swap(start, end) => (Side::Right, Side::Left),
|
|
|
|
_ => (Side::Left, Side::Right),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.region.start.side = start_side;
|
|
|
|
self.region.end.side = end_side;
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Convert selection to grid coordinates.
|
|
|
|
pub fn to_range<T>(&self, term: &Term<T>) -> Option<SelectionRange> {
|
|
|
|
let grid = term.grid();
|
|
|
|
let num_cols = grid.num_cols();
|
2019-06-06 09:04:12 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Order start above the end.
|
2020-06-26 12:04:55 -04:00
|
|
|
let mut start = self.region.start;
|
|
|
|
let mut end = self.region.end;
|
|
|
|
|
2020-01-24 17:57:22 -05:00
|
|
|
if Self::points_need_swap(start.point, end.point) {
|
|
|
|
mem::swap(&mut start, &mut end);
|
|
|
|
}
|
2019-06-20 11:56:09 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Clamp to inside the grid buffer.
|
2020-01-24 17:57:22 -05:00
|
|
|
let is_block = self.ty == SelectionType::Block;
|
|
|
|
let (start, end) = Self::grid_clamp(start, end, is_block, grid.len()).ok()?;
|
|
|
|
|
2020-02-07 01:50:18 -05:00
|
|
|
match self.ty {
|
2020-01-24 17:57:22 -05:00
|
|
|
SelectionType::Simple => self.range_simple(start, end, num_cols),
|
|
|
|
SelectionType::Block => self.range_block(start, end),
|
|
|
|
SelectionType::Semantic => Self::range_semantic(term, start.point, end.point),
|
|
|
|
SelectionType::Lines => Self::range_lines(term, start.point, end.point),
|
2020-01-20 18:56:10 -05:00
|
|
|
}
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
/// Bring start and end points in the correct order.
|
2020-01-20 18:56:10 -05:00
|
|
|
fn points_need_swap(start: Point<usize>, end: Point<usize>) -> bool {
|
2019-11-03 15:59:28 -05:00
|
|
|
start.line < end.line || start.line == end.line && start.col > end.col
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Clamp selection inside grid to prevent OOB.
|
2019-06-20 11:56:09 -04:00
|
|
|
fn grid_clamp(
|
2020-01-20 18:56:10 -05:00
|
|
|
mut start: Anchor,
|
|
|
|
end: Anchor,
|
|
|
|
is_block: bool,
|
|
|
|
lines: usize,
|
|
|
|
) -> Result<(Anchor, Anchor), ()> {
|
2020-05-05 18:50:23 -04:00
|
|
|
// Clamp selection inside of grid to prevent OOB.
|
2020-01-20 18:56:10 -05:00
|
|
|
if start.point.line >= lines {
|
2020-05-05 18:50:23 -04:00
|
|
|
// Remove selection if it is fully out of the grid.
|
2020-01-20 18:56:10 -05:00
|
|
|
if end.point.line >= lines {
|
|
|
|
return Err(());
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Clamp to grid if it is still partially visible.
|
2020-01-20 18:56:10 -05:00
|
|
|
if !is_block {
|
|
|
|
start.side = Side::Left;
|
|
|
|
start.point.col = Column(0);
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
2020-01-20 18:56:10 -05:00
|
|
|
start.point.line = lines - 1;
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
Ok((start, end))
|
2018-10-22 15:39:26 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
fn range_semantic<T>(
|
|
|
|
term: &Term<T>,
|
|
|
|
mut start: Point<usize>,
|
|
|
|
mut end: Point<usize>,
|
|
|
|
) -> Option<SelectionRange> {
|
|
|
|
if start == end {
|
|
|
|
if let Some(matching) = term.bracket_search(start) {
|
|
|
|
if (matching.line == start.line && matching.col < start.col)
|
|
|
|
|| (matching.line > start.line)
|
|
|
|
{
|
|
|
|
start = matching;
|
|
|
|
} else {
|
|
|
|
end = matching;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Some(SelectionRange { start, end, is_block: false });
|
2019-05-11 12:15:32 -04:00
|
|
|
}
|
2020-01-20 18:56:10 -05:00
|
|
|
}
|
2017-06-16 00:43:28 -04:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
start = term.semantic_search_left(start);
|
|
|
|
end = term.semantic_search_right(end);
|
|
|
|
|
|
|
|
Some(SelectionRange { start, end, is_block: false })
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
fn range_lines<T>(
|
|
|
|
term: &Term<T>,
|
|
|
|
mut start: Point<usize>,
|
|
|
|
mut end: Point<usize>,
|
|
|
|
) -> Option<SelectionRange> {
|
|
|
|
start = term.line_search_left(start);
|
|
|
|
end = term.line_search_right(end);
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
Some(SelectionRange { start, end, is_block: false })
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
fn range_simple(
|
2019-06-20 11:56:09 -04:00
|
|
|
&self,
|
2020-01-20 18:56:10 -05:00
|
|
|
mut start: Anchor,
|
|
|
|
mut end: Anchor,
|
|
|
|
num_cols: Column,
|
|
|
|
) -> Option<SelectionRange> {
|
2019-06-20 11:56:09 -04:00
|
|
|
if self.is_empty() {
|
2018-03-25 15:09:18 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Remove last cell if selection ends to the left of a cell.
|
2020-01-20 18:56:10 -05:00
|
|
|
if end.side == Side::Left && start.point != end.point {
|
2020-05-05 18:50:23 -04:00
|
|
|
// Special case when selection ends to left of first cell.
|
2020-01-20 18:56:10 -05:00
|
|
|
if end.point.col == Column(0) {
|
2020-01-21 16:17:25 -05:00
|
|
|
end.point.col = num_cols - 1;
|
2020-01-20 18:56:10 -05:00
|
|
|
end.point.line += 1;
|
2018-07-21 13:17:41 -04:00
|
|
|
} else {
|
2020-01-20 18:56:10 -05:00
|
|
|
end.point.col -= 1;
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Remove first cell if selection starts at the right of a cell.
|
2020-01-20 18:56:10 -05:00
|
|
|
if start.side == Side::Right && start.point != end.point {
|
|
|
|
start.point.col += 1;
|
2020-03-11 20:14:00 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Wrap to next line when selection starts to the right of last column.
|
2020-03-11 20:14:00 -04:00
|
|
|
if start.point.col == num_cols {
|
|
|
|
start.point = Point::new(start.point.line.saturating_sub(1), Column(0));
|
|
|
|
}
|
2017-06-16 00:43:28 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
Some(SelectionRange { start: start.point, end: end.point, is_block: false })
|
2019-06-06 09:04:12 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
fn range_block(&self, mut start: Anchor, mut end: Anchor) -> Option<SelectionRange> {
|
2019-06-20 11:56:09 -04:00
|
|
|
if self.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Always go top-left -> bottom-right.
|
2020-01-20 18:56:10 -05:00
|
|
|
if start.point.col > end.point.col {
|
|
|
|
mem::swap(&mut start.side, &mut end.side);
|
|
|
|
mem::swap(&mut start.point.col, &mut end.point.col);
|
2018-10-20 18:30:59 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Remove last cell if selection ends to the left of a cell.
|
2020-01-20 18:56:10 -05:00
|
|
|
if end.side == Side::Left && start.point != end.point && end.point.col.0 > 0 {
|
|
|
|
end.point.col -= 1;
|
2019-06-20 11:56:09 -04:00
|
|
|
}
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-05 18:50:23 -04:00
|
|
|
// Remove first cell if selection starts at the right of a cell.
|
2020-01-20 18:56:10 -05:00
|
|
|
if start.side == Side::Right && start.point != end.point {
|
|
|
|
start.point.col += 1;
|
2018-10-20 18:30:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
Some(SelectionRange { start: start.point, end: end.point, is_block: true })
|
2018-10-20 18:30:59 -04:00
|
|
|
}
|
2018-03-06 23:57:40 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Tests for selection.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// There are comments on all of the tests describing the selection. Pictograms
|
|
|
|
/// are used to avoid ambiguity. Grid cells are represented by a [ ]. Only
|
2017-10-30 11:03:58 -04:00
|
|
|
/// cells that are completely covered are counted in a selection. Ends are
|
2016-12-22 13:43:06 -05:00
|
|
|
/// represented by `B` and `E` for begin and end, respectively. A selected cell
|
|
|
|
/// looks like [XX], [BX] (at the start), [XB] (at the end), [XE] (at the end),
|
|
|
|
/// and [EX] (at the start), or [BE] for a single cell. Partially selected cells
|
|
|
|
/// look like [ B] and [E ].
|
|
|
|
#[cfg(test)]
|
2020-01-28 07:32:35 -05:00
|
|
|
mod tests {
|
2020-03-17 22:35:08 -04:00
|
|
|
use super::*;
|
|
|
|
|
2019-10-04 20:29:26 -04:00
|
|
|
use crate::config::MockConfig;
|
|
|
|
use crate::event::{Event, EventListener};
|
2019-04-29 10:33:25 -04:00
|
|
|
use crate::index::{Column, Line, Point, Side};
|
|
|
|
use crate::term::{SizeInfo, Term};
|
|
|
|
|
2019-10-04 20:29:26 -04:00
|
|
|
struct Mock;
|
|
|
|
impl EventListener for Mock {
|
|
|
|
fn send_event(&self, _event: Event) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn term(width: usize, height: usize) -> Term<Mock> {
|
2019-04-29 10:33:25 -04:00
|
|
|
let size = SizeInfo {
|
|
|
|
width: width as f32,
|
|
|
|
height: height as f32,
|
|
|
|
cell_width: 1.0,
|
|
|
|
cell_height: 1.0,
|
|
|
|
padding_x: 0.0,
|
|
|
|
padding_y: 0.0,
|
|
|
|
dpr: 1.0,
|
|
|
|
};
|
2020-06-06 17:33:20 -04:00
|
|
|
Term::new(&MockConfig::default(), &size, Mock)
|
2017-06-17 13:29:59 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test case of single cell selection.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ]
|
|
|
|
/// 2. [B ]
|
|
|
|
/// 3. [BE]
|
|
|
|
#[test]
|
|
|
|
fn single_cell_left_to_right() {
|
2018-03-06 23:57:40 -05:00
|
|
|
let location = Point { line: 0, col: Column(0) };
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection = Selection::new(SelectionType::Simple, location, Side::Left);
|
2016-12-22 13:43:06 -05:00
|
|
|
selection.update(location, Side::Right);
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(1, 1)).unwrap(), SelectionRange {
|
2019-04-29 10:33:25 -04:00
|
|
|
start: location,
|
2019-06-20 11:56:09 -04:00
|
|
|
end: location,
|
2020-01-09 18:06:41 -05:00
|
|
|
is_block: false
|
2016-12-22 13:43:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test case of single cell selection.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ]
|
|
|
|
/// 2. [ B]
|
|
|
|
/// 3. [EB]
|
|
|
|
#[test]
|
|
|
|
fn single_cell_right_to_left() {
|
2018-03-06 23:57:40 -05:00
|
|
|
let location = Point { line: 0, col: Column(0) };
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection = Selection::new(SelectionType::Simple, location, Side::Right);
|
2016-12-22 13:43:06 -05:00
|
|
|
selection.update(location, Side::Left);
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(1, 1)).unwrap(), SelectionRange {
|
2019-04-29 10:33:25 -04:00
|
|
|
start: location,
|
2019-06-20 11:56:09 -04:00
|
|
|
end: location,
|
2020-01-09 18:06:41 -05:00
|
|
|
is_block: false
|
2016-12-22 13:43:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test adjacent cell selection from left to right.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ][ ]
|
|
|
|
/// 2. [ B][ ]
|
|
|
|
/// 3. [ B][E ]
|
|
|
|
#[test]
|
|
|
|
fn between_adjacent_cells_left_to_right() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(0, Column(0)), Side::Right);
|
2018-03-06 23:57:40 -05:00
|
|
|
selection.update(Point::new(0, Column(1)), Side::Left);
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(2, 1)), None);
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test adjacent cell selection from right to left.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ][ ]
|
|
|
|
/// 2. [ ][B ]
|
|
|
|
/// 3. [ E][B ]
|
|
|
|
#[test]
|
|
|
|
fn between_adjacent_cells_right_to_left() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(0, Column(1)), Side::Left);
|
2018-03-06 23:57:40 -05:00
|
|
|
selection.update(Point::new(0, Column(0)), Side::Right);
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(2, 1)), None);
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test selection across adjacent lines.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ][ ][ ][ ][ ]
|
|
|
|
/// [ ][ ][ ][ ][ ]
|
2018-03-25 15:09:18 -04:00
|
|
|
/// 2. [ ][ B][ ][ ][ ]
|
|
|
|
/// [ ][ ][ ][ ][ ]
|
|
|
|
/// 3. [ ][ B][XX][XX][XX]
|
|
|
|
/// [XX][XE][ ][ ][ ]
|
2016-12-22 13:43:06 -05:00
|
|
|
#[test]
|
|
|
|
fn across_adjacent_lines_upward_final_cell_exclusive() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(1, Column(1)), Side::Right);
|
2018-03-06 23:57:40 -05:00
|
|
|
selection.update(Point::new(0, Column(1)), Side::Right);
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(5, 2)).unwrap(), SelectionRange {
|
2019-11-03 15:59:28 -05:00
|
|
|
start: Point::new(1, Column(2)),
|
|
|
|
end: Point::new(0, Column(1)),
|
2019-06-20 11:56:09 -04:00
|
|
|
is_block: false,
|
2016-12-22 13:43:06 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
/// Test selection across adjacent lines.
|
2016-12-22 13:43:06 -05:00
|
|
|
///
|
|
|
|
/// 1. [ ][ ][ ][ ][ ]
|
|
|
|
/// [ ][ ][ ][ ][ ]
|
2018-03-25 15:09:18 -04:00
|
|
|
/// 2. [ ][ ][ ][ ][ ]
|
|
|
|
/// [ ][ B][ ][ ][ ]
|
|
|
|
/// 3. [ ][ E][XX][XX][XX]
|
|
|
|
/// [XX][XB][ ][ ][ ]
|
|
|
|
/// 4. [ E][XX][XX][XX][XX]
|
|
|
|
/// [XX][XB][ ][ ][ ]
|
2016-12-22 13:43:06 -05:00
|
|
|
#[test]
|
|
|
|
fn selection_bigger_then_smaller() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(0, Column(1)), Side::Right);
|
2018-03-06 23:57:40 -05:00
|
|
|
selection.update(Point::new(1, Column(1)), Side::Right);
|
|
|
|
selection.update(Point::new(1, Column(0)), Side::Right);
|
2016-12-22 13:43:06 -05:00
|
|
|
|
2020-01-20 18:56:10 -05:00
|
|
|
assert_eq!(selection.to_range(&term(5, 2)).unwrap(), SelectionRange {
|
2019-11-03 15:59:28 -05:00
|
|
|
start: Point::new(1, Column(1)),
|
|
|
|
end: Point::new(0, Column(1)),
|
2019-06-20 11:56:09 -04:00
|
|
|
is_block: false,
|
2016-12-22 13:43:06 -05:00
|
|
|
});
|
|
|
|
}
|
2018-10-20 18:30:59 -04:00
|
|
|
|
|
|
|
#[test]
|
2019-06-20 11:56:09 -04:00
|
|
|
fn line_selection() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Lines, Point::new(0, Column(1)), Side::Left);
|
2020-01-20 18:56:10 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(0)..num_lines), 7).unwrap();
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-20 18:56:10 -05:00
|
|
|
start: Point::new(9, Column(0)),
|
|
|
|
end: Point::new(7, Column(4)),
|
2019-06-20 11:56:09 -04:00
|
|
|
is_block: false,
|
2018-10-20 18:30:59 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-06-20 11:56:09 -04:00
|
|
|
fn semantic_selection() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Semantic, Point::new(0, Column(3)), Side::Left);
|
2020-01-20 18:56:10 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(0)..num_lines), 7).unwrap();
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-20 18:56:10 -05:00
|
|
|
start: Point::new(9, Column(0)),
|
|
|
|
end: Point::new(7, Column(3)),
|
2019-06-20 11:56:09 -04:00
|
|
|
is_block: false,
|
2018-10-20 18:30:59 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-06-20 11:56:09 -04:00
|
|
|
fn simple_selection() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(0, Column(3)), Side::Right);
|
2020-01-20 18:56:10 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(0)..num_lines), 7).unwrap();
|
2018-10-20 18:30:59 -04:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-20 18:56:10 -05:00
|
|
|
start: Point::new(9, Column(0)),
|
|
|
|
end: Point::new(7, Column(3)),
|
2019-06-20 11:56:09 -04:00
|
|
|
is_block: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_selection() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Block, Point::new(0, Column(3)), Side::Right);
|
2020-01-20 18:56:10 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(0)..num_lines), 7).unwrap();
|
2019-06-20 11:56:09 -04:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-20 18:56:10 -05:00
|
|
|
start: Point::new(9, Column(2)),
|
|
|
|
end: Point::new(7, Column(3)),
|
2020-01-09 18:06:41 -05:00
|
|
|
is_block: true
|
2019-04-29 10:33:25 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:51:14 -04:00
|
|
|
#[test]
|
|
|
|
fn simple_is_empty() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(0, Column(0)), Side::Right);
|
2019-09-13 19:51:14 -04:00
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(0, Column(1)), Side::Left);
|
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(1, Column(0)), Side::Right);
|
|
|
|
assert!(!selection.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn block_is_empty() {
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Block, Point::new(0, Column(0)), Side::Right);
|
2019-09-13 19:51:14 -04:00
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(0, Column(1)), Side::Left);
|
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(0, Column(1)), Side::Right);
|
|
|
|
assert!(!selection.is_empty());
|
|
|
|
selection.update(Point::new(1, Column(0)), Side::Right);
|
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(1, Column(1)), Side::Left);
|
|
|
|
assert!(selection.is_empty());
|
|
|
|
selection.update(Point::new(1, Column(1)), Side::Right);
|
|
|
|
assert!(!selection.is_empty());
|
|
|
|
}
|
2020-01-24 17:57:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rotate_in_region_up() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(2, Column(3)), Side::Right);
|
2020-01-24 17:57:22 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(1)..(num_lines - 1)), 4).unwrap();
|
2020-01-24 17:57:22 -05:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-24 17:57:22 -05:00
|
|
|
start: Point::new(8, Column(0)),
|
|
|
|
end: Point::new(6, Column(3)),
|
|
|
|
is_block: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rotate_in_region_down() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Simple, Point::new(5, Column(3)), Side::Right);
|
2020-01-24 17:57:22 -05:00
|
|
|
selection.update(Point::new(8, Column(1)), Side::Left);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(1)..(num_lines - 1)), -5).unwrap();
|
2020-01-24 17:57:22 -05:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-24 17:57:22 -05:00
|
|
|
start: Point::new(3, Column(1)),
|
2020-05-30 16:45:44 -04:00
|
|
|
end: Point::new(1, num_cols - 1),
|
2020-01-24 17:57:22 -05:00
|
|
|
is_block: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rotate_in_region_up_block() {
|
2020-05-30 16:45:44 -04:00
|
|
|
let num_lines = Line(10);
|
|
|
|
let num_cols = Column(5);
|
2020-03-17 22:35:08 -04:00
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Block, Point::new(2, Column(3)), Side::Right);
|
2020-01-24 17:57:22 -05:00
|
|
|
selection.update(Point::new(5, Column(1)), Side::Right);
|
2020-05-30 16:45:44 -04:00
|
|
|
selection = selection.rotate(num_lines, num_cols, &(Line(1)..(num_lines - 1)), 4).unwrap();
|
2020-01-24 17:57:22 -05:00
|
|
|
|
2020-05-30 16:45:44 -04:00
|
|
|
assert_eq!(selection.to_range(&term(num_cols.0, num_lines.0)).unwrap(), SelectionRange {
|
2020-01-24 17:57:22 -05:00
|
|
|
start: Point::new(8, Column(2)),
|
|
|
|
end: Point::new(6, Column(3)),
|
|
|
|
is_block: true,
|
|
|
|
});
|
|
|
|
}
|
2020-06-26 12:04:55 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn range_intersection() {
|
|
|
|
let mut selection =
|
|
|
|
Selection::new(SelectionType::Lines, Point::new(6, Column(1)), Side::Left);
|
|
|
|
selection.update(Point::new(3, Column(1)), Side::Right);
|
|
|
|
|
|
|
|
assert!(selection.intersects_range(..));
|
|
|
|
assert!(selection.intersects_range(2..));
|
|
|
|
assert!(selection.intersects_range(2..=4));
|
|
|
|
assert!(selection.intersects_range(2..=7));
|
|
|
|
assert!(selection.intersects_range(4..=5));
|
|
|
|
assert!(selection.intersects_range(5..8));
|
|
|
|
|
|
|
|
assert!(!selection.intersects_range(..=2));
|
|
|
|
assert!(!selection.intersects_range(7..=8));
|
|
|
|
}
|
2016-12-22 13:43:06 -05:00
|
|
|
}
|