@@ -1,5 +1,5 @@
1−use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary};
1+use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
2−use crate::{Buffer, Selection, SelectionRange};
2+use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange};
4/// A state represents the current editor state of a single buffer.
4/// A state represents the current editor state of a single buffer.
5pub struct State {
5pub struct State {
@@ -117,3 +117,44 @@ impl State {
117 // TODO: update selection in state via transaction
117 // TODO: update selection in state via transaction
121+/// Coordinates are a 0-indexed line and column pair.
122+type Coords = (usize, usize); // line, col
124+/// Convert a character index to (line, column) coordinates.
125+pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
126+ let line = text.char_to_line(pos);
127+ let line_start = text.line_to_char(line);
128+ let col = RopeGraphemes::new(&text.slice(line_start..pos)).count();
132+/// Convert (line, column) coordinates to a character index.
133+pub fn pos_at_coords(text: &RopeSlice, coords: Coords) -> usize {
134+ let (line, col) = coords;
135+ let line_start = text.line_to_char(line);
136+ nth_next_grapheme_boundary(text, line_start, col)
144+ fn test_coords_at_pos() {
145+ let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
146+ assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
147+ assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
148+ 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
153+ fn test_pos_at_coords() {
154+ let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
155+ assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
156+ assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
157+ 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