aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/position.rs
blob: 392eee9ccf40eb460326ea7ea00fb73964f95a05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::{
    chars::char_is_line_ending,
    graphemes::{nth_next_grapheme_boundary, RopeGraphemes},
    Rope, RopeSlice,
};

/// Represents a single point in a text buffer. Zero indexed.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
    pub row: usize,
    pub col: usize,
}

impl Position {
    pub const fn new(row: usize, col: usize) -> Self {
        Self { row, col }
    }

    pub const fn is_zero(self) -> bool {
        self.row == 0 && self.col == 0
    }

    // TODO: generalize
    pub fn traverse(self, text: &crate::Tendril) -> Self {
        let Self { mut row, mut col } = self;
        // TODO: there should be a better way here
        let mut chars = text.chars().peekable();
        while let Some(ch) = chars.next() {
            if char_is_line_ending(ch) && !(ch == '\r' && chars.peek() == Some(&'\n')) {
                row += 1;
                col = 0;
            } else {
                col += 1;
            }
        }
        Self { row, col }
    }
}

impl From<(usize, usize)> for Position {
    fn from(tuple: (usize, usize)) -> Self {
        Self {
            row: tuple.0,
            col: tuple.1,
        }
    }
}

impl From<Position> for tree_sitter::Point {
    fn from(pos: Position) -> Self {
        Self::new(pos.row, pos.col)
    }
}
/// Convert a character index to (line, column) coordinates.
pub fn coords_at_pos(text: RopeSlice, pos: usize) -> Position {
    let line = text.char_to_line(pos);
    let line_start = text.line_to_char(line);
    let col = RopeGraphemes::new(text.slice(line_start..pos)).count();
    Position::new(line, col)
}

/// Convert (line, column) coordinates to a character index.
pub fn pos_at_coords(text: RopeSlice, coords: Position) -> usize {
    let Position { row, col } = coords;
    let line_start = text.line_to_char(row);
    // line_start + col
    nth_next_grapheme_boundary(text, line_start, col)
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_ordering() {
        // (0, 5) is less than (1, 0)
        assert!(Position::new(0, 5) < Position::new(1, 0));
    }

    #[test]
    fn test_coords_at_pos() {
        let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
        let slice = text.slice(..);
        // assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
        // assert_eq!(coords_at_pos(slice, 5), (0, 5).into()); // position on \n
        // assert_eq!(coords_at_pos(slice, 6), (1, 0).into()); // position on w
        // 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 with grapheme clusters
        let text = Rope::from("a̐éö̲\r\n");
        let slice = text.slice(..);
        assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
        assert_eq!(coords_at_pos(slice, 2), (0, 1).into());
        assert_eq!(coords_at_pos(slice, 4), (0, 2).into());
        assert_eq!(coords_at_pos(slice, 7), (0, 3).into());

        let text = Rope::from("किमपि");
        let slice = text.slice(..);
        assert_eq!(coords_at_pos(slice, 0), (0, 0).into());
        assert_eq!(coords_at_pos(slice, 2), (0, 1).into());
        assert_eq!(coords_at_pos(slice, 3), (0, 2).into());
        assert_eq!(coords_at_pos(slice, 5), (0, 3).into());
    }

    #[test]
    fn test_pos_at_coords() {
        let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
        let slice = text.slice(..);
        assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
        assert_eq!(pos_at_coords(slice, (0, 5).into()), 5); // position on \n
        assert_eq!(pos_at_coords(slice, (1, 0).into()), 6); // position on w
        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 with grapheme clusters
        let text = Rope::from("a̐éö̲\r\n");
        let slice = text.slice(..);
        assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
        assert_eq!(pos_at_coords(slice, (0, 1).into()), 2);
        assert_eq!(pos_at_coords(slice, (0, 2).into()), 4);
        assert_eq!(pos_at_coords(slice, (0, 3).into()), 7); // \r\n is one char here
        assert_eq!(pos_at_coords(slice, (0, 4).into()), 9);
        let text = Rope::from("किमपि");
        // 2 - 1 - 2 codepoints
        // TODO: delete handling as per https://news.ycombinator.com/item?id=20058454
        let slice = text.slice(..);
        assert_eq!(pos_at_coords(slice, (0, 0).into()), 0);
        assert_eq!(pos_at_coords(slice, (0, 1).into()), 2);
        assert_eq!(pos_at_coords(slice, (0, 2).into()), 3);
        assert_eq!(pos_at_coords(slice, (0, 3).into()), 5); // eol
    }
}