From 91aa683bcd060b2ac2f621a388a6448f564d0537 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 30 Mar 2019 09:23:48 +0000 Subject: Rework URL highlighting This completely reworks URL highlighting to fix two issues which were caused by the original approach. The primary issues that were not straight-forward to resolve with the previous implementation were about handling the URL highlighted content moving while the highlight is active. This lead to issues with highlighting with scrolling and when the display offset was not 0. The new approach sticks closely to prior art done for the selection, where the selection is tracked on the grid and updated whenever the buffer is rotated. The truncation of URLs was incorrectly assuming input to be just a single codepoint wide to truncate the end of URLs with unmatching closing parenthesis. This is now handled properly using Rust's built-in Unicode support. This fixes #2231. This fixes #2225. --- src/grid/mod.rs | 57 +++++++++++++++++++++++++++++++++++++++++++------------ src/grid/tests.rs | 12 ++++++------ 2 files changed, 51 insertions(+), 18 deletions(-) (limited to 'src/grid') diff --git a/src/grid/mod.rs b/src/grid/mod.rs index 272ea340..a1903535 100644 --- a/src/grid/mod.rs +++ b/src/grid/mod.rs @@ -15,9 +15,9 @@ //! A specialized 2d grid implementation optimized for use in a terminal. use std::cmp::{min, max, Ordering}; -use std::ops::{Deref, Range, Index, IndexMut, RangeTo, RangeFrom, RangeFull}; +use std::ops::{Deref, Range, Index, IndexMut, RangeTo, RangeFrom, RangeFull, RangeInclusive}; -use crate::index::{self, Point, Line, Column, IndexRange, PointIterator}; +use crate::index::{self, Point, Line, Column, IndexRange}; use crate::selection::Selection; mod row; @@ -60,7 +60,8 @@ impl ::std::cmp::PartialEq for Grid { self.lines.eq(&other.lines) && self.display_offset.eq(&other.display_offset) && self.scroll_limit.eq(&other.scroll_limit) && - self.selection.eq(&other.selection) + self.selection.eq(&other.selection) && + self.url_highlight.eq(&other.url_highlight) } } @@ -103,6 +104,10 @@ pub struct Grid { #[serde(default)] max_scroll_limit: usize, + + /// Range for URL hover highlights + #[serde(default)] + pub url_highlight: Option>, } #[derive(Copy, Clone)] @@ -132,6 +137,7 @@ impl Grid { scroll_limit: 0, selection: None, max_scroll_limit: scrollback, + url_highlight: None, } } @@ -387,6 +393,7 @@ impl Grid { let prev = self.lines; self.selection = None; + self.url_highlight = None; self.raw.rotate(*prev as isize - *target as isize); self.raw.shrink_visible_lines(target); self.lines = target; @@ -422,6 +429,7 @@ impl Grid { if let Some(ref mut selection) = self.selection { selection.rotate(-(*positions as isize)); } + self.url_highlight = None; self.decrease_scroll_limit(*positions); @@ -473,6 +481,7 @@ impl Grid { if let Some(ref mut selection) = self.selection { selection.rotate(*positions as isize); } + self.url_highlight = None; // // This next loop swaps "fixed" lines outside of a scroll region // // back into place after the rotation. The work is done in buffer- @@ -517,6 +526,7 @@ impl Grid { self.display_offset = 0; self.selection = None; + self.url_highlight = None; } } @@ -573,7 +583,7 @@ impl Grid { pub fn iter_from(&self, point: Point) -> GridIterator<'_, T> { GridIterator { grid: self, - point_iter: point.iter(self.num_cols() - 1, self.len() - 1), + cur: point, } } @@ -589,27 +599,50 @@ impl Grid { } pub struct GridIterator<'a, T> { - point_iter: PointIterator, + /// Immutable grid reference grid: &'a Grid, -} -impl<'a, T> GridIterator<'a, T> { - pub fn cur(&self) -> Point { - self.point_iter.cur - } + /// Current position of the iterator within the grid. + pub cur: Point, } impl<'a, T> Iterator for GridIterator<'a, T> { type Item = &'a T; fn next(&mut self) -> Option { - self.point_iter.next().map(|p| &self.grid[p.line][p.col]) + 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]) + } + } } } impl<'a, T> BidirectionalIterator for GridIterator<'a, T> { fn prev(&mut self) -> Option { - self.point_iter.prev().map(|p| &self.grid[p.line][p.col]) + 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]) + } + } } } diff --git a/src/grid/tests.rs b/src/grid/tests.rs index 33d772a2..82edda69 100644 --- a/src/grid/tests.rs +++ b/src/grid/tests.rs @@ -112,8 +112,8 @@ fn test_iter() { assert_eq!(None, iter.prev()); assert_eq!(Some(&1), iter.next()); - assert_eq!(Column(1), iter.cur().col); - assert_eq!(4, iter.cur().line); + assert_eq!(Column(1), iter.cur.col); + assert_eq!(4, iter.cur.line); assert_eq!(Some(&2), iter.next()); assert_eq!(Some(&3), iter.next()); @@ -121,12 +121,12 @@ fn test_iter() { // test linewrapping assert_eq!(Some(&5), iter.next()); - assert_eq!(Column(0), iter.cur().col); - assert_eq!(3, iter.cur().line); + assert_eq!(Column(0), iter.cur.col); + assert_eq!(3, iter.cur.line); assert_eq!(Some(&4), iter.prev()); - assert_eq!(Column(4), iter.cur().col); - assert_eq!(4, iter.cur().line); + assert_eq!(Column(4), iter.cur.col); + assert_eq!(4, iter.cur.line); // test that iter ends at end of grid -- cgit