|
|
@ -101,7 +101,7 @@ impl State {
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let start = text.line_to_char(line);
|
|
|
|
let start = text.line_to_char(line);
|
|
|
|
let pos = std::cmp::max(
|
|
|
|
let pos = std::cmp::max(
|
|
|
|
nth_prev_grapheme_boundary(&text.slice(..), pos, count),
|
|
|
|
nth_prev_grapheme_boundary(text.slice(..), pos, count),
|
|
|
|
start,
|
|
|
|
start,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
|
|
@ -113,14 +113,14 @@ impl State {
|
|
|
|
// subtract another 1 because the line ends with \n
|
|
|
|
// subtract another 1 because the line ends with \n
|
|
|
|
let end = text.line_to_char(line + 1).saturating_sub(2);
|
|
|
|
let end = text.line_to_char(line + 1).saturating_sub(2);
|
|
|
|
let pos =
|
|
|
|
let pos =
|
|
|
|
std::cmp::min(nth_next_grapheme_boundary(&text.slice(..), pos, count), end);
|
|
|
|
std::cmp::min(nth_next_grapheme_boundary(text.slice(..), pos, count), end);
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(_, Granularity::Line) => move_vertically(&text.slice(..), dir, range, count, extend),
|
|
|
|
(_, Granularity::Line) => move_vertically(text.slice(..), dir, range, count, extend),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_next_word_start(slice: &RopeSlice, mut pos: usize) -> usize {
|
|
|
|
pub fn move_next_word_start(slice: RopeSlice, mut pos: usize) -> usize {
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
@ -143,7 +143,7 @@ impl State {
|
|
|
|
pos
|
|
|
|
pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_prev_word_start(slice: &RopeSlice, mut pos: usize) -> usize {
|
|
|
|
pub fn move_prev_word_start(slice: RopeSlice, mut pos: usize) -> usize {
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let prev = slice.char(pos.saturating_sub(1)); // TODO: just return original pos if at start
|
|
|
|
let prev = slice.char(pos.saturating_sub(1)); // TODO: just return original pos if at start
|
|
|
@ -169,7 +169,7 @@ impl State {
|
|
|
|
pos.saturating_add(1)
|
|
|
|
pos.saturating_add(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn move_next_word_end(slice: &RopeSlice, mut pos: usize, _count: usize) -> usize {
|
|
|
|
pub fn move_next_word_end(slice: RopeSlice, mut pos: usize, _count: usize) -> usize {
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
// TODO: confirm it's fine without using graphemes, I think it should be
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let ch = slice.char(pos);
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
|
let next = slice.char(pos.saturating_add(1));
|
|
|
@ -217,7 +217,7 @@ impl State {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Convert a character index to (line, column) coordinates.
|
|
|
|
/// Convert a character index to (line, column) coordinates.
|
|
|
|
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Position {
|
|
|
|
pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
let line_start = text.line_to_char(line);
|
|
|
|
let line_start = text.line_to_char(line);
|
|
|
|
let col = text.slice(line_start..pos).len_chars();
|
|
|
|
let col = text.slice(line_start..pos).len_chars();
|
|
|
@ -225,14 +225,14 @@ pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Position {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Convert (line, column) coordinates to a character index.
|
|
|
|
/// Convert (line, column) coordinates to a character index.
|
|
|
|
pub fn pos_at_coords(text: &RopeSlice, coords: Position) -> usize {
|
|
|
|
pub fn pos_at_coords(text: RopeSlice, coords: Position) -> usize {
|
|
|
|
let Position { row, col } = coords;
|
|
|
|
let Position { row, col } = coords;
|
|
|
|
let line_start = text.line_to_char(row);
|
|
|
|
let line_start = text.line_to_char(row);
|
|
|
|
nth_next_grapheme_boundary(text, line_start, col)
|
|
|
|
nth_next_grapheme_boundary(text, line_start, col)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn move_vertically(
|
|
|
|
fn move_vertically(
|
|
|
|
text: &RopeSlice,
|
|
|
|
text: RopeSlice,
|
|
|
|
dir: Direction,
|
|
|
|
dir: Direction,
|
|
|
|
range: Range,
|
|
|
|
range: Range,
|
|
|
|
count: usize,
|
|
|
|
count: usize,
|
|
|
@ -287,7 +287,7 @@ fn categorize(ch: char) -> Category {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn skip_over_next<F>(slice: &RopeSlice, pos: &mut usize, fun: F)
|
|
|
|
fn skip_over_next<F>(slice: RopeSlice, pos: &mut usize, fun: F)
|
|
|
|
where
|
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -301,7 +301,7 @@ where
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn skip_over_prev<F>(slice: &RopeSlice, pos: &mut usize, fun: F)
|
|
|
|
fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F)
|
|
|
|
where
|
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -323,34 +323,36 @@ mod test {
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_coords_at_pos() {
|
|
|
|
fn test_coords_at_pos() {
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0).into());
|
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5).into()); // position on \n
|
|
|
|
assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0).into()); // position on w
|
|
|
|
assert_eq!(coords_at_pos(slice, 5), (0, 5).into()); // position on \n
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 7), (1, 1).into()); // position on o
|
|
|
|
assert_eq!(coords_at_pos(slice, 6), (1, 0).into()); // position on w
|
|
|
|
assert_eq!(coords_at_pos(&text.slice(..), 10), (1, 4).into()); // position on d
|
|
|
|
assert_eq!(coords_at_pos(slice, 7), (1, 1).into()); // position on o
|
|
|
|
|
|
|
|
assert_eq!(coords_at_pos(slice, 10), (1, 4).into()); // position on d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_pos_at_coords() {
|
|
|
|
fn test_pos_at_coords() {
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (0, 0).into()), 0);
|
|
|
|
let slice = text.slice(..);
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (0, 5).into()), 5); // position on \n
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 0).into()), 6); // position on w
|
|
|
|
assert_eq!(pos_at_coords(slice, (0, 5).into()), 5); // position on \n
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 1).into()), 7); // position on o
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 0).into()), 6); // position on w
|
|
|
|
assert_eq!(pos_at_coords(&text.slice(..), (1, 4).into()), 10); // position on d
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 1).into()), 7); // position on o
|
|
|
|
|
|
|
|
assert_eq!(pos_at_coords(slice, (1, 4).into()), 10); // position on d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_vertical_move() {
|
|
|
|
fn test_vertical_move() {
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
let pos = pos_at_coords(&text.slice(..), (0, 4).into());
|
|
|
|
|
|
|
|
let slice = text.slice(..);
|
|
|
|
let slice = text.slice(..);
|
|
|
|
|
|
|
|
let pos = pos_at_coords(slice, (0, 4).into());
|
|
|
|
|
|
|
|
|
|
|
|
let range = Range::new(pos, pos);
|
|
|
|
let range = Range::new(pos, pos);
|
|
|
|
assert_eq!(
|
|
|
|
assert_eq!(
|
|
|
|
coords_at_pos(
|
|
|
|
coords_at_pos(
|
|
|
|
&slice,
|
|
|
|
slice,
|
|
|
|
move_vertically(&slice, Direction::Forward, range, 1, false).head
|
|
|
|
move_vertically(slice, Direction::Forward, range, 1, false).head
|
|
|
|
),
|
|
|
|
),
|
|
|
|
(1, 2).into()
|
|
|
|
(1, 2).into()
|
|
|
|
);
|
|
|
|
);
|
|
|
|