@@ -63,6 +63,7 @@ impl State {
63 ) -> usize {
63 ) -> usize {
64 let text = &self.doc.contents;
64 let text = &self.doc.contents;
65 match (dir, granularity) {
65 match (dir, granularity) {
66+ // TODO: clamp movement to line, prevent moving onto \n at the end
66 (Direction::Backward, Granularity::Character) => {
67 (Direction::Backward, Granularity::Character) => {
67 nth_prev_grapheme_boundary(&text.slice(..), pos, count)
68 nth_prev_grapheme_boundary(&text.slice(..), pos, count)
@@ -131,8 +132,7 @@ type Coords = (usize, usize); // line, col
131pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
132pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
132 let line = text.char_to_line(pos);
133 let line = text.char_to_line(pos);
133 let line_start = text.line_to_char(line);
134 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();
136 (line, col)
136 (line, col)
@@ -151,8 +151,8 @@ fn move_vertically(text: &RopeSlice, dir: Direction, pos: usize, count: usize) -
151 Direction::Forward => std::cmp::min(line.saturating_add(count), text.len_lines() - 1),
151 Direction::Forward => std::cmp::min(line.saturating_add(count), text.len_lines() - 1),
154− // convert to 0-indexed
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(1);
155+ let new_line_len = text.line(new_line).len_chars().saturating_sub(2);
157 let new_col = if new_line_len < col {
157 let new_col = if new_line_len < col {
158 // TODO: preserve horiz here
158 // TODO: preserve horiz here
@@ -172,9 +172,11 @@ mod test {
172 fn test_coords_at_pos() {
172 fn test_coords_at_pos() {
173 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
173 let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
174 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
174 assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
175+ // TODO: what is the coordinate of newline?
175− assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 4)); // position on \n
176+ assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
176 assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
177 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
@@ -183,7 +185,8 @@ mod test {
183 assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
185 assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
184 assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
186 assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
185 assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
187 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