From 5e49a79705893e2d883dbedce6d7ec8c0dcf44bd Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Feb 2018 18:35:58 -0800 Subject: [PATCH] fix a selection range issue When dragging a multi-line selection down and to the left, we were incorrectly setting the computed column range. Add a test and fix it. --- term/src/selection.rs | 15 +++++++-------- term/src/test/selection.rs | 3 +++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/term/src/selection.rs b/term/src/selection.rs index 331be6537..419c48441 100644 --- a/term/src/selection.rs +++ b/term/src/selection.rs @@ -61,11 +61,6 @@ impl SelectionRange { /// more precise than that. /// Must be called on a normalized range! pub fn cols_for_row(&self, row: ScrollbackOrVisibleRowIndex) -> Range { - let (x1, x2) = if self.start.x <= self.end.x { - (self.start.x, self.end.x.saturating_add(1)) - } else { - (self.end.x, self.start.x.saturating_add(1)) - }; debug_assert!( self.start.y <= self.end.y, "you forgot to normalize a SelectionRange" @@ -74,13 +69,17 @@ impl SelectionRange { 0..0 } else if self.start.y == self.end.y { // A single line selection - x1..x2 + if self.start.x <= self.end.x { + self.start.x..self.end.x.saturating_add(1) + } else { + self.end.x..self.start.x.saturating_add(1) + } } else if row == self.end.y { // last line of multi-line - 0..x2 + 0..self.end.x.saturating_add(1) } else if row == self.start.y { // first line of multi-line - x1..usize::max_value() + self.start.x..usize::max_value() } else { // some "middle" line of multi-line 0..usize::max_value() diff --git a/term/src/test/selection.rs b/term/src/test/selection.rs index 6d43d0da3..4c1e4cacf 100644 --- a/term/src/test/selection.rs +++ b/term/src/test/selection.rs @@ -36,6 +36,9 @@ fn drag_selection() { term.get_clipboard().unwrap(), "hello world\n\u{1F480}skull\n" ); + + term.drag_select(6, 0, 3, 1); + assert_eq!(term.get_clipboard().unwrap(), "world\n\u{1F480}sk"); } /// Test double click to select a word