From a672f7d553ac17d5aaef8dc667dee31d5815677d Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 19 Mar 2019 19:14:17 +0000 Subject: Add URL hover highlighting This changes the cursor whenever it moves to a cell which contains part of a URL. When a URL is hovered over, all characters that are recognized as part of the URL will be underlined and the mouse cursor shape will be changed. After the cursor leaves the URL, the previous hover state is restored. This also changes the behavior when clicking an illegal character right in front of a URL. Previously this would still launch the URL, but strip the illegal character. Now these clicks are ignored to make sure there's no mismatch between underline and legal URL click positions --- src/grid/mod.rs | 60 +++++++++++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 38 deletions(-) (limited to 'src/grid/mod.rs') diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 837d9b0e..c555400d 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -17,7 +17,7 @@ use std::cmp::{min, max, Ordering}; use std::ops::{Deref, Range, Index, IndexMut, RangeTo, RangeFrom, RangeFull}; -use crate::index::{self, Point, Line, Column, IndexRange}; +use crate::index::{self, Point, Line, Column, IndexRange, PointIterator}; use crate::selection::Selection; mod row; @@ -105,14 +105,6 @@ pub struct Grid { max_scroll_limit: usize, } -pub struct GridIterator<'a, T> { - /// Immutable grid reference - grid: &'a Grid, - - /// Current position of the iterator within the grid. - pub cur: Point, -} - #[derive(Copy, Clone)] pub enum Scroll { Lines(isize), @@ -587,7 +579,7 @@ impl Grid { pub fn iter_from(&self, point: Point) -> GridIterator<'_, T> { GridIterator { grid: self, - cur: point, + point_iter: point.iter(self.num_cols() - 1, self.len() - 1), } } @@ -602,43 +594,28 @@ impl Grid { } } +pub struct GridIterator<'a, T> { + point_iter: PointIterator, + grid: &'a Grid, +} + +impl<'a, T> GridIterator<'a, T> { + pub fn cur(&self) -> Point { + self.point_iter.cur + } +} + impl<'a, T> Iterator for GridIterator<'a, T> { type Item = &'a T; fn next(&mut self) -> Option { - let last_col = self.grid.num_cols() - Column(1); - match self.cur { - Point { line, col } if line == 0 && col == last_col => None, - Point { col, .. } if - (col == last_col) => { - self.cur.line -= 1; - self.cur.col = Column(0); - Some(&self.grid[self.cur.line][self.cur.col]) - }, - _ => { - self.cur.col += Column(1); - Some(&self.grid[self.cur.line][self.cur.col]) - } - } + self.point_iter.next().map(|p| &self.grid[p.line][p.col]) } } impl<'a, T> BidirectionalIterator for GridIterator<'a, T> { fn prev(&mut self) -> Option { - let num_cols = self.grid.num_cols(); - - match self.cur { - Point { line, col: Column(0) } if line == self.grid.len() - 1 => None, - Point { col: Column(0), .. } => { - self.cur.line += 1; - self.cur.col = num_cols - Column(1); - Some(&self.grid[self.cur.line][self.cur.col]) - }, - _ => { - self.cur.col -= Column(1); - Some(&self.grid[self.cur.line][self.cur.col]) - } - } + self.point_iter.prev().map(|p| &self.grid[p.line][p.col]) } } @@ -669,6 +646,13 @@ impl IndexMut for Grid { } } +impl IndexMut for Grid { + #[inline] + fn index_mut(&mut self, index: usize) -> &mut Row { + &mut self.raw[index] + } +} + impl<'point, T> Index<&'point Point> for Grid { type Output = T; -- cgit