use std::convert::TryFrom;
use std::mem;
use std::ops::Range;
use crate::index::{Column, Line, Point, Side};
use crate::term::cell::Flags;
use crate::term::{Search, Term};
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Anchor {
point: Point<usize>,
side: Side,
}
impl Anchor {
fn new(point: Point<usize>, side: Side) -> Anchor {
Anchor { point, side }
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct SelectionRange<L = usize> {
pub start: Point<L>,
pub end: Point<L>,
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))
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
enum SelectionType {
Simple,
Block,
Semantic,
Lines,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Selection {
region: Range<Anchor>,
ty: SelectionType,
}
impl Selection {
pub fn simple(location: Point<usize>, side: Side) -> Selection {
Self {
region: Range { start: Anchor::new(location, side), end: Anchor::new(location, side) },
ty: SelectionType::Simple,
}
}
pub fn block(location: Point<usize>, side: Side) -> Selection {
Self {
region: Range { start: Anchor::new(location, side), end: Anchor::new(location, side) },
ty: SelectionType::Block,
}
}
pub fn semantic(location: Point<usize>) -> Selection {
Self {
region: Range {
start: Anchor::new(location, Side::Left),
end: Anchor::new(location, Side::Right),
},
ty: SelectionType::Semantic,
}
}
pub fn lines(location: Point<usize>) -> Selection {
Self {
region: Range {
start: Anchor::new(location, Side::Left),
end: Anchor::new(location, Side::Right),
},
ty: SelectionType::Lines,
}
}
pub fn update(&mut self, location: Point<usize>, side: Side) {
self.region.end.point = location;
self.region.end.side = side;
}
pub fn rotate(
mut self,
num_lines: usize,
num_cols: usize,
scrolling_region: &Range<Line>,
offset: isize,
) -> Option<Selection> {
let region_start = num_lines - scrolling_region.start.0;
let region_end = num_lines - scrolling_region.end.0;
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);
}
if (start.point.line < region_start || region_start == num_lines)
&& start.point.line >= region_end
{
start.point.line = usize::try_from(start.point.line as isize + offset).unwrap_or(0);
if start.point.line < region_end && end.point.line >= region_end {
return None;
}
if start.point.line >= region_start && region_start != num_lines {
if self.ty != SelectionType::Block {
start.point.col = Column(0);
start.side = Side::Left;
}
start.point.line = region_start - 1;
}
}
if (end.point.line < region_start || region_start == num_lines)
&& end.point.line >= region_end
{
end.point.line = usize::try_from(end.point.line as isize + offset).unwrap_or(0);
if end.point.line > start.point.line {
return None;
}
if end.point.line < region_end {
if self.ty != SelectionType::Block {
end.point.col = Column(num_cols - 1);
end.side = Side::Right;
}
end.point.line = region_end;
}
}
Some(self)
}
pub fn is_empty(&self) -> bool {
match self.ty {
SelectionType::Simple => {
let (mut start, mut end) = (self.region.start, self.region.end);
if Selection::points_need_swap(start.point, end.point) {
mem::swap(&mut start, &mut end);
}
start == end
|| (start.side == Side::Right
&& end.side == Side::Left
&& (start.point.line == end.point.line)
&& start.point.col + 1 == end.point.col)
}
SelectionType::Block => {
let (start, end) = (self.region.start, self.region.end);
(start.point.col == end.point.col && start.side == end.side)
|| (start.point.col + 1 == end.point.col
&& start.side == Side::Right
&& end.side == Side::Left)
|| (end.point.col + 1 == start.point.col
&& start.side == Side::Left
&& end.side == Side::Right)
}
SelectionType::Semantic | SelectionType::Lines => false,
}
}
pub fn to_range<T>(&self, term: &Term<T>) -> Option<SelectionRange> {
let grid = term.grid();
let num_cols = grid.num_cols();
let (mut start, mut end) = (self.region.start, self.region.end);
if Self::points_need_swap(start.point, end.point) {
mem::swap(&mut start, &mut end);
}
let is_block = self.ty == SelectionType::Block;
let (start, end) = Self::grid_clamp(start, end, is_block, grid.len()).ok()?;
let range = match self.ty {
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),
};
range.map(|range| Self::range_expand_fullwidth(term, range))
}
fn range_expand_fullwidth<T>(term: &Term<T>, mut range: SelectionRange) -> SelectionRange {
let grid = term.grid();
let num_cols = grid.num_cols();
let flag_at = |point: Point<usize>, flag: Flags| -> bool {
grid[point.line][point.col].flags.contains(flag)
};
if range.start.col < num_cols {
if range.start.line + 1 != grid.len() || range.start.col.0 != 0 {
let prev = range.start.sub(num_cols.0, 1, true);
if flag_at(range.start, Flags::WIDE_CHAR_SPACER) && flag_at(prev, Flags::WIDE_CHAR)
{
range.start = prev;
}
}
if range.start.line + 1 != grid.len() || range.start.col.0 != 0 {
let prev = range.start.sub(num_cols.0, 1, true);
if (prev.line + 1 != grid.len() || prev.col.0 != 0)
&& flag_at(prev, Flags::WIDE_CHAR_SPACER)
&& !flag_at(prev.sub(num_cols.0, 1, true), Flags::WIDE_CHAR)
{
range.start = prev;
}
}
}
if range.end.line != 0 || range.end.col < num_cols {
if (range.end.line + 1 != grid.len() || range.end.col.0 != 0)
&& flag_at(range.end, Flags::WIDE_CHAR_SPACER)
&& !flag_at(range.end.sub(num_cols.0, 1, true), Flags::WIDE_CHAR)
{
range.end = range.end.add(num_cols.0, 1, true);
}
if flag_at(range.end, Flags::WIDE_CHAR) {
range.end = range.end.add(num_cols.0, 1, true);
}
}
range
}
fn points_need_swap(start: Point<usize>, end: Point<usize>) -> bool {
start.line < end.line || start.line == end.line && start.col > end.col
}
fn grid_clamp(
mut start: Anchor,
end: Anchor,
is_block: bool,
lines: usize,
) -> Result<(Anchor, Anchor), ()> {
if start.point.line >= lines {
if end.point.line >= lines {
return Err(());
}
if !is_block {
start.side = Side::Left;
start.point.col = Column(0);
}
start.point.line = lines - 1;
}
Ok((start, end))
}
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 });
}
}
start = term.semantic_search_left(start);
end = term.semantic_search_right(end);
Some(SelectionRange { start, end, is_block: false })
}
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);
Some(SelectionRange { start, end, is_block: false })
}
fn range_simple(
&self,
mut start: Anchor,
mut end: Anchor,
num_cols: Column,
) -> Option<SelectionRange> {
if self.is_empty() {
return None;
}
if end.side == Side::Left && start.point != end.point {
if end.point.col == Column(0) {
end.point.col = num_cols - 1;
end.point.line += 1;
} else {
end.point.col -= 1;
}
}
if start.side == Side::Right && start.point != end.point {
start.point.col += 1;
}
Some(SelectionRange { start: start.point, end: end.point, is_block: false })
}
fn range_block(&self, mut start: Anchor, mut end: Anchor) -> Option<SelectionRange> {
if self.is_empty() {
return None;
}
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);
}
if end.side == Side::Left && start.point != end.point && end.point.col.0 > 0 {
end.point.col -= 1;
}
if start.side == Side::Right && start.point != end.point {
start.point.col += 1;
}
Some(SelectionRange { start: start.point, end: end.point, is_block: true })
}
}
#[cfg(test)]
mod test {
use std::mem;
use super::{Selection, SelectionRange};
use crate::clipboard::Clipboard;
use crate::config::MockConfig;
use crate::event::{Event, EventListener};
use crate::grid::Grid;
use crate::index::{Column, Line, Point, Side};
use crate::term::cell::{Cell, Flags};
use crate::term::{SizeInfo, Term};
struct Mock;
impl EventListener for Mock {
fn send_event(&self, _event: Event) {}
}
fn term(width: usize, height: usize) -> Term<Mock> {
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,
};
Term::new(&MockConfig::default(), &size, Clipboard::new_nop(), Mock)
}
#[test]
fn single_cell_left_to_right() {
let location = Point { line: 0, col: Column(0) };
let mut selection = Selection::simple(location, Side::Left);
selection.update(location, Side::Right);
assert_eq!(
selection.to_range(&term(1, 1)).unwrap(),
SelectionRange { start: location, end: location, is_block: false }
);
}
#[test]
fn single_cell_right_to_left() {
let location = Point { line: 0, col: Column(0) };
let mut selection = Selection::simple(location, Side::Right);
selection.update(location, Side::Left);
assert_eq!(
selection.to_range(&term(1, 1)).unwrap(),
SelectionRange { start: location, end: location, is_block: false }
);
}
#[test]
fn between_adjacent_cells_left_to_right() {
let mut selection = Selection::simple(Point::new(0, Column(0)), Side::Right);
selection.update(Point::new(0, Column(1)), Side::Left);
assert_eq!(selection.to_range(&term(2, 1)), None);
}
#[test]
fn between_adjacent_cells_right_to_left() {
let mut selection = Selection::simple(Point::new(0, Column(1)), Side::Left);
selection.update(Point::new(0, Column(0)), Side::Right);
assert_eq!(selection.to_range(&term(2, 1)), None);
}
#[test]
fn across_adjacent_lines_upward_final_cell_exclusive() {
let mut selection = Selection::simple(Point::new(1, Column(1)), Side::Right);
selection.update(Point::new(0, Column(1)), Side::Right);
assert_eq!(
selection.to_range(&term(5, 2)).unwrap(),
SelectionRange {
start: Point::new(1, Column(2)),
end: Point::new(0, Column(1)),
is_block: false,
}
);
}
#[test]
fn selection_bigger_then_smaller() {
let mut selection = Selection::simple(Point::new(0, Column(1)), Side::Right);
selection.update(Point::new(1, Column(1)), Side::Right);
selection.update(Point::new(1, Column(0)), Side::Right);
assert_eq!(
selection.to_range(&term(5, 2)).unwrap(),
SelectionRange {
start: Point::new(1, Column(1)),
end: Point::new(0, Column(1)),
is_block: false,
}
);
}
#[test]
fn line_selection() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::lines(Point::new(0, Column(1)));
selection.update(Point::new(5, Column(1)), Side::Right);
selection = selection.rotate(num_lines, num_cols, &(Line(0)..Line(num_lines)), 7).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(9, Column(0)),
end: Point::new(7, Column(4)),
is_block: false,
}
);
}
#[test]
fn semantic_selection() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::semantic(Point::new(0, Column(3)));
selection.update(Point::new(5, Column(1)), Side::Right);
selection = selection.rotate(num_lines, num_cols, &(Line(0)..Line(num_lines)), 7).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(9, Column(0)),
end: Point::new(7, Column(3)),
is_block: false,
}
);
}
#[test]
fn simple_selection() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::simple(Point::new(0, Column(3)), Side::Right);
selection.update(Point::new(5, Column(1)), Side::Right);
selection = selection.rotate(num_lines, num_cols, &(Line(0)..Line(num_lines)), 7).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(9, Column(0)),
end: Point::new(7, Column(3)),
is_block: false,
}
);
}
#[test]
fn block_selection() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::block(Point::new(0, Column(3)), Side::Right);
selection.update(Point::new(5, Column(1)), Side::Right);
selection = selection.rotate(num_lines, num_cols, &(Line(0)..Line(num_lines)), 7).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(9, Column(2)),
end: Point::new(7, Column(3)),
is_block: true
}
);
}
#[test]
fn double_width_expansion() {
let mut term = term(10, 1);
let mut grid = Grid::new(Line(1), Column(10), 0, Cell::default());
grid[Line(0)][Column(0)].flags.insert(Flags::WIDE_CHAR);
grid[Line(0)][Column(1)].flags.insert(Flags::WIDE_CHAR_SPACER);
grid[Line(0)][Column(8)].flags.insert(Flags::WIDE_CHAR);
grid[Line(0)][Column(9)].flags.insert(Flags::WIDE_CHAR_SPACER);
mem::swap(term.grid_mut(), &mut grid);
let mut selection = Selection::simple(Point::new(0, Column(1)), Side::Left);
selection.update(Point::new(0, Column(8)), Side::Right);
assert_eq!(
selection.to_range(&term).unwrap(),
SelectionRange {
start: Point::new(0, Column(0)),
end: Point::new(0, Column(9)),
is_block: false,
}
);
}
#[test]
fn simple_is_empty() {
let mut selection = Selection::simple(Point::new(0, Column(0)), Side::Right);
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() {
let mut selection = Selection::block(Point::new(0, Column(0)), Side::Right);
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());
}
#[test]
fn rotate_in_region_up() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::simple(Point::new(2, Column(3)), Side::Right);
selection.update(Point::new(5, Column(1)), Side::Right);
selection =
selection.rotate(num_lines, num_cols, &(Line(1)..Line(num_lines - 1)), 4).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(8, Column(0)),
end: Point::new(6, Column(3)),
is_block: false,
}
);
}
#[test]
fn rotate_in_region_down() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::simple(Point::new(5, Column(3)), Side::Right);
selection.update(Point::new(8, Column(1)), Side::Left);
selection =
selection.rotate(num_lines, num_cols, &(Line(1)..Line(num_lines - 1)), -5).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(3, Column(1)),
end: Point::new(1, Column(num_cols - 1)),
is_block: false,
}
);
}
#[test]
fn rotate_in_region_up_block() {
let num_lines = 10;
let num_cols = 5;
let mut selection = Selection::block(Point::new(2, Column(3)), Side::Right);
selection.update(Point::new(5, Column(1)), Side::Right);
selection =
selection.rotate(num_lines, num_cols, &(Line(1)..Line(num_lines - 1)), 4).unwrap();
assert_eq!(
selection.to_range(&term(num_cols, num_lines)).unwrap(),
SelectionRange {
start: Point::new(8, Column(2)),
end: Point::new(6, Column(3)),
is_block: true,
}
);
}
}