@@ -69,6 +69,7 @@ impl State {
69 (Direction::Forward, Granularity::Character) => {
69 (Direction::Forward, Granularity::Character) => {
70 nth_next_grapheme_boundary(&text.slice(..), pos, n)
70 nth_next_grapheme_boundary(&text.slice(..), pos, n)
72+ (_, Granularity::Line) => move_vertically(&text.slice(..), dir, pos, n),
@@ -125,7 +126,8 @@ type Coords = (usize, usize); // line, col
125pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
126pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
126 let line = text.char_to_line(pos);
127 let line = text.char_to_line(pos);
127 let line_start = text.line_to_char(line);
128 let line_start = text.line_to_char(line);
129+ // convert to 0-indexed
128− let col = RopeGraphemes::new(&text.slice(line_start..pos)).count();
130+ let col = text.slice(line_start..pos).len_chars().saturating_sub(1);
129 (line, col)
131 (line, col)
@@ -136,6 +138,31 @@ pub fn pos_at_coords(text: &RopeSlice, coords: Coords) -> usize {
136 nth_next_grapheme_boundary(text, line_start, col)
138 nth_next_grapheme_boundary(text, line_start, col)
141+fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, n: usize) -> usize {
142+ let (line, col) = coords_at_pos(text, pos);
144+ let new_line = match dir {
145+ Direction::Backward => line.saturating_sub(n),
146+ Direction::Forward => std::cmp::min(line.saturating_add(n), text.len_lines() - 1),
149+ // convert to 0-indexed
150+ let new_line_len = text.line(new_line).len_chars().saturating_sub(1);
152+ let new_col = if new_line_len < col {
153+ // TODO: preserve horiz here
159+ pos_at_coords(text, (new_line, new_col))
162+/// A command is a function that takes the current state and a count, and does a side-effect on the
163+/// state (usually by creating and applying a transaction).
164+type Command = fn(state: &mut State, count: usize) -> bool;
139#[cfg(test)]
166#[cfg(test)]
140mod test {
167mod test {
141 use super::*;
168 use super::*;
@@ -144,9 +171,9 @@ mod test {
144 fn test_coords_at_pos() {
171 fn test_coords_at_pos() {
145 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
172 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
146 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
173 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
147− assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
174+ assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 4)); // position on \n
148 assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
175 assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
149− assert_eq!(coords_at_pos(&text.slice(..), 11), (1, 5)); // position on d
176+ assert_eq!(coords_at_pos(&text.slice(..), 11), (1, 4)); // position on d
@@ -157,4 +184,16 @@ mod test {
157 assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
184 assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
158 assert_eq!(pos_at_coords(&text.slice(..), (1, 5)), 11); // position on d
185 assert_eq!(pos_at_coords(&text.slice(..), (1, 5)), 11); // position on d
189+ fn test_vertical_move() {
190+ let text = Rope::from("abcd\nefg\nwrs");
191+ let pos = pos_at_coords(&text.slice(..), (0, 4));
192+ let slice = text.slice(..);
195+ coords_at_pos(&slice, move_vertically(&slice, Direction::Forward, pos, 1)),