aboutsummaryrefslogtreecommitdiff
path: root/helix-core/src/state.rs
blob: ccbfd28aefa26c017edb11893576c8504005bde0 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
use crate::{Buffer, Rope, RopeSlice, Selection, SelectionRange};

/// A state represents the current editor state of a single buffer.
pub struct State {
    doc: Buffer,
    selection: Selection,
}

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Direction {
    Forward,
    Backward,
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Granularity {
    Character,
    Word,
    Line,
    // LineBoundary
}

impl State {
    #[must_use]
    pub fn new(doc: Buffer) -> Self {
        Self {
            doc,
            selection: Selection::single(0, 0),
        }
    }

    // TODO: buf/selection accessors

    // update/transact:
    // update(desc) => transaction ?  transaction.doc() for applied doc
    // transaction.apply(doc)
    // doc.transact(fn -> ... end)

    // replaceSelection (transaction that replaces selection)
    // changeByRange
    // changes
    // slice
    //
    // getters:
    // tabSize
    // indentUnit
    // languageDataAt()
    //
    // config:
    // indentation
    // tabSize
    // lineUnit
    // syntax
    // foldable
    // changeFilter/transactionFilter

    pub fn move_pos(
        &self,
        pos: usize,
        dir: Direction,
        granularity: Granularity,
        n: usize,
    ) -> usize {
        let text = &self.doc.contents;
        match (dir, granularity) {
            (Direction::Backward, Granularity::Character) => {
                nth_prev_grapheme_boundary(&text.slice(..), pos, n)
            }
            (Direction::Forward, Granularity::Character) => {
                nth_next_grapheme_boundary(&text.slice(..), pos, n)
            }
            _ => pos,
        }
    }

    pub fn move_selection(
        &self,
        sel: Selection,
        dir: Direction,
        granularity: Granularity,
        // TODO: n
    ) -> Selection {
        // TODO: move all selections according to normal cursor move semantics by collapsing it
        // into cursors and moving them vertically

        let ranges = sel.ranges.into_iter().map(|range| {
            // let pos = if !range.is_empty() {
            //     // if selection already exists, bump it to the start or end of current select first
            //     if dir == Direction::Backward {
            //         range.from()
            //     } else {
            //         range.to()
            //     }
            // } else {
            let pos = self.move_pos(range.head, dir, granularity, 1);
            // };
            SelectionRange::new(pos, pos)
        });

        Selection::new(ranges.collect(), sel.primary_index)
        // TODO: update selection in state via transaction
    }

    pub fn extend_selection(
        &self,
        sel: Selection,
        dir: Direction,
        granularity: Granularity,
        n: usize,
    ) -> Selection {
        let ranges = sel.ranges.into_iter().map(|range| {
            let pos = self.move_pos(range.head, dir, granularity, n);
            SelectionRange::new(range.anchor, pos)
        });

        Selection::new(ranges.collect(), sel.primary_index)
        // TODO: update selection in state via transaction
    }
}

/// Coordinates are a 0-indexed line and column pair.
type Coords = (usize, usize); // line, col

/// Convert a character index to (line, column) coordinates.
pub fn coords_at_pos(text: &RopeSlice, pos: usize) -> Coords {
    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();
    (line, col)
}

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

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

    #[test]
    fn test_coords_at_pos() {
        let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
        assert_eq!(coords_at_pos(&text.slice(..), 0), (0, 0));
        assert_eq!(coords_at_pos(&text.slice(..), 5), (0, 5)); // position on \n
        assert_eq!(coords_at_pos(&text.slice(..), 6), (1, 0)); // position on w
        assert_eq!(coords_at_pos(&text.slice(..), 11), (1, 5)); // position on d
    }

    #[test]
    fn test_pos_at_coords() {
        let text = Rope::from("ḧëḷḷö\nẅöṛḷḋ");
        assert_eq!(pos_at_coords(&text.slice(..), (0, 0)), 0);
        assert_eq!(pos_at_coords(&text.slice(..), (0, 5)), 5); // position on \n
        assert_eq!(pos_at_coords(&text.slice(..), (1, 0)), 6); // position on w
        assert_eq!(pos_at_coords(&text.slice(..), (1, 5)), 11); // position on d
    }
}