Skip to content

Commit

Permalink
Fix empty block selection detection
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduerr authored Sep 13, 2019
1 parent 1067fa6 commit fb37a9c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
45 changes: 42 additions & 3 deletions alacritty_terminal/src/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,34 @@ impl Selection {

pub fn is_empty(&self) -> bool {
match *self {
Selection::Simple { ref region } | Selection::Block { ref region } => {
Selection::Simple { ref region } => {
let (start, end) =
if Selection::points_need_swap(region.start.point, region.end.point) {
(&region.end, &region.start)
} else {
(&region.start, &region.end)
};

// Empty when single cell with identical sides or two cell with right+left sides
// Simple selection is empty when the points are identical
// or two adjacent cells have the sides right -> left
start == end
|| (start.side == Side::Left
&& end.side == Side::Right
&& start.point.line == end.point.line
&& (start.point.line == end.point.line)
&& start.point.col == end.point.col + 1)
},
Selection::Block { region: Range { ref start, ref end } } => {
// Block selection is empty when the points' columns and sides are identical
// or two cells with adjacent columns have the sides right -> left,
// regardless of their lines
(start.point.col == end.point.col && start.side == end.side)
|| (start.point.col == end.point.col + 1
&& start.side == Side::Left
&& end.side == Side::Right)
|| (end.point.col == start.point.col + 1
&& end.side == Side::Left
&& start.side == Side::Right)
},
Selection::Semantic { .. } | Selection::Lines { .. } => false,
}
}
Expand Down Expand Up @@ -589,4 +602,30 @@ mod test {
is_block: false,
});
}

#[test]
fn simple_is_empty() {
let mut selection = Selection::simple(Point::new(0, Column(0)), Side::Right);
assert!(selection.is_empty());
selection.update(Point::new(0, Column(1)), Side::Left);
assert!(selection.is_empty());
selection.update(Point::new(1, Column(0)), Side::Right);
assert!(!selection.is_empty());
}

#[test]
fn block_is_empty() {
let mut selection = Selection::block(Point::new(0, Column(0)), Side::Right);
assert!(selection.is_empty());
selection.update(Point::new(0, Column(1)), Side::Left);
assert!(selection.is_empty());
selection.update(Point::new(0, Column(1)), Side::Right);
assert!(!selection.is_empty());
selection.update(Point::new(1, Column(0)), Side::Right);
assert!(selection.is_empty());
selection.update(Point::new(1, Column(1)), Side::Left);
assert!(selection.is_empty());
selection.update(Point::new(1, Column(1)), Side::Right);
assert!(!selection.is_empty());
}
}
2 changes: 1 addition & 1 deletion alacritty_terminal/src/tty/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ use mio::unix::EventedFd;
use std::ffi::CStr;
use std::fs::File;
use std::io;
use std::mem::MaybeUninit;
use std::os::unix::{
io::{AsRawFd, FromRawFd, RawFd},
process::CommandExt,
};
use std::process::{Child, Command, Stdio};
use std::ptr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::mem::MaybeUninit;

/// Process ID of child process
///
Expand Down

0 comments on commit fb37a9c

Please sign in to comment.