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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::cmp::Ordering;

use crate::prelude::*;
use crate::{
    editing::cursor::{Adjustable, Cursor, CursorAdjustment},
    editing::rope::EditRope,
    util::sort2,
};

/// A selection is an extendable range of text within a buffer.
pub type Selection = (Cursor, Cursor, TargetShape);

/// Multiple extendable ranges within a buffer.
pub type Selections = Vec<Selection>;

/// Current position and selection state for a member of a cursor group.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CursorState {
    /// Current position of a cursor.
    Location(Cursor),

    /// The current position of a cursor, and the anchor and shape of its selection.
    Selection(Cursor, Cursor, TargetShape),
}

impl CursorState {
    /// Get a reference to the anchor of a selection.
    pub fn anchor(&self) -> &Cursor {
        match self {
            CursorState::Location(cursor) => cursor,
            CursorState::Selection(_, anchor, _) => anchor,
        }
    }

    /// Get a reference to the cursor.
    pub fn cursor(&self) -> &Cursor {
        match self {
            CursorState::Location(cursor) => cursor,
            CursorState::Selection(cursor, _, _) => cursor,
        }
    }

    /// Get the shape of the selection.
    pub fn shape(&self) -> TargetShape {
        match self {
            CursorState::Location(_) => TargetShape::CharWise,
            CursorState::Selection(_, _, shape) => *shape,
        }
    }

    /// Convert this to a [CursorState::Location] if it isn't already.
    pub fn unselect(&mut self) {
        match self {
            CursorState::Location(_) => return,
            CursorState::Selection(cursor, _, _) => {
                *self = CursorState::Location(cursor.clone());
            },
        }
    }

    /// Indicates if this is a [CursorState::Selection].
    pub fn is_selection(&self) -> bool {
        matches!(self, CursorState::Selection(..))
    }

    /// Set fields in this [CursorState] using those in another.
    pub fn set(&mut self, update: CursorState) {
        match self {
            CursorState::Location(_) => *self = update,
            CursorState::Selection(ref mut cursor, ref mut anchor, ref mut shape) => {
                match update {
                    CursorState::Location(c) => *cursor = c,
                    CursorState::Selection(c, a, s) => {
                        *cursor = c;
                        *anchor = a;
                        *shape = s;
                    },
                }
            },
        }
    }

    /// Set the cursor.
    pub fn set_cursor(&mut self, new: Cursor) {
        match self {
            CursorState::Location(ref mut cursor) => *cursor = new,
            CursorState::Selection(ref mut cursor, _, _) => *cursor = new,
        }
    }

    /// Set the anchor for the selection.
    ///
    /// The value will be converted to a [CursorState::Selection] if needed.
    pub fn set_anchor(&mut self, anchor: Cursor) {
        match self {
            CursorState::Location(cursor) => {
                *self = CursorState::Selection(cursor.clone(), anchor, TargetShape::CharWise);
            },
            CursorState::Selection(_, ref mut a, _) => {
                *a = anchor;
            },
        }
    }

    /// Set the shape of the selection.
    ///
    /// The value will be converted to a [CursorState::Selection] if needed.
    pub fn set_shape(&mut self, shape: TargetShape) {
        match self {
            CursorState::Location(cursor) => {
                *self = CursorState::Selection(cursor.clone(), cursor.clone(), shape);
            },
            CursorState::Selection(_, _, ref mut s) => {
                *s = shape;
            },
        }
    }

    /// Return a sorted pair of the cursor and anchor.
    pub fn sorted(&self) -> (Cursor, Cursor) {
        match self {
            CursorState::Location(cursor) => (cursor.clone(), cursor.clone()),
            CursorState::Selection(cursor, anchor, _) => sort2(cursor.clone(), anchor.clone()),
        }
    }

    /// Swap the cursor and anchor.
    pub fn swap(&mut self) {
        if let CursorState::Selection(ref mut cursor, ref mut anchor, _) = self {
            std::mem::swap(cursor, anchor);
        }
    }

    /// If this is a [CursorState::Selection], return it as a [Selection].
    pub fn to_selection(&self) -> Option<Selection> {
        match self {
            CursorState::Location(_) => None,
            CursorState::Selection(cursor, anchor, shape) => {
                let (sels, sele) = sort2(cursor.clone(), anchor.clone());

                Some((sels, sele, *shape))
            },
        }
    }

    /// Return a triple of cursor, anchor, and shape.
    pub fn to_triple(&self) -> (Cursor, Cursor, TargetShape) {
        match self {
            CursorState::Location(cursor) => {
                (cursor.clone(), cursor.clone(), TargetShape::CharWise)
            },
            CursorState::Selection(cursor, anchor, shape) => {
                (cursor.clone(), anchor.clone(), *shape)
            },
        }
    }

    /// The number of characters contained by the selection.
    pub fn len(&self, rope: &EditRope) -> usize {
        match self {
            CursorState::Location(_) => 1,
            CursorState::Selection(ref cursor, ref anchor, _) => {
                let (lc, rc) = sort2(cursor, anchor);
                let loff = rope.cursor_to_offset(lc);
                let roff = rope.cursor_to_offset(rc);

                rope.chars_until(loff, roff).count()
            },
        }
    }

    /// Indicates the direction of the cursor relatie to the anchor.
    pub fn direction(&self) -> MoveDir1D {
        match self {
            CursorState::Location(_) => MoveDir1D::Next,
            CursorState::Selection(cursor, anchor, _) => {
                if cursor < anchor {
                    MoveDir1D::Previous
                } else {
                    MoveDir1D::Next
                }
            },
        }
    }

    /// Returns the cursor or the anchor, depending on which comes earlier in the text.
    pub fn start(&self) -> &Cursor {
        match self {
            CursorState::Location(cursor) => cursor,
            CursorState::Selection(cursor, anchor, _) => sort2(&cursor, &anchor).0,
        }
    }

    /// Returns the cursor or the anchor, depending on which comes later in the text.
    pub fn end(&self) -> &Cursor {
        match self {
            CursorState::Location(cursor) => cursor,
            CursorState::Selection(cursor, anchor, _) => sort2(&cursor, &anchor).1,
        }
    }

    /// Returns the intersection of this selection with another.
    pub fn overlaps(&self, other: &Self) -> bool {
        let (amin, amax) = (self.start(), self.end());
        let (bmin, bmax) = (other.start(), other.end());

        (amin <= bmin && bmin <= amax) || (bmin <= amin && amin <= bmax)
    }

    /// Returns the intersection of this selection with another.
    pub fn intersect(&self, other: &Self) -> Self {
        let direction = other.direction();
        let shape = other.shape();

        let lc = sort2(self.start(), other.start()).1;
        let rc = sort2(self.end(), other.end()).0;

        let (lc, rc) = if lc <= rc {
            (lc.clone(), rc.clone())
        } else {
            (lc.clone(), lc.clone())
        };

        match direction {
            MoveDir1D::Previous => CursorState::Selection(lc, rc, shape),
            MoveDir1D::Next => CursorState::Selection(rc, lc, shape),
        }
    }

    /// Returns the union of this selection with another.
    pub fn union(&self, other: &Self) -> Self {
        let direction = other.direction();
        let shape = other.shape();

        let lc = sort2(self.start(), other.start()).0.clone();
        let rc = sort2(self.end(), other.end()).1.clone();

        match direction {
            MoveDir1D::Previous => CursorState::Selection(lc, rc, shape),
            MoveDir1D::Next => CursorState::Selection(rc, lc, shape),
        }
    }

    /// Merge this selection with another.
    pub fn merge(&self, other: &Self, style: &CursorMergeStyle, rope: &EditRope) -> Self {
        match style {
            CursorMergeStyle::Union => {
                return self.union(other);
            },
            CursorMergeStyle::Intersect => {
                return self.intersect(other);
            },
            CursorMergeStyle::SelectLong => {
                let slen = self.len(rope);
                let olen = other.len(rope);

                if slen > olen {
                    return self.clone();
                } else {
                    return other.clone();
                }
            },
            CursorMergeStyle::SelectShort => {
                let slen = self.len(rope);
                let olen = other.len(rope);

                if slen < olen {
                    return self.clone();
                } else {
                    return other.clone();
                }
            },
            CursorMergeStyle::SelectCursor(MoveDir1D::Previous) => {
                if self.cursor() < other.cursor() {
                    return self.clone();
                } else {
                    return other.clone();
                }
            },
            CursorMergeStyle::SelectCursor(MoveDir1D::Next) => {
                if self.cursor() > other.cursor() {
                    return self.clone();
                } else {
                    return other.clone();
                }
            },
        }
    }
}

impl From<Cursor> for CursorState {
    fn from(cursor: Cursor) -> Self {
        CursorState::Location(cursor)
    }
}

impl Adjustable for CursorState {
    fn zero(&mut self) {
        match self {
            CursorState::Location(cursor) => cursor.zero(),
            CursorState::Selection(cursor, anchor, _) => {
                cursor.zero();
                anchor.zero();
            },
        }
    }

    fn adjust(&mut self, adj: &[CursorAdjustment]) {
        match self {
            CursorState::Location(cursor) => cursor.adjust(adj),
            CursorState::Selection(cursor, anchor, _) => {
                cursor.adjust(adj);
                anchor.adjust(adj);
            },
        }
    }
}

impl Default for CursorState {
    fn default() -> Self {
        CursorState::Location(Cursor::new(0, 0))
    }
}

impl PartialOrd for CursorState {
    fn partial_cmp(&self, other: &CursorState) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for CursorState {
    fn cmp(&self, other: &CursorState) -> Ordering {
        match (self, other) {
            (CursorState::Location(c1), CursorState::Location(c2)) => c1.cmp(c2),
            (CursorState::Location(c1), CursorState::Selection(c2, ..)) => c1.cmp(c2),
            (CursorState::Selection(c1, ..), CursorState::Location(c2)) => c1.cmp(c2),
            (CursorState::Selection(c1, a1, _), CursorState::Selection(c2, a2, _)) => {
                c1.cmp(c2).then(a1.cmp(a2))
            },
        }
    }
}

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

    #[test]
    fn test_overlaps() {
        let cw = TargetShape::CharWise;
        let s1 = CursorState::Selection(Cursor::new(0, 2), Cursor::new(0, 5), cw);
        let s2 = CursorState::Selection(Cursor::new(2, 4), Cursor::new(2, 7), cw);
        let s3 = CursorState::Selection(Cursor::new(0, 0), Cursor::new(0, 3), cw);
        let s4 = CursorState::Selection(Cursor::new(0, 6), Cursor::new(2, 3), cw);
        let s5 = CursorState::Selection(Cursor::new(3, 0), Cursor::new(3, 5), cw);
        let s6 = CursorState::Selection(Cursor::new(5, 0), Cursor::new(2, 7), cw);

        // s1 only overlaps w/ s3.
        assert_eq!(s1.overlaps(&s2), false);
        assert_eq!(s1.overlaps(&s3), true);
        assert_eq!(s1.overlaps(&s4), false);
        assert_eq!(s1.overlaps(&s5), false);
        assert_eq!(s1.overlaps(&s6), false);

        // s2 only overlaps w/ s6.
        assert_eq!(s2.overlaps(&s3), false);
        assert_eq!(s2.overlaps(&s4), false);
        assert_eq!(s2.overlaps(&s5), false);
        assert_eq!(s2.overlaps(&s6), true);

        // s3 overlaps w/ nothing else.
        assert_eq!(s3.overlaps(&s4), false);
        assert_eq!(s3.overlaps(&s5), false);
        assert_eq!(s3.overlaps(&s6), false);

        // s4 overlaps w/ nothing else.
        assert_eq!(s4.overlaps(&s5), false);
        assert_eq!(s4.overlaps(&s6), false);

        // s5 overlaps w/ s6.
        assert_eq!(s5.overlaps(&s6), true);
    }

    #[test]
    fn test_merge_union() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Location(Cursor::new(2, 4));
        let b = CursorState::Selection(Cursor::new(1, 75), Cursor::new(3, 0), cw);
        let c = CursorState::Selection(Cursor::new(2, 30), Cursor::new(4, 6), cw);
        let u = CursorMergeStyle::Union;
        let r = EditRope::from("");

        // Merges with self.
        assert_eq!(
            a.merge(&a, &u, &r),
            CursorState::Selection(Cursor::new(2, 4), Cursor::new(2, 4), cw)
        );
        assert_eq!(b.merge(&b, &u, &r), b);
        assert_eq!(c.merge(&c, &u, &r), c);

        // a is completely surrounded by b.
        assert_eq!(a.merge(&b, &u, &r), b);

        // a is outside of c, so the start of the selection moves left.
        assert_eq!(
            a.merge(&c, &u, &r),
            CursorState::Selection(Cursor::new(2, 4), Cursor::new(4, 6), cw)
        );

        // b and c overlap.
        assert_eq!(
            b.merge(&c, &u, &r),
            CursorState::Selection(Cursor::new(1, 75), Cursor::new(4, 6), cw)
        );
    }

    #[test]
    fn test_merge_intersect() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Location(Cursor::new(2, 4));
        let b = CursorState::Selection(Cursor::new(1, 75), Cursor::new(3, 0), cw);
        let c = CursorState::Selection(Cursor::new(2, 30), Cursor::new(4, 6), cw);
        let i = CursorMergeStyle::Intersect;
        let r = EditRope::from("");

        // Merges with self.
        assert_eq!(
            a.merge(&a, &i, &r),
            CursorState::Selection(Cursor::new(2, 4), Cursor::new(2, 4), cw)
        );
        assert_eq!(b.merge(&b, &i, &r), b);
        assert_eq!(c.merge(&c, &i, &r), c);

        // a intersects b at only one point, itself.
        assert_eq!(
            a.merge(&b, &i, &r),
            CursorState::Selection(Cursor::new(2, 4), Cursor::new(2, 4), cw)
        );

        // a doesn't truly intersect with c, so we use the leftmost position of the rightmost
        // selection.
        assert_eq!(
            a.merge(&c, &i, &r),
            CursorState::Selection(Cursor::new(2, 30), Cursor::new(2, 30), cw)
        );

        // b and c overlap, so we return the intersecting range.
        assert_eq!(
            b.merge(&c, &i, &r),
            CursorState::Selection(Cursor::new(2, 30), Cursor::new(3, 0), cw)
        );
    }

    #[test]
    fn test_merge_select_long() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Selection(Cursor::new(1, 0), Cursor::new(0, 0), cw);
        let b = CursorState::Selection(Cursor::new(2, 0), Cursor::new(1, 0), cw);
        let c = CursorState::Selection(Cursor::new(3, 0), Cursor::new(2, 0), cw);
        let l = CursorMergeStyle::SelectLong;
        let r = EditRope::from("12345\n\n678\n\n");

        // Merges with self.
        assert_eq!(a.merge(&a, &l, &r), a);
        assert_eq!(b.merge(&b, &l, &r), b);
        assert_eq!(c.merge(&c, &l, &r), c);

        // Selection a is longer than b and c.
        assert_eq!(a.merge(&b, &l, &r), a);
        assert_eq!(a.merge(&c, &l, &r), a);

        // Selection c is longer than b.
        assert_eq!(b.merge(&c, &l, &r), c);
    }

    #[test]
    fn test_merge_select_short() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Selection(Cursor::new(1, 0), Cursor::new(0, 0), cw);
        let b = CursorState::Selection(Cursor::new(2, 0), Cursor::new(1, 0), cw);
        let c = CursorState::Selection(Cursor::new(3, 0), Cursor::new(2, 0), cw);
        let s = CursorMergeStyle::SelectShort;
        let r = EditRope::from("12345\n\n678\n\n");

        // Merges with self.
        assert_eq!(a.merge(&a, &s, &r), a);
        assert_eq!(b.merge(&b, &s, &r), b);
        assert_eq!(c.merge(&c, &s, &r), c);

        // Selection c is shorter than a.
        assert_eq!(a.merge(&c, &s, &r), c);

        // Selection b is shorter than a and c.
        assert_eq!(a.merge(&b, &s, &r), b);
        assert_eq!(b.merge(&c, &s, &r), b);
    }

    #[test]
    fn test_merge_select_cursor_prev() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Location(Cursor::new(2, 4));
        let b = CursorState::Selection(Cursor::new(1, 75), Cursor::new(3, 0), cw);
        let c = CursorState::Selection(Cursor::new(2, 30), Cursor::new(4, 6), cw);
        let d = CursorState::Selection(Cursor::new(5, 0), Cursor::new(0, 0), cw);
        let p = CursorMergeStyle::SelectCursor(MoveDir1D::Previous);
        let r = EditRope::from("");

        // Merges with self.
        assert_eq!(a.merge(&a, &p, &r), a);
        assert_eq!(b.merge(&b, &p, &r), b);
        assert_eq!(c.merge(&c, &p, &r), c);
        assert_eq!(d.merge(&d, &p, &r), d);

        // a, b, and c's cursors come before d's; anchor doesn't matter.
        assert_eq!(a.merge(&d, &p, &r), a);
        assert_eq!(b.merge(&d, &p, &r), b);
        assert_eq!(c.merge(&d, &p, &r), c);

        // b's cursor comes before a's.
        assert_eq!(a.merge(&b, &p, &r), b);

        // a's cursor comes before c's.
        assert_eq!(a.merge(&c, &p, &r), a);

        // b's cursor comes before c's.
        assert_eq!(b.merge(&c, &p, &r), b);
    }

    #[test]
    fn test_merge_select_cursor_next() {
        let cw = TargetShape::CharWise;
        let a = CursorState::Location(Cursor::new(2, 4));
        let b = CursorState::Selection(Cursor::new(1, 75), Cursor::new(3, 0), cw);
        let c = CursorState::Selection(Cursor::new(2, 30), Cursor::new(4, 6), cw);
        let d = CursorState::Selection(Cursor::new(0, 0), Cursor::new(5, 0), cw);
        let n = CursorMergeStyle::SelectCursor(MoveDir1D::Next);
        let r = EditRope::from("");

        // Merges with self.
        assert_eq!(a.merge(&a, &n, &r), a);
        assert_eq!(b.merge(&b, &n, &r), b);
        assert_eq!(c.merge(&c, &n, &r), c);
        assert_eq!(d.merge(&d, &n, &r), d);

        // a, b, and c's cursors come after d's; anchor doesn't matter.
        assert_eq!(a.merge(&d, &n, &r), a);
        assert_eq!(b.merge(&d, &n, &r), b);
        assert_eq!(c.merge(&d, &n, &r), c);

        // a's cursor comes after b's.
        assert_eq!(a.merge(&b, &n, &r), a);

        // c's cursor comes after a's.
        assert_eq!(a.merge(&c, &n, &r), c);

        // c's cursor comes after b's.
        assert_eq!(b.merge(&c, &n, &r), c);
    }
}