diff options
author | Jean Mertz <jean@mertz.fm> | 2019-05-11 18:15:32 +0200 |
---|---|---|
committer | Christian Duerr <chrisduerr@users.noreply.github.com> | 2019-05-11 16:15:32 +0000 |
commit | 1656aff85ed7412fd6ab8a7f75384f86d98a483b (patch) | |
tree | 6c8f2202ef8ce19f6098dee2e3087bf4dc8cb8a3 /alacritty_terminal/src/term/mod.rs | |
parent | ca9724a5efe38a2db47d1b44b9d9f75cb6b6d20e (diff) | |
download | alacritty-1656aff85ed7412fd6ab8a7f75384f86d98a483b.tar.gz alacritty-1656aff85ed7412fd6ab8a7f75384f86d98a483b.zip |
Add support for double-click bracket-pair selection
Diffstat (limited to 'alacritty_terminal/src/term/mod.rs')
-rw-r--r-- | alacritty_terminal/src/term/mod.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs index de4d1325..c01c159d 100644 --- a/alacritty_terminal/src/term/mod.rs +++ b/alacritty_terminal/src/term/mod.rs @@ -46,6 +46,9 @@ use crate::tty; pub mod cell; pub mod color; +/// Used to match equal brackets, when performing a bracket-pair selection. +const BRACKET_PAIRS: [(char, char); 4] = [('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')]; + /// A type that can expand a given point to a region /// /// Usually this is implemented for some 2-D array type since @@ -57,6 +60,8 @@ pub trait Search { fn semantic_search_right(&self, _: Point<usize>) -> Point<usize>; /// Find the nearest URL boundary in both directions. fn url_search(&self, _: Point<usize>) -> Option<Url>; + /// Find the nearest matching bracket. + fn bracket_search(&self, _: Point<usize>) -> Option<Point<usize>>; } impl Search for Term { @@ -137,6 +142,49 @@ impl Search for Term { } url_parser.url() } + + fn bracket_search(&self, point: Point<usize>) -> Option<Point<usize>> { + let start_char = self.grid[point.line][point.col].c; + + // Find the matching bracket we're looking for + let (forwards, end_char) = BRACKET_PAIRS.iter().find_map(|(open, close)| { + if open == &start_char { + Some((true, *close)) + } else if close == &start_char { + Some((false, *open)) + } else { + None + } + })?; + + let mut iter = self.grid.iter_from(point); + + // For every character match that equals the starting bracket, we + // ignore one bracket of the opposite type. + let mut skip_pairs = 0; + + loop { + // Check the next cell + let cell = if forwards { iter.next() } else { iter.prev() }; + + // Break if there are no more cells + let c = match cell { + Some(cell) => cell.c, + None => break, + }; + + // Check if the bracket matches + if c == end_char && skip_pairs == 0 { + return Some(iter.cur); + } else if c == start_char { + skip_pairs += 1; + } else if c == end_char { + skip_pairs -= 1; + } + } + + None + } } impl selection::Dimensions for Term { |