@@ -63,6 +63,7 @@ impl State {
6363 ) -> usize {
6464 let text = &self.doc.contents;
6565 match (dir, granularity) {
66+ // TODO: clamp movement to line, prevent moving onto \n at the end
6667 (Direction::Backward, Granularity::Character) => {
6768 nth_prev_grapheme_boundary(&text.slice(..), pos, count)
6869 }
@@ -131,8 +132,7 @@ type Coords = (usize, usize); // line, col
131132pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
132133 let line = text.char_to_line(pos);
133134 let line_start = text.line_to_char(line);
134− // convert to 0-indexed
135− let col = text.slice(line_start..pos).len_chars().saturating_sub(1);
135+ let col = text.slice(line_start..pos).len_chars();
136136 (line, col)
137137}
138138
@@ -151,8 +151,8 @@ fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, count: usize) -
151151 Direction::Forward => std::cmp::min(line.saturating_add(count), text.len_lines() - 1),
152152 };
153153
154− // convert to 0-indexed
155− let new_line_len = text.line(new_line).len_chars().saturating_sub(1);
154+ // convert to 0-indexed, subtract another 1 because len_chars() counts \n
155+ let new_line_len = text.line(new_line).len_chars().saturating_sub(2);
156156
157157 let new_col = if new_line_len < col {
158158 // TODO: preserve horiz here
@@ -172,9 +172,11 @@ mod test {
172172 fn test_coords_at_pos() {
173173 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
174174 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
175− assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 4)); // position on \n
175+ // TODO: what is the coordinate of newline?
176+ assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
176177 assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
177− assert_eq!(coords_at_pos(&text.slice(..), 11), (1, 4)); // position on d
178+ assert_eq!(coords_at_pos(&text.slice(..), 7), (1, 1)); // position on o
179+ assert_eq!(coords_at_pos(&text.slice(..), 10), (1, 4)); // position on d
178180 }
179181
180182 #[test]
@@ -183,7 +185,8 @@ mod test {
183185 assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
184186 assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
185187 assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
186− assert_eq!(pos_at_coords(&text.slice(..), (1, 5)), 11); // position on d
188+ assert_eq!(pos_at_coords(&text.slice(..), (1, 1)), 7); // position on o
189+ assert_eq!(pos_at_coords(&text.slice(..), (1, 4)), 10); // position on d
187190 }
188191
189192 #[test]