From f986e0c6d898f412092cffe05cdec55b87008e59 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 28 Feb 2018 22:07:48 -0800 Subject: [PATCH] Properly check the range intersection for invalidating selection --- term/src/lib.rs | 9 +++++++++ term/src/terminalstate.rs | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/term/src/lib.rs b/term/src/lib.rs index 572dc0256..6fc9ce6c9 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -79,6 +79,15 @@ pub fn in_range(value: T, range: &Range) -> bool { value >= range.start && value < range.end } +/// Returns true if r1 intersects r2 +pub fn intersects_range(r1: Range, r2: Range) -> bool { + use std::cmp::{max, min}; + let start = max(r1.start, r2.start); + let end = min(r1.end, r2.end); + + end > start +} + /// Position allows referring to an absolute visible row number /// or a position relative to some existing row number (typically /// where the cursor is located). Both of the cases are represented diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 46e317c28..7d65df401 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -213,7 +213,7 @@ impl TerminalState { match sel { Some(sel) => { let sel_cols = sel.cols_for_row(row); - if cols.start >= sel_cols.start && cols.end <= sel_cols.end { + if intersects_range(cols, sel_cols) { // Intersects, so clear the selection self.clear_selection(); true @@ -239,7 +239,7 @@ impl TerminalState { match sel { Some(sel) => { let sel_rows = sel.rows(); - if rows.start >= sel_rows.start && rows.end <= sel_rows.end { + if intersects_range(rows, sel_rows) { // Intersects, so clear the selection self.clear_selection(); true