Skip to content

Commit

Permalink
fix selected text is not deleted on paste
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 14, 2023
1 parent 8e3a432 commit 13ce6aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,7 @@ impl<'a> TextArea<'a> {
/// assert_eq!(textarea.lines(), [" bbb cccaaa"]);
/// ```
pub fn paste(&mut self) -> bool {
self.delete_selection(false);
match self.yank.clone() {
YankText::Piece(s) => self.insert_piece(s),
YankText::Chunk(c) => self.insert_chunk(c),
Expand Down
21 changes: 21 additions & 0 deletions tests/textarea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,27 @@ fn test_select_all() {
assert_undo_redo((2, 3), &["aaa", "bbb", "ccc"], &[""], &mut t, "");
}

#[test]
fn test_paste_while_selection() {
let mut t = TextArea::from(["ab", "cd"]);
t.move_cursor(CursorMove::Jump(0, 1));
t.start_selection();
t.move_cursor(CursorMove::Jump(1, 1));
t.set_yank_text("x\ny");
assert!(t.paste());
assert_eq!(t.lines(), ["ax", "yd"]);
assert_eq!(t.cursor(), (1, 1));
assert!(!t.is_selecting());

let mut t = TextArea::from(["ab", "cd"]);
t.select_all();
t.set_yank_text("xy\nzw");
assert!(t.paste());
assert_eq!(t.lines(), ["xy", "zw"]);
assert_eq!(t.cursor(), (1, 2));
assert!(!t.is_selecting());
}

struct DeleteTester(&'static [&'static str], fn(&mut TextArea) -> bool);
impl DeleteTester {
fn test(&self, before: (usize, usize), after: (usize, usize, &[&str], &str)) {
Expand Down

0 comments on commit 13ce6aa

Please sign in to comment.