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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
mod builder;
mod cfg;
mod iter;
pub mod reader;
mod records;
mod tags;
mod types;

use std::{
    ops::{Range, RangeBounds},
    path::Path,
    str::from_utf8_unchecked,
};

use gapbuf::GapBuffer;
use point::TwoPoints;
use records::Records;

use self::tags::{Markers, RawTag, Tags};
pub use self::{
    builder::{err, hint, ok, text, AlignCenter, AlignLeft, AlignRight, Builder, Ghost},
    cfg::*,
    iter::{Item, Iter, RevIter},
    point::Point,
    tags::{Marker, Tag, ToggleId},
    types::Part,
};
use crate::{history::Change, input::Cursors};

/// The text in a given area.
#[derive(Default, Clone, Eq)]
pub struct Text {
    buf: Box<GapBuffer<u8>>,
    pub tags: Box<Tags>,
    /// This [`Marker`] is used for the addition and removal of cursor
    /// [`Tag`]s.
    marker: Marker,
    pub records: Records<(usize, usize, usize)>,
}

impl Text {
    pub fn new() -> Self {
        Self {
            buf: Box::new(GapBuffer::new()),
            tags: Box::new(Tags::new()),
            marker: Marker::base(),
            records: Records::new(),
        }
    }

    pub fn from_file(path: impl AsRef<Path>) -> Self {
        let file = std::fs::read_to_string(path).expect("File failed to open");
        let buf = Box::new(GapBuffer::from_iter(file.bytes()));
        let tags = Box::new(Tags::with_len(buf.len()));

        Self {
            buf,
            tags,
            marker: Marker::base(),
            records: Records::with_max((file.len(), file.chars().count(), file.lines().count())),
        }
    }

    pub fn builder() -> Builder {
        Builder::new()
    }

    /// Merges `String`s with the body of text, given a range to
    /// replace.
    fn replace_range(&mut self, old: Range<usize>, edit: impl AsRef<str>) {
        let edit = edit.as_ref();

        let old_start = {
            let p = self.point_at(old.start);
            (p.byte(), p.char(), p.line())
        };

        let old_len = unsafe {
            let str = String::from_utf8_unchecked(
                self.buf
                    .splice(old.clone(), edit.as_bytes().iter().cloned())
                    .collect(),
            );

            let lines = str.bytes().filter(|b| *b == b'\n').count();
            (str.len(), str.chars().count(), lines)
        };

        let new_len = {
            let lines = edit.bytes().filter(|b| *b == b'\n').count();
            (edit.len(), edit.chars().count(), lines)
        };

        self.records.transform(old_start, old_len, new_len);

        let new_end = old.start + edit.len();
        self.tags.transform(old, new_end);
    }

    pub(crate) fn insert_str(&mut self, at: usize, str: &str) {
        self.replace_range(at..at, str);
    }

    pub(crate) fn apply_change(&mut self, change: &Change) {
        self.replace_range(change.taken_range(), &change.added_text);
    }

    pub(crate) fn undo_change(&mut self, change: &Change, chars: isize) {
        let start = change.start.saturating_add_signed(chars);
        let end = change.added_end().saturating_add_signed(chars);
        self.replace_range(start..end, &change.taken_text);
    }

    fn clear(&mut self) {
        self.buf = Box::new(GapBuffer::new());
        self.tags.clear();
        self.records.clear();
    }

    pub fn insert_tag(&mut self, at: usize, tag: Tag, marker: Marker) {
        self.tags.insert(at, tag, marker);
    }

    pub fn remove_tags_on(&mut self, b: usize, markers: impl Markers) {
        self.tags.remove_at(b, markers)
    }

    /// Removes the tags for all the cursors, used before they are
    /// expected to move.
    pub(crate) fn add_cursor_tags(&mut self, cursors: &Cursors) {
        for (cursor, is_main) in cursors.iter() {
            let Range { start, end } = cursor.range();
            let (caret_tag, start_tag, end_tag) = cursor_tags(is_main);

            let tags = [
                (start, start_tag),
                (end, end_tag),
                (cursor.caret().byte(), caret_tag),
            ];

            let no_selection = if start == end { 2 } else { 0 };

            for (b, tag) in tags.into_iter().skip(no_selection) {
                let point = self.point_at(b);
                let record = (point.byte(), point.char(), point.line());
                self.records.insert(record);
                self.tags.insert(b, tag, self.marker);
            }
        }
    }

    /// Adds the tags for all the cursors, used after they are
    /// expected to have moved.
    pub(crate) fn remove_cursor_tags(&mut self, cursors: &Cursors) {
        for (cursor, _) in cursors.iter() {
            let Range { start, end } = cursor.range();
            let skip = if start == end { 1 } else { 0 };
            for ch_index in [start, end].into_iter().skip(skip) {
                self.tags.remove_at(ch_index, self.marker);
            }
        }
    }

    pub(crate) fn write_to(&self, mut writer: impl std::io::Write) -> std::io::Result<usize> {
        let (s0, s1) = self.buf.as_slices();
        Ok(writer.write(s0)? + writer.write(s1)?)
    }

    pub fn slices(&self) -> (&'_ str, &'_ str) {
        let (s0, s1) = self.buf.as_slices();
        unsafe { (from_utf8_unchecked(s0), from_utf8_unchecked(s1)) }
    }

    pub fn strs_in_range(
        &self,
        range: impl RangeBounds<usize> + std::fmt::Debug,
    ) -> (&'_ str, &'_ str) {
        let (s0, s1) = self.buf.as_slices();
        let (start, end) = get_ends(range, self.len_bytes());

        unsafe {
            let r0 = start.min(s0.len())..end.min(s0.len());
            let r1 = start.saturating_sub(s0.len())..end.saturating_sub(s0.len());

            (from_utf8_unchecked(&s0[r0]), from_utf8_unchecked(&s1[r1]))
        }
    }

    pub fn tags(&self) -> impl Iterator<Item = (usize, RawTag)> + '_ {
        self.tags.iter_at(0)
    }

    pub fn tags_at(&self, at: usize) -> impl Iterator<Item = (usize, RawTag)> + Clone + '_ {
        self.tags.iter_at(at)
    }

    pub fn chars(&self) -> impl Iterator<Item = char> + Clone + '_ {
        let (s0, s1) = self.buf.as_slices();
        let (s0, s1) = unsafe { (from_utf8_unchecked(s0), from_utf8_unchecked(s1)) };

        s0.chars().chain(s1.chars())
    }

    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    #[inline(always)]
    pub fn point_at(&self, at: usize) -> Point {
        assert!(
            at <= self.len_bytes(),
            "byte out of bounds: the len is {}, but the byte is {at}",
            self.len_bytes()
        );
        let (b, c, mut l) = self.records.closest_to(at);

        let found = if at >= b {
            let (s0, s1) = self.strs_in_range(b..);

            s0.char_indices()
                .chain(s1.char_indices().map(|(b, char)| (b + s0.len(), char)))
                .enumerate()
                .map(|(i, (this_b, char))| {
                    l += (char == '\n') as usize;
                    (b + this_b, c + i, l - (char == '\n') as usize)
                })
                .take_while(|&(b, ..)| at >= b)
                .last()
        } else {
            let (s0, s1) = self.strs_in_range(..b);
            let s1 = s1.chars().rev();
            let mut c_len = 0;

            s1.chain(s0.chars().rev())
                .enumerate()
                .map(|(i, char)| {
                    l -= (char == '\n') as usize;
                    c_len += char.len_utf8();
                    (b - c_len, c - (i + 1), l)
                })
                .take_while(|&(b, ..)| b >= at)
                .last()
        };

        found
            .map(|(b, c, l)| Point::from_coords(b, c, l))
            .unwrap_or(self.max_point())
    }

    #[inline(always)]
    pub fn ghost_max_points_at(&self, at: usize) -> (Point, Option<Point>) {
        let point = self.point_at(at);
        (point, self.tags.ghosts_total_at(point.byte()))
    }

    /// Points visually after the [`TwoPoints`]
    ///
    /// If the [`TwoPoints`] in question is concealed, treats the
    /// next visible character as the first character, and returns
    /// the points of the next visible character.
    ///
    /// This method is useful if you want to iterater reversibly
    /// right after a certain point, thus including the character
    /// of said point.
    pub fn points_after(&self, tp: impl TwoPoints) -> Option<(Point, Option<Point>)> {
        self.iter_at(tp)
            .filter_map(|item| item.part.as_char().map(|_| item.points()))
            .nth(1)
    }

    pub fn len_bytes(&self) -> usize {
        self.buf.len()
    }

    pub fn len_chars(&self) -> usize {
        self.records.max().1
    }

    pub fn len_lines(&self) -> usize {
        self.records.max().2
    }

    pub fn max_point(&self) -> Point {
        let (b, c, l) = self.records.max();
        Point::from_coords(b, c, l)
    }

    pub fn max_points(&self) -> (Point, Option<Point>) {
        self.ghost_max_points_at(self.max_point().byte())
    }

    /// The visual start of the line
    ///
    /// This point is defined not by where the line actually begins,
    /// but by where the last '\n' was located. For example, if
    /// [`Tag`]s create ghost text or ommit text from multiple
    /// different lines, this point may differ from where in the
    /// [`Text`] the physical line actually begins.
    pub fn visual_line_start(&self, p: impl TwoPoints) -> (Point, Option<Point>) {
        let (real, ghost) = p.to_points();

        // NOTE: 20000 is a magic number, being a guess for what a reasonable
        // limit would be.
        let mut iter = self.rev_iter_at(real).peekable();
        let mut points = (real, ghost);
        while let Some(peek) = iter.peek() {
            match peek.part {
                Part::Char('\n') => return points,
                Part::Char(_) => points = iter.next().unwrap().to_points(),
                _ => drop(iter.next()),
            }
        }

        points
    }

    /// Clones the inner [`GapBuffer`] as a [`String`]
    // NOTE: Inherent because I don't want this to implement Display
    #[allow(clippy::inherent_to_string)]
    pub fn to_string(&self) -> String {
        let (s0, s1) = self.strs_in_range(..);
        s0.to_string() + s1
    }
}

// Iterator methods.
impl Text {
    pub fn iter(&self) -> Iter<'_> {
        Iter::new_at(self, Point::default())
    }

    pub fn iter_at(&self, p: impl TwoPoints) -> Iter<'_> {
        Iter::new_at(self, p)
    }

    pub fn rev_iter(&self) -> RevIter {
        RevIter::new_at(self, self.max_point())
    }

    pub fn rev_iter_at(&self, p: impl TwoPoints) -> RevIter<'_> {
        RevIter::new_at(self, p)
    }

    pub fn iter_bytes_at(&self, b: usize) -> impl Iterator<Item = u8> + '_ {
        let (s0, s1) = self.strs_in_range(b..);
        s0.bytes().chain(s1.bytes())
    }

    pub fn iter_chars_at(&self, c: usize) -> impl Iterator<Item = char> + '_ {
        let (s0, s1) = self.strs_in_range(..);
        s0.chars().chain(s1.chars()).skip(c)
    }
}

impl<S> From<S> for Text
where
    S: ToString,
{
    fn from(value: S) -> Self {
        let value = value.to_string();
        let buf = Box::new(GapBuffer::from_iter(value.bytes()));
        let tags = Box::new(Tags::with_len(buf.len()));

        Self {
            buf,
            tags,
            marker: Marker::new(),
            records: Records::with_max((value.len(), value.chars().count(), value.lines().count())),
        }
    }
}

impl std::fmt::Debug for Text {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Text")
            .field(
                "buf",
                &format!("'{}', '{}'", self.slices().0, self.slices().1),
            )
            .field("tags", &self.tags)
            .field("records", &self.records)
            .finish()
    }
}

impl PartialEq for Text {
    fn eq(&self, other: &Self) -> bool {
        self.buf == other.buf && self.tags == other.tags
    }
}

mod point {
    use super::Item;
    /// A position in a [`Text`].
    #[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub struct Point {
        b: usize,
        c: usize,
        l: usize,
    }

    impl Point {
        /// Returns a new [`Point`], at the first byte.
        pub fn new() -> Self {
            Self::default()
        }

        pub(super) fn from_coords(b: usize, c: usize, l: usize) -> Self {
            Self { b, c, l }
        }

        pub(super) fn fwd(self, char: char) -> Self {
            Self {
                b: self.b + char.len_utf8(),
                c: self.c + 1,
                l: self.l + (char == '\n') as usize,
            }
        }

        pub(super) fn rev(self, char: char) -> Self {
            Self {
                b: self.b - char.len_utf8(),
                c: self.c - 1,
                l: self.l - (char == '\n') as usize,
            }
        }

        /// Returns the byte (relative to the beginning of the file)
        /// of self. Indexed at 0.
        pub fn byte(&self) -> usize {
            self.b
        }

        /// Returns the char index (relative tow the beginning of the
        /// file). Indexed at 0.
        pub fn char(&self) -> usize {
            self.c
        }

        /// Returns the line. Indexed at 0.
        pub fn line(&self) -> usize {
            self.l
        }
    }

    pub trait TwoPoints: std::fmt::Debug + Clone + Copy {
        fn to_points(self) -> (Point, Option<Point>);
    }

    impl TwoPoints for Point {
        fn to_points(self) -> (Point, Option<Point>) {
            (self, None)
        }
    }

    impl TwoPoints for (Point, Point) {
        fn to_points(self) -> (Point, Option<Point>) {
            (self.0, Some(self.1))
        }
    }

    impl TwoPoints for (Point, Option<Point>) {
        fn to_points(self) -> (Point, Option<Point>) {
            self
        }
    }

    impl TwoPoints for Item {
        fn to_points(self) -> (Point, Option<Point>) {
            (self.real, self.ghost)
        }
    }

    impl std::ops::Add for Point {
        type Output = Self;

        fn add(self, rhs: Self) -> Self::Output {
            Self {
                b: self.b + rhs.b,
                c: self.c + rhs.c,
                l: self.l + rhs.l,
            }
        }
    }

    impl std::ops::AddAssign for Point {
        fn add_assign(&mut self, rhs: Self) {
            *self = *self + rhs;
        }
    }

    impl std::ops::Sub for Point {
        type Output = Self;

        fn sub(self, rhs: Self) -> Self::Output {
            Self {
                b: self.b - rhs.b,
                c: self.c - rhs.c,
                l: self.l - rhs.l,
            }
        }
    }

    impl std::ops::SubAssign for Point {
        fn sub_assign(&mut self, rhs: Self) {
            *self = *self - rhs;
        }
    }
}

fn cursor_tags(is_main: bool) -> (Tag, Tag, Tag) {
    use tags::Tag::{ExtraCursor, MainCursor, PopForm, PushForm};

    use crate::palette::{EXTRA_SEL, MAIN_SEL};

    if is_main {
        (MainCursor, PushForm(MAIN_SEL), PopForm(MAIN_SEL))
    } else {
        (ExtraCursor, PushForm(EXTRA_SEL), PopForm(EXTRA_SEL))
    }
}

pub fn get_ends(range: impl std::ops::RangeBounds<usize>, max: usize) -> (usize, usize) {
    let start = match range.start_bound() {
        std::ops::Bound::Included(start) => *start,
        std::ops::Bound::Excluded(start) => *start + 1,
        std::ops::Bound::Unbounded => 0,
    };
    let end = match range.end_bound() {
        std::ops::Bound::Included(end) => *end + 1,
        std::ops::Bound::Excluded(end) => *end,
        std::ops::Bound::Unbounded => max,
    };

    (start, end)
}