selection extension test, selection extension fixes

This commit is contained in:
Anton-4 2021-01-05 17:56:28 +01:00
parent 85553987ec
commit bed9166fcb
2 changed files with 111 additions and 8 deletions

View File

@ -771,4 +771,85 @@ mod test_parse {
Ok(())
}
#[test]
fn extend_selection_right() -> Result<(), String> {
assert_move(&["[a]|bc"], &["[ab]|c"], true, move_caret_right)?;
assert_move(&["a[b]|c"], &["a[bc]|"], true, move_caret_right)?;
assert_move(&["[ab]|c"], &["[abc]|"], true, move_caret_right)?;
assert_move(&["[ ]|abc"], &["[ a]|bc"], true, move_caret_right)?;
assert_move(&["[abc]|"], &["[abc]|"], true, move_caret_right)?;
assert_move(&["a[bc]|"], &["a[bc]|"], true, move_caret_right)?;
assert_move(&["ab[c]|"], &["ab[c]|"], true, move_caret_right)?;
assert_move(&["abc[\n","]|d"], &["abc[\n","d]|"], true, move_caret_right)?;
assert_move(&["ab[c]|\n",""], &["ab[c\n","]|"], true, move_caret_right)?;
assert_move(&["ab[c]|\n","d"], &["ab[c\n","]|d"], true, move_caret_right)?;
assert_move(&["abc\n","def\n", "ghi[\n","]|jkl"], &["abc\n","def\n", "ghi[\n", "j]|kl"], true, move_caret_right)?;
assert_move(&["ab[c\n","def\n", "ghi\n","]|jkl"], &["ab[c\n","def\n", "ghi\n", "j]|kl"], true, move_caret_right)?;
assert_move(&["ab[c\n","def\n", "]|ghi\n","jkl"], &["ab[c\n","def\n", "g]|hi\n", "jkl"], true, move_caret_right)?;
assert_move(&["[abc\n","def\n", "ghi\n","jk]|l"], &["[abc\n","def\n", "ghi\n", "jkl]|"], true, move_caret_right)?;
assert_move(&["[abc\n","def\n", "ghi\n", "jkl]|"], &["[abc\n","def\n", "ghi\n", "jkl]|"], true, move_caret_right)?;
Ok(())
}
#[test]
fn extend_selection_left() -> Result<(), String> {
assert_move(&["ab|[c]"], &["a|[bc]"], true, move_caret_left)?;
assert_move(&["a|[bc]"], &["|[abc]"], true, move_caret_left)?;
assert_move(&["|[abc]"], &["|[abc]"], true, move_caret_left)?;
assert_move(&["|[ab]c"], &["|[ab]c"], true, move_caret_left)?;
assert_move(&["|[a]bc"], &["|[a]bc"], true, move_caret_left)?;
assert_move(&[" |[a]bc"], &["|[ a]bc"], true, move_caret_left)?;
assert_move(&["abc|[\n","]d"], &["ab|[c\n","]d"], true, move_caret_left)?;
assert_move(&["abc\n","|[d]"], &["abc|[\n","d]"], true, move_caret_left)?;
assert_move(&["ab|[c\n","]"], &["a|[bc\n","]"], true, move_caret_left)?;
assert_move(&["abc\n","def|[\n", "ghi\n","j]kl"], &["abc\n","de|[f\n", "ghi\n", "j]kl"], true, move_caret_left)?;
assert_move(&["a|[bc\n","def\n", "ghi\n","jkl]"], &["|[abc\n","def\n", "ghi\n", "jkl]"], true, move_caret_left)?;
assert_move(&["abc\n","def\n", "ghi\n", "|[jkl]"], &["abc\n","def\n", "ghi|[\n", "jkl]"], true, move_caret_left)?;
Ok(())
}
#[test]
fn extend_selection_up() -> Result<(), String> {
assert_move(&["ab|[c]"], &["|[abc]"], true, move_caret_up)?;
assert_move(&["a|[bc]"], &["|[abc]"], true, move_caret_up)?;
assert_move(&["|[abc]"], &["|[abc]"], true, move_caret_up)?;
assert_move(&["|[ab]c"], &["|[ab]c"], true, move_caret_up)?;
assert_move(&["|[a]bc"], &["|[a]bc"], true, move_caret_up)?;
assert_move(&[" |[a]bc"], &["|[ a]bc"], true, move_caret_up)?;
assert_move(&["ab[c]|"], &["|[ab]c"], true, move_caret_up)?;
assert_move(&["[a]|"], &["|a"], true, move_caret_up)?;
assert_move(&["[a]|bc"], &["|abc"], true, move_caret_up)?;
assert_move(&["[a]|bc\n","d"], &["|abc\n","d"], true, move_caret_up)?;
assert_move(&["abc\n","de[f]|"], &["abc|[\n","de]f"], true, move_caret_up)?;
assert_move(&["abc\n","de|[f]"], &["ab|[c\n","def]"], true, move_caret_up)?;
assert_move(&["ab|[c\n","def]"], &["|[abc\n","def]"], true, move_caret_up)?;
assert_move(&["ab\n","cdef\n", "ghijkl\n","[mnopqr]|st"], &["ab\n","cdef\n", "ghijkl|[\n","]mnopqrst"], true, move_caret_up)?;
assert_move(&["ab\n","cdef\n", "ghijkl\n","[mnopqrs]|t"], &["ab\n","cdef\n", "ghijkl|[\n","]mnopqrst"], true, move_caret_up)?;
assert_move(&["abcdefgh\n","ijklmn\n", "|[o]pqr\n","st"], &["abcdefgh\n","|[ijklmn\n", "o]pqr\n","st"], true, move_caret_up)?;
Ok(())
}
#[test]
fn extend_selection_down() -> Result<(), String> {
assert_move(&["[ab]|c"], &["[abc]|"], true, move_caret_down)?;
assert_move(&["[a]|bc"], &["[abc]|"], true, move_caret_down)?;
assert_move(&["[abc]|"], &["[abc]|"], true, move_caret_down)?;
assert_move(&["|[ab]c"], &["ab[c]|"], true, move_caret_down)?;
assert_move(&["|[a]bc"], &["a[bc]|"], true, move_caret_down)?;
assert_move(&["[a]|bc\n","d"], &["[abc\n","d]|"], true, move_caret_down)?;
assert_move(&["[a]|bc\n","de"], &["[abc\n","d]|e"], true, move_caret_down)?;
assert_move(&["[abc\n","d]|e"], &["[abc\n","de]|"], true, move_caret_down)?;
assert_move(&["[a]|bc\n",""], &["[abc\n","]|"], true, move_caret_down)?;
assert_move(&["ab\n","cdef\n", "ghijkl\n","[mnopqr]|st"], &["ab\n","cdef\n", "ghijkl\n","[mnopqrst]|"], true, move_caret_down)?;
assert_move(&["a[b\n","cdef\n", "ghijkl\n","mnopqr]|st"], &["a[b\n","cdef\n", "ghijkl\n","mnopqrst]|"], true, move_caret_down)?;
assert_move(&["[ab\n","cdef\n", "ghijkl\n","mnopqrst]|"], &["[ab\n","cdef\n", "ghijkl\n","mnopqrst]|"], true, move_caret_down)?;
assert_move(&["abcd[efgh]|\n","ijklmn\n", "opqr\n","st"], &["abcd[efgh\n","ijklmn]|\n", "opqr\n","st"], true, move_caret_down)?;
assert_move(&["abcd[e]|fgh\n","ijklmn\n", "opqr\n","st"], &["abcd[efgh\n","ijklm]|n\n", "opqr\n","st"], true, move_caret_down)?;
Ok(())
}
}

View File

@ -168,10 +168,21 @@ pub fn move_caret_up(
let new_selection_opt = if shift_pressed {
if let Some(old_selection) = old_selection_opt {
Some(RawSelection {
start_pos: new_caret_pos,
end_pos: old_selection.end_pos,
})
if old_selection.end_pos <= old_caret_pos {
if new_caret_pos == old_selection.start_pos {
None
} else {
Some(RawSelection {
start_pos: new_caret_pos,
end_pos: old_selection.start_pos,
})
}
} else {
Some(RawSelection {
start_pos: new_caret_pos,
end_pos: old_selection.end_pos,
})
}
} else if !(old_line_nr == line_nr && old_col_nr == col_nr) {
Some(RawSelection {
start_pos: min(old_caret_pos, new_caret_pos),
@ -233,10 +244,21 @@ pub fn move_caret_down(
let new_selection_opt = if shift_pressed {
if let Some(old_selection) = old_selection_opt {
Some(RawSelection {
start_pos: old_selection.start_pos,
end_pos: new_caret_pos,
})
if old_caret_pos <= old_selection.start_pos {
if new_caret_pos == old_selection.end_pos {
None
} else {
Some(RawSelection {
start_pos: old_selection.end_pos,
end_pos: new_caret_pos,
})
}
} else {
Some(RawSelection {
start_pos: old_selection.start_pos,
end_pos: new_caret_pos,
})
}
} else if !(old_line_nr == line_nr && old_col_nr == col_nr) {
Some(RawSelection {
start_pos: min(old_caret_pos, new_caret_pos),