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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use std::{cmp, ops::Range};

use super::{CharIndex, TextStorage, TextStorageMut};

#[derive(Clone, Debug, PartialEq)]
pub struct Cursor {
    /// Char range under cursor, aligned to extended grapheme clusters.
    range: Range<CharIndex>,
    /// The start of a selection if in select mode, ending at `range.start` or
    /// `range.end`, depending on direction. Aligned to extended grapheme
    /// clusters.
    selection: Option<CharIndex>,
    visual_horizontal_offset: Option<usize>,
}

impl Default for Cursor {
    fn default() -> Self {
        Self {
            range: CharIndex(0)..CharIndex(1),
            visual_horizontal_offset: None,
            selection: None,
        }
    }
}

impl Cursor {
    pub fn new() -> Self {
        Self::default()
    }

    #[cfg(test)]
    pub fn end_of_buffer<'a>(text: impl TextStorage<'a>) -> Self {
        Self {
            range: text.prev_grapheme_boundary(text.len_chars())..text.len_chars(),
            visual_horizontal_offset: None,
            selection: None,
        }
    }

    pub fn range(&self) -> &Range<CharIndex> {
        &self.range
    }

    pub fn selection(&self) -> Range<CharIndex> {
        match self.selection {
            Some(selection) if selection > self.range.start => self.range.start..selection,
            Some(selection) if selection < self.range.start => selection..self.range.start,
            _ => self.range.clone(),
        }
    }

    pub fn begin_selection(&mut self) {
        self.selection = Some(self.range.start)
    }

    pub fn clear_selection(&mut self) {
        self.selection = None;
    }

    pub fn select_all<'a>(&mut self, text: &impl TextStorage<'a>) {
        self.move_to_start_of_buffer(text);
        self.selection = Some(text.len_chars());
    }

    // pub fn move_up(&mut self, text: impl TextStorage) {
    //     let current_line_index = text.char_to_line(self.range.start);
    //     if current_line_index.0 == 0 {
    //         return;
    //     }
    //     self.move_vertically(text, current_line_index, current_line_index - 1);
    // }

    // pub fn move_up_n(&mut self, text: &impl TextStorage, n: usize) {
    //     for _ in 0..n {
    //         self.move_up(text);
    //     }
    // }

    // pub fn move_down(&mut self, text: impl TextStorage) {
    //     let current_line_index = text.char_to_line(self.range.start);
    //     if current_line_index >= text.len_lines() {
    //         return;
    //     }
    //     self.move_vertically(text, current_line_index.0, current_line_index.0 + 1);
    // }

    // pub fn move_down_n(&mut self, text: impl TextStorage, n: usize) {
    //     for _ in 0..n {
    //         self.move_down(text);
    //     }
    // }

    pub fn move_left<'a>(&mut self, text: &impl TextStorage<'a>) {
        let previous_grapheme_start = text.prev_grapheme_boundary(self.range.start);
        if previous_grapheme_start == CharIndex(0) && self.range.start == CharIndex(0) {
            return;
        }

        self.range = previous_grapheme_start..self.range.start;
        self.visual_horizontal_offset = None;
    }

    pub fn move_right<'a>(&mut self, text: &impl TextStorage<'a>) {
        let grapheme_start = self.range.end;
        let grapheme_end = text.next_grapheme_boundary(grapheme_start);
        if grapheme_start != grapheme_end {
            self.range = grapheme_start..grapheme_end;
        }
        self.visual_horizontal_offset = None;
    }

    pub fn move_right_n<'a>(&mut self, text: &impl TextStorage<'a>, n: usize) {
        for _ in 0..n {
            self.move_right(text);
        }
    }

    pub fn move_to_start_of_line<'a>(&mut self, text: &impl TextStorage<'a>) {
        let line_index = text.char_to_line(self.range.start);
        let char_index = text.line_to_char(line_index);
        self.range = char_index..text.next_grapheme_boundary(char_index);
        self.visual_horizontal_offset = None;
    }

    pub fn move_to_end_of_line<'a>(&mut self, text: &'a impl TextStorage<'a>) {
        self.range = {
            let line_index = text.char_to_line(
                cmp::min(text.len_chars().0.saturating_sub(1), self.range.start.0).into(),
            );
            let line_length = text.line(line_index).len_chars();
            let char_index = text.line_to_char(line_index);
            (char_index + line_length).saturating_sub(1.into())..char_index + line_length
        };
        self.visual_horizontal_offset = None;
    }

    pub fn move_to_start_of_buffer<'a>(&mut self, text: &impl TextStorage<'a>) {
        self.range = CharIndex(0)..text.next_grapheme_boundary(CharIndex(0));
        self.visual_horizontal_offset = None;
    }

    pub fn move_to_end_of_buffer<'a>(&mut self, text: &impl TextStorage<'a>) {
        self.range = text.prev_grapheme_boundary(text.len_chars())..text.len_chars();
        self.visual_horizontal_offset = None;
    }

    pub fn insert_char<'a>(&mut self, text: &mut impl TextStorageMut<'a>, character: char) {
        text.insert_char(self.range.start, character);
        ensure_trailing_newline_with_content(text);
    }

    pub fn insert_chars<'a>(
        &mut self,
        text: &mut impl TextStorageMut<'a>,
        characters: impl Iterator<Item = char>,
    ) {
        characters.enumerate().for_each(|(offset, character)| {
            text.insert_char(self.range.start + offset.into(), character);
        });
        ensure_trailing_newline_with_content(text);
    }

    //     // pub fn insert_slice<StorageT>(&mut self, text: &mut StorageT, slice: StorageT::Slice)
    //     // where
    //     //     StorageT: TextStorage,
    //     // {
    //     //     let mut cursor_start = self.range.start;
    //     //     for chunk in slice.chunks() {
    //     //         text.insert(cursor_start.0, chunk);
    //     //         cursor_start.0 += chunk.chars().count();
    //     //     }
    //     //     // TODO: make sure cursor start is aligned to grapheme boundary
    //     //     self.range = cursor_start..next_grapheme_boundary(&text.slice(..), cursor_start);
    //     // }

    pub fn delete<'a>(&mut self, text: &mut impl TextStorageMut<'a>) {
        if text.len_chars() == 0.into()
            || self.range.start == text.len_chars().saturating_sub(1.into())
        {
            return;
        }
        text.remove(self.range.start.0..self.range.end.0);

        let grapheme_start = self.range.start;
        let grapheme_end = text.next_grapheme_boundary(self.range.start);
        if grapheme_start < grapheme_end {
            self.range = grapheme_start..grapheme_end
        } else {
            self.range = CharIndex(0)..CharIndex(1)
        }
        ensure_trailing_newline_with_content(text);
    }

    pub fn delete_line<'a>(&mut self, text: &mut impl TextStorageMut<'a>) {
        if text.len_chars() == 0.into() {
            return;
        }

        // Delete line
        let line_index = text.char_to_line(self.range.start);
        let delete_range_start = text.line_to_char(line_index);
        let delete_range_end = text.line_to_char(line_index + 1.into());
        text.remove(delete_range_start.0..delete_range_end.0);

        // Update cursor position
        let grapheme_start = text.line_to_char(cmp::min(
            line_index,
            text.len_lines().saturating_sub(2.into()),
        ));
        let grapheme_end = text.next_grapheme_boundary(grapheme_start);
        if grapheme_start != grapheme_end {
            self.range = grapheme_start..grapheme_end
        } else {
            self.range = CharIndex(0)..CharIndex(1)
        }
    }

    //     pub fn delete_selection(&mut self, text: &mut impl TextStorage) {
    //         // Delete selection
    //         let selection = self.selection();
    //         text.remove(selection.start.0..selection.end.0);

    //         // Update cursor position
    //         let grapheme_start = cmp::min(
    //             self.range.start,
    //             prev_grapheme_boundary(&text.slice(..), CharIndex(text.len_chars())),
    //         );
    //         let grapheme_end = next_grapheme_boundary(&text.slice(..), grapheme_start);
    //         if grapheme_start != grapheme_end {
    //             self.range = grapheme_start..grapheme_end
    //         } else {
    //             self.range = CharIndex(0)..CharIndex(1)
    //         }
    //         self.clear_selection();
    //         self.visual_horizontal_offset = None;
    //     }

    pub fn backspace<'a>(&mut self, text: &mut impl TextStorageMut<'a>) {
        if self.range.start.0 > 0 {
            self.move_left(text);
            self.delete(text)
        }
    }

    pub fn sync<'a>(
        &mut self,
        current_text: &'a impl TextStorage<'a>,
        new_text: &'a impl TextStorage<'a>,
    ) {
        let current_line = current_text.char_to_line(self.range.start);
        let current_line_offset = self.range.start - current_text.line_to_char(current_line);

        let new_line = cmp::min(current_line, new_text.len_lines().saturating_sub(1.into()));
        let new_line_offset = cmp::min(
            current_line_offset,
            new_text.line(new_line).len_chars().saturating_sub(1.into()),
        );
        let grapheme_end =
            new_text.next_grapheme_boundary(new_text.line_to_char(new_line) + new_line_offset);
        let grapheme_start = new_text.prev_grapheme_boundary(grapheme_end);

        self.range = if grapheme_start != grapheme_end {
            grapheme_start..grapheme_end
        } else {
            CharIndex(0)..CharIndex(1)
        };
        self.visual_horizontal_offset = None;
        self.selection = None;
    }

    // fn move_vertically(
    //     &mut self,
    //     text: impl TextStorage,
    //     current_line_index: LineIndex,
    //     new_line_index: LineIndex,
    // ) {
    //     if new_line_index >= text.len_lines() {
    //         return;
    //     }

    //     let current_line_start = text.line_to_char(current_line_index).0;
    //     let cursor_range_start = self.range.start.0;
    //     // Should be grapheme width, not len
    //     let current_visual_x = self.visual_horizontal_offset.get_or_insert_with(|| {
    //         text.slice(current_line_start..cursor_range_start)
    //             .len_graphemes()
    //     });

    //     let new_line = text.line(new_line_index);
    //     let mut graphemes = new_line.graphemes();
    //     let mut new_visual_x = 0;
    //     while let Some(grapheme) = graphemes.next() {
    //         // Should be grapheme width, not len
    //         let width = grapheme.len_graphemes();
    //         if new_visual_x + width > *current_visual_x {
    //             break;
    //         }
    //         new_visual_x += width;
    //     }

    //     let new_line_offset = CharIndex(
    //         text.byte_to_char(text.line_to_byte(new_line_index) + graphemes.cursor.cur_cursor()),
    //     );

    //     self.range = if new_visual_x <= *current_visual_x {
    //         let grapheme_start = prev_grapheme_boundary(&text.slice(..), new_line_offset);
    //         let grapheme_end = next_grapheme_boundary(&text.slice(..), grapheme_start);
    //         grapheme_start..grapheme_end
    //     } else {
    //         let grapheme_end = next_grapheme_boundary(&text.slice(..), new_line_offset);
    //         let grapheme_start = prev_grapheme_boundary(&text.slice(..), grapheme_end);
    //         grapheme_start..grapheme_end
    //     }
    // }
}

pub(crate) fn ensure_trailing_newline_with_content<'a>(text: &mut impl TextStorageMut<'a>) {
    if text.len_chars() == 0.into() || text.char(text.len_chars() - 1.into()) != '\n' {
        text.insert_char(text.len_chars(), '\n');
    }
}