Skip to content

Commit

Permalink
Fix last column block selection
Browse files Browse the repository at this point in the history
This fixes a regression introduced in 8e58409, where block selections
containing the last cell would have the trailing newline stripped and be
joined into one long line on copy.
  • Loading branch information
a5ob7r authored Jan 7, 2022
1 parent 50c7c2b commit 5aa8046
Showing 1 changed file with 83 additions and 22 deletions.
105 changes: 83 additions & 22 deletions alacritty_terminal/src/term/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,9 @@ impl<T> Term<T> {
res += self
.line_to_string(line, start.column..end.column, start.column.0 != 0)
.trim_end();

// If the last column is included, newline is appended automatically.
if end.column != self.columns() - 1 {
res += "\n";
}
res += "\n";
}

res += self.line_to_string(end.line, start.column..end.column, true).trim_end();
},
Some(Selection { ty: SelectionType::Lines, .. }) => {
Expand Down Expand Up @@ -2018,6 +2015,52 @@ mod tests {
assert_eq!(term.grid.display_offset(), 0);
}

#[test]
fn simple_selection_works() {
let size = SizeInfo::new(5., 5., 1.0, 1.0, 0.0, 0.0, false);
let mut term = Term::new(&Config::default(), size, ());
let grid = term.grid_mut();
for i in 0..4 {
if i == 1 {
continue;
}

grid[Line(i)][Column(0)].c = '"';

for j in 1..4 {
grid[Line(i)][Column(j)].c = 'a';
}

grid[Line(i)][Column(4)].c = '"';
}
grid[Line(2)][Column(0)].c = ' ';
grid[Line(2)][Column(4)].c = ' ';
grid[Line(2)][Column(4)].flags.insert(Flags::WRAPLINE);
grid[Line(3)][Column(0)].c = ' ';

// Multiple lines contain an empty line.
term.selection = Some(Selection::new(
SelectionType::Simple,
Point { line: Line(0), column: Column(0) },
Side::Left,
));
if let Some(s) = term.selection.as_mut() {
s.update(Point { line: Line(2), column: Column(4) }, Side::Right);
}
assert_eq!(term.selection_to_string(), Some(String::from("\"aaa\"\n\n aaa ")));

// A wrapline.
term.selection = Some(Selection::new(
SelectionType::Simple,
Point { line: Line(2), column: Column(0) },
Side::Left,
));
if let Some(s) = term.selection.as_mut() {
s.update(Point { line: Line(3), column: Column(4) }, Side::Right);
}
assert_eq!(term.selection_to_string(), Some(String::from(" aaa aaa\"")));
}

#[test]
fn semantic_selection_works() {
let size = SizeInfo::new(5., 3., 1.0, 1.0, 0.0, 0.0, false);
Expand Down Expand Up @@ -2088,28 +2131,46 @@ mod tests {
}

#[test]
fn selecting_empty_line() {
let size = SizeInfo::new(3.0, 3.0, 1.0, 1.0, 0.0, 0.0, false);
fn block_selection_works() {
let size = SizeInfo::new(5., 5., 1.0, 1.0, 0.0, 0.0, false);
let mut term = Term::new(&Config::default(), size, ());
let mut grid: Grid<Cell> = Grid::new(3, 3, 0);
for l in 0..3 {
if l != 1 {
for c in 0..3 {
grid[Line(l)][Column(c)].c = 'a';
}
let grid = term.grid_mut();
for i in 1..4 {
grid[Line(i)][Column(0)].c = '"';

for j in 1..4 {
grid[Line(i)][Column(j)].c = 'a';
}
}

mem::swap(&mut term.grid, &mut grid);
grid[Line(i)][Column(4)].c = '"';
}
grid[Line(2)][Column(2)].c = ' ';
grid[Line(2)][Column(4)].flags.insert(Flags::WRAPLINE);
grid[Line(3)][Column(4)].c = ' ';

let mut selection = Selection::new(
SelectionType::Simple,
Point { line: Line(0), column: Column(0) },
term.selection = Some(Selection::new(
SelectionType::Block,
Point { line: Line(0), column: Column(3) },
Side::Left,
);
selection.update(Point { line: Line(2), column: Column(2) }, Side::Right);
term.selection = Some(selection);
assert_eq!(term.selection_to_string(), Some("aaa\n\naaa".into()));
));

// The same column.
if let Some(s) = term.selection.as_mut() {
s.update(Point { line: Line(3), column: Column(3) }, Side::Right);
}
assert_eq!(term.selection_to_string(), Some(String::from("\na\na\na")));

// The first column.
if let Some(s) = term.selection.as_mut() {
s.update(Point { line: Line(3), column: Column(0) }, Side::Left);
}
assert_eq!(term.selection_to_string(), Some(String::from("\n\"aa\n\"a\n\"aa")));

// The last column.
if let Some(s) = term.selection.as_mut() {
s.update(Point { line: Line(3), column: Column(4) }, Side::Right);
}
assert_eq!(term.selection_to_string(), Some(String::from("\na\"\na\"\na")));
}

/// Check that the grid can be serialized back and forth losslessly.
Expand Down

0 comments on commit 5aa8046

Please sign in to comment.