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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
use std::cmp::Ordering;
use std::collections::HashMap;
use std::slice::{Iter, IterMut};
use log::{error, warn};
use crate::cursor::cursor::{
default_word_determinant, BackwardWordDeterminant, Cursor, CursorStatus, ForwardWordDeterminant, NEWLINE_WIDTH, ZERO_CURSOR,
};
use crate::primitives::has_invariant::HasInvariant;
use crate::text::text_buffer::TextBuffer;
// INVARIANTS:
// - non-empty this is so I can use cursor for anchoring and call "supercursor" easily
// - cursors are distinct
// - cursors have their anchors either on begin or on end, and they all have the anchor on the same
// side
// - cursors DO NOT OVERLAP
// - cursors are SORTED by their anchor
// TODO add invariants:
// - sort cursors by anchor (they don't overlap, so it's easy)
// - (maybe) add "supercursor", which is always the first or the last, depending on which direction they were moved.
// it would help with anchoring.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct CursorSet {
set: Vec<Cursor>,
}
impl CursorSet {
pub fn single() -> Self {
CursorSet {
set: vec![Cursor::single()],
}
}
pub fn new(set: Vec<Cursor>) -> Self {
CursorSet { set }
}
/*
This is singleton in set theory sense, not "design pattern"
*/
pub fn singleton(cursor: Cursor) -> Self {
CursorSet { set: vec![cursor] }
}
pub fn set(&self) -> &Vec<Cursor> {
&self.set
}
pub fn set_mut(&mut self) -> &mut Vec<Cursor> {
&mut self.set
}
// Returns only element OR None if the set is NOT a singleton.
pub fn as_single(&self) -> Option<Cursor> {
if self.set.len() != 1 {
None
} else {
self.set.first().copied()
}
}
pub fn as_single_mut(&mut self) -> Option<&mut Cursor> {
if self.set.len() != 1 {
None
} else {
self.set.first_mut()
}
}
// Returns largest index either under the cursor or *within* selection.
pub fn max_cursor_pos(&self) -> usize {
let mut max: usize = 0;
for c in self.set.iter() {
max = usize::max(max, c.a);
if let Some(sel) = c.s {
debug_assert!(sel.b < sel.e);
max = usize::max(max, usize::max(sel.b, sel.e));
}
}
max
}
pub fn move_left(&mut self, selecting: bool) -> bool {
self.move_left_by(1, selecting)
}
pub fn move_left_by(&mut self, l: usize, selecting: bool) -> bool {
let mut res = false;
for c in self.set.iter_mut() {
if c.a > 0 {
let old_pos = c.a;
c.a -= std::cmp::min(c.a, l);
if selecting {
c.update_select(old_pos, c.a);
c.preferred_column = None;
} else {
c.clear_both();
};
res = true;
}
}
// TODO test
if selecting {
// we will test for overlaps, and cut them. Since this is a move left, we cut a piece
// from right side.
for i in 0..self.set.len() - 1 {
let right = self.set[i + 1];
let left = &mut self.set[i];
if let (Some(left_s), Some(right_s)) = (&mut left.s, right.s) {
// always remember: left index inclusive, right exclusive.
if left_s.e > right_s.b {
left_s.e = right_s.b;
if left_s.b >= left_s.e {
left.s = None;
}
}
}
}
}
self.reduce_left();
res
}
pub fn move_right(&mut self, rope: &dyn TextBuffer, selecting: bool) -> bool {
self.move_right_by(rope, 1, selecting)
}
pub fn move_right_by(&mut self, rope: &dyn TextBuffer, l: usize, selecting: bool) -> bool {
if self.max_cursor_pos() > rope.len_chars() {
error!("buffer shorter than cursor positions. Returning prematurely to avoid crash.");
return false;
}
let len = rope.len_chars();
let mut res = false;
for c in &mut self.set {
//we allow anchor after last char (so you can backspace last char)
if c.a < len {
let old_pos = c.a;
c.a = std::cmp::min(c.a + l, len);
if selecting {
c.update_select(old_pos, c.a);
} else {
c.clear_both();
}
res = true;
};
}
if selecting {
// we will test for overlaps, and cut them. Since this is a move right, we cut a piece
// from left side. I proceed in reverse order, so if setting selection to None I don't
// destroy data I need to use in next pair, introducing glittering.
for i in (0..self.set.len() - 1).rev() {
let left = self.set[i];
let right = &mut self.set[i + 1];
if let (Some(left_s), Some(right_s)) = (left.s, &mut right.s) {
// always remember: left index inclusive, right exclusive.
if left_s.e > right_s.b {
right_s.b = left_s.e;
if right_s.b >= right_s.e {
right.s = None;
}
}
}
}
}
self.reduce_right();
res
}
pub fn move_vertically_by(&mut self, rope: &dyn TextBuffer, l: isize, selecting: bool) -> bool {
if self.max_cursor_pos() > rope.len_chars() {
error!("buffer shorter than cursor positions. Returning prematurely to avoid crash.");
return false;
}
if l == 0 {
return false;
}
let mut res = false;
let last_line_idx = rope.len_lines() - 1;
for c in &mut self.set {
//getting data
if !selecting {
c.clear_selection();
}
let cur_line_idx = match rope.char_to_line(c.a) {
Some(line) => line,
None => {
// If c.a == rope.len_chars(), rope (which is underlying impl) does not fail
// (see rope_tests.rs). Otherwise we would have to check whether last character
// is in fact a newline or not, as this would affect the result.
rope.len_lines()
}
};
let cur_line_begin_char_idx = match rope.line_to_char(cur_line_idx) {
Some(idx) => idx,
None => {
error!("rope.line_to_char failed unexpectedly (1), skipping cursor.");
continue;
}
};
let current_col_idx = c.a - cur_line_begin_char_idx;
// line beyond the end of buffer
if cur_line_idx as isize + l > last_line_idx as isize {
if c.a == rope.len_chars() {
continue;
}
c.preferred_column = Some(current_col_idx);
let old_pos = c.a;
c.a = rope.len_chars(); // pointing to index higher than last valid one.
if selecting {
c.update_select(old_pos, c.a);
}
res = true;
continue;
}
// can't scroll that much up, begin of file is best we can get.
if cur_line_idx as isize + l < 0 {
if c.a == 0 {
continue;
}
c.preferred_column = Some(current_col_idx);
let old_pos = c.a;
c.a = 0;
if selecting {
c.update_select(old_pos, c.a);
}
res = true;
continue;
}
// at this point we know that 0 <= cur_line_idx <= last_line_idx
debug_assert!(cur_line_idx <= last_line_idx);
let new_line_idx = (cur_line_idx as isize + l) as usize;
// This is actually right. Ropey counts '\n' as last character of current line.
let last_char_in_new_line_idx = if new_line_idx == last_line_idx {
//this corresponds to a notion of "potential new character" beyond the buffer. It's a valid cursor
// position.
rope.len_chars()
} else {
match rope.line_to_char(new_line_idx + 1) {
Some(char_idx) => char_idx - NEWLINE_WIDTH as usize,
None => {
error!("rope.line_to_char failed unexpectedly (2), skipping cursor.");
continue;
}
}
};
let new_line_begin = match rope.line_to_char(new_line_idx) {
Some(char_idx) => char_idx,
None => {
error!("rope.line_to_char failed unexpectedly (3), skipping cursor.");
continue;
}
};
let _new_line_is_last = new_line_idx + 1 == rope.len_lines();
let new_line_num_chars = last_char_in_new_line_idx - new_line_begin;
let preferred_target_column = match c.preferred_column {
Some(pc) => pc,
None => current_col_idx,
};
let old_pos = c.a;
if new_line_num_chars >= preferred_target_column {
c.clear_pc();
c.a = new_line_begin + preferred_target_column;
} else {
c.a = new_line_begin + new_line_num_chars;
if c.preferred_column.is_none() {
c.preferred_column = Some(current_col_idx);
debug_assert!(current_col_idx > 0);
}
}
if selecting {
c.update_select(old_pos, c.a);
}
if old_pos != c.a {
res = true;
}
debug_assert!(
c.a <= rope.len_chars(),
"somehow put the cursor {:?} too far out (len_chars = {})",
c,
rope.len_chars()
);
}
if l < 0 {
self.reduce_left();
} else {
self.reduce_right();
}
res
}
pub fn get_cursor_status_for_char(&self, char_idx: usize) -> CursorStatus {
let mut current_status = CursorStatus::None;
for i in self.set.iter() {
let new_status = i.get_cursor_status_for_char(char_idx);
if new_status == CursorStatus::WithinSelection && current_status == CursorStatus::None {
current_status = new_status;
}
if new_status == CursorStatus::UnderCursor {
current_status = new_status;
break;
}
}
current_status
}
pub fn iter(&self) -> Iter<'_, Cursor> {
self.set.iter()
}
pub fn iter_mut(&mut self) -> IterMut<'_, Cursor> {
self.set.iter_mut()
}
// Returns FALSE if results in no-op
pub fn move_home(&mut self, rope: &dyn TextBuffer, selecting: bool) -> bool {
if self.max_cursor_pos() > rope.len_chars() {
error!("buffer shorter than cursor positions. Returning prematurely to avoid crash.");
return false;
}
let mut res = false;
for c in self.set.iter_mut() {
res |= c.move_home(rope, selecting);
}
self.reduce_left();
res
}
// Returns FALSE if results in noop.
pub fn move_end(&mut self, rope: &dyn TextBuffer, selecting: bool) -> bool {
if self.max_cursor_pos() > rope.len_chars() {
error!("buffer shorter than cursor positions. Returning prematurely to avoid crash.");
return false;
}
let mut res = false;
for c in self.set.iter_mut() {
res |= c.move_end(rope, selecting);
}
// reducing - we just pick ones that are furthest left
self.reduce_right();
res
}
// this is a helper function, that moves cursors' .a to begin or end of selection.
// returns whether operation had any impact on set or not.
fn normalize_anchor(&mut self, right: bool) -> bool {
let mut changed = false;
for cursor in self.set.iter_mut() {
if let Some(s) = cursor.s {
debug_assert!(cursor.a == s.b || cursor.a == s.e);
if right {
if cursor.a != s.e {
changed = true;
}
cursor.a = s.e;
} else {
if cursor.a != s.b {
changed = true;
}
cursor.a = s.b;
}
}
}
changed
}
// Reduces cursors after a move left.
// Moves anchors left.
// When two anchors collide, keeps the one with longer selection.
// When anchors are different, but selections overlap, I SHORTEN THE EARLIER SELECTION, because
// I assume there have been a move LEFT with selection on.
// Returns true if set was modified
pub fn reduce_left(&mut self) -> bool {
if self.set.len() == 1 {
return false;
}
let mut res = false;
res = self.normalize_anchor(false);
if res {
warn!("normalizing anchor left had an effect, this is not expected.");
}
let mut new_set = HashMap::<usize, Cursor>::new();
self.set.sort_by_key(|c| c.a);
for c in self.set.iter() {
match new_set.get(&c.a) {
None => {
new_set.insert(c.a, *c);
}
Some(old_c) => {
// we replace only if old one has shorter selection than new one.
match (old_c.s, c.s) {
(Some(old_sel), Some(new_sel)) => {
if old_sel.e < new_sel.e {
new_set.insert(c.a, *c);
res = true;
}
}
// if previous one had no selection, we consider new selection longer.
(None, Some(_new_sel)) => {
new_set.insert(c.a, *c);
res = true;
}
_ => {}
}
}
}
}
if new_set.len() < self.set.len() {
self.set.clear();
for (_a, c) in new_set.iter() {
self.set.push(*c);
}
self.set.sort_by_key(|c| c.a);
}
// now possibly shortening the selections.
if self.set.len() > 1 {
for i in 0..self.set.len() - 1 {
let next = self.set[i + 1];
let curr = &mut self.set[i];
match &mut curr.s {
Some(curr_s) => {
// it's a little easier because I know from above sorts, that curr.a < next.a
if curr_s.e > next.a {
curr_s.e = next.a;
res = true;
}
}
None => {}
}
}
}
debug_assert!(!self.set.is_empty());
res
}
// Reduces cursors after a move right.
// Moves anchors right.
// When two anchors collide, keeps the one with longer selection.
// When anchors are different, but selections overlap, I SHORTEN THE LATER SELECTION, because
// I assume there have been a move RIGHT with selection on.
// Return true if set was modified
pub fn reduce_right(&mut self) -> bool {
let mut res = false;
if self.set.len() == 1 {
return res;
}
let norm_res = self.normalize_anchor(true);
if norm_res {
warn!("normalizing anchor right had an effect, this is not expected.");
}
let mut new_set = HashMap::<usize, Cursor>::new();
self.set.sort_by_key(|c| c.a);
for c in self.set.iter().rev() {
match new_set.get(&c.a) {
None => {
new_set.insert(c.a, *c);
}
Some(old_c) => {
// we replace only if old one has shorter selection than new one.
match (old_c.s, c.s) {
(Some(old_sel), Some(new_sel)) => {
if old_sel.b > new_sel.b {
new_set.insert(c.a, *c);
res = true;
}
}
// if previous one had no selection, we consider new selection longer.
(None, Some(_new_sel)) => {
new_set.insert(c.a, *c);
res = true;
}
_ => {}
}
}
}
}
if new_set.len() < self.set.len() {
self.set.clear();
for (_a, c) in new_set.iter() {
self.set.push(*c);
}
self.set.sort_by_key(|c| c.a);
}
// now possibly shortening the selections.
for i in (1..self.set.len()).rev() {
let prev = self.set[i - 1];
let curr = &mut self.set[i];
if let (Some(curr_s), Some(prev_s)) = (&mut curr.s, prev.s) {
// it's a little easier because I know from above sorts, that curr.a < next.a
if curr_s.b < prev_s.e {
curr_s.b = prev_s.e;
debug_assert!(curr.a == curr_s.e);
res = true;
}
}
}
debug_assert!(!self.set.is_empty());
res
}
pub fn word_begin(&mut self, buffer: &dyn TextBuffer, selecting: bool, word_determinant: &BackwardWordDeterminant) -> bool {
let mut res = false;
for c in self.set.iter_mut() {
res |= c.word_begin(buffer, selecting, word_determinant);
}
self.reduce_left();
res
}
pub fn word_end(&mut self, buffer: &dyn TextBuffer, selecting: bool, word_determinant: &ForwardWordDeterminant) -> bool {
let mut res = false;
for c in self.set.iter_mut() {
res |= c.word_end(buffer, selecting, word_determinant);
}
self.reduce_right();
res
}
pub fn word_begin_default(&mut self, buffer: &dyn TextBuffer, selecting: bool) -> bool {
self.word_begin(buffer, selecting, &default_word_determinant)
}
pub fn word_end_default(&mut self, buffer: &dyn TextBuffer, selecting: bool) -> bool {
self.word_end(buffer, selecting, &default_word_determinant)
}
/*
Drops selection and preferred column, used while jumping to "dropping cursor mode"
*/
pub fn simplify(&mut self) -> bool {
let mut res = false;
for c in &mut self.set {
res |= c.simplify();
}
res
}
/*
Considers only selection, ignores preferred column.
*/
pub fn are_simple(&self) -> bool {
for c in &self.set {
if !c.is_simple() {
return false;
}
}
true
}
/*
Adds cursor, returns true if such cursor did not exist.
*/
pub fn add_cursor(&mut self, cursor: Cursor) -> bool {
debug_assert!(self.are_simple());
if self.get_cursor_status_for_char(cursor.a) == CursorStatus::None {
self.set.push(cursor);
self.set.sort();
debug_assert!(self.check_invariant());
true
} else {
false
}
}
pub fn len(&self) -> usize {
self.set.len()
}
pub fn remove_by_anchor(&mut self, anchor_char: usize) -> bool {
for i in 0..self.set.len() {
if self.set[i].a == anchor_char {
self.set.remove(i);
// disabled, we now allow "temporarily" empty sets in processing shift_tab
//debug_assert!(self.check_invariant());
return true;
}
}
false
}
pub fn supercursor(&self) -> &Cursor {
//TODO this should vary, depending on which direction cursors were moved last. Now it just points
// to first one.
// this succeeds, because of invariants
self.set.first().unwrap_or_else(|| {
error!("invariant broken, empty cursor_set.");
&ZERO_CURSOR
})
}
pub fn is_single(&self) -> bool {
self.set.len() == 1
}
pub fn first(&self) -> Cursor {
// TODO unwrap
*self.set.first().unwrap()
}
}
impl HasInvariant for CursorSet {
fn check_invariant(&self) -> bool {
// at least one
if self.set.is_empty() {
error!("cursor_set empty");
return false;
}
for c in &self.set {
if !c.check_invariant() {
return false;
}
}
// sorted, and anchors on the same side
// TODO change to is_sorted once stabilized
for idx in 1..self.set.len() {
if self.set[idx - 1].cmp(&self.set[idx]) != Ordering::Less {
error!(
"cursor[{}] = {:?} >= {:?} = cursor[{}]",
idx - 1,
self.set[idx - 1],
self.set[idx],
idx
);
return false;
}
}
let mut anchor_left = false;
let mut anchor_right = false;
for c in &self.set {
anchor_left |= c.anchor_left();
anchor_right |= c.anchor_right();
}
if anchor_left && anchor_right {
error!("invariant \"anchors on the same side\" failed.");
return false;
}
// at this point I know they are sorted and anchor-aligned. All I need to do is to check if begin is
// after previous end.
for idx in 1..self.set.len() {
if self.set[idx - 1].get_end() > self.set[idx].get_begin() {
error!(
"cursor[{}].get_end() = {} > {} = cursor[{}].get_begin()",
idx - 1,
self.set[idx - 1].get_end(),
self.set[idx].get_begin(),
idx
);
return false;
}
}
true
}
}