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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
use {
    super::{Pos, Range},
    crate::TAB_REPLACEMENT,
    std::{
        fmt,
    },
    unicode_width::UnicodeWidthChar,
};

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct Line {
    pub chars: Vec<char>,
}
/// an iterator over the chars of an InputFieldContent or
/// of a selection
pub struct Chars<'c> {
    content: &'c InputFieldContent,
    pos: Pos,
    end: Pos,
}
/// the content of an InputField.
///
/// Doesn't know about rendering, styles, areas, etc.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputFieldContent {
    /// the cursor's position
    pos: Pos,
    /// the other end of the selection, if any
    selection_tail: Option<Pos>,
    /// text lines, always at least one
    lines: Vec<Line>,
}

impl Iterator for Chars<'_> {
    type Item = char;
    fn next(&mut self) -> Option<char> {
        if self.pos > self.end {
            return None;
        }
        let line = &self.content.lines[self.pos.y];
        if self.pos.x < line.chars.len() {
            self.pos.x += 1;
            Some(line.chars[self.pos.x - 1])
        } else if self.pos.y + 1 < self.content.lines.len() {
            self.pos.y += 1;
            self.pos.x = 0;
            Some('\n')
        } else {
            None
        }
    }
}
impl<'c> IntoIterator for &'c InputFieldContent {
    type Item = char;
    type IntoIter = Chars<'c>;
    fn into_iter(self) -> Self::IntoIter {
        Chars {
            content: self,
            pos: Pos::default(),
            end: self.end(),
        }
    }
}

impl fmt::Display for Line {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use fmt::Write;
        for &c in &self.chars {
            f.write_char(c)?;
        }
        Ok(())
    }
}

impl Default for InputFieldContent {
    fn default() -> Self {
        Self {
            pos: Pos::default(),
            selection_tail: None,
            // there's always a line
            lines: vec![Line::default()],
        }
    }
}

impl fmt::Display for InputFieldContent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use fmt::Write;
        let mut lines = self.lines.iter().peekable();
        while let Some(line) = lines.next() {
            for &c in &line.chars {
                f.write_char(c)?;
            }
            if lines.peek().is_some() {
                f.write_char('\n')?;
            }
        }
        Ok(())
    }
}

impl Line {
    pub fn col_to_char_idx(&self, col: usize) -> Option<usize> {
        let mut sum_widths = 0;
        for (idx, &c) in self.chars.iter().enumerate() {
            if col <= sum_widths {
                return Some(idx);
            }
            sum_widths += InputFieldContent::char_width(c);
        }
        None
    }
    pub fn char_idx_to_col(&self, idx: usize) -> usize {
        self.chars[0..idx].iter()
            .map(|&c| InputFieldContent::char_width(c))
            .sum()
    }
    pub fn width(&self) -> usize {
        self.chars.iter().map(|&c| InputFieldContent::char_width(c)).sum()
    }
}

impl InputFieldContent {
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }
    pub fn line(&self, y: usize) -> Option<&Line> {
        self.lines.get(y)
    }
    pub fn line_saturating(&self, y: usize) -> &Line {
        self.lines.get(y)
            .unwrap_or(&self.lines[self.lines.len()-1])
    }
    pub fn current_line(&self) -> &Line {
        self.lines
            .get(self.pos.y)
            .expect("current line should exist")
    }
    pub fn lines(&self) -> &[Line] {
        &self.lines
    }
    pub const fn cursor_pos(&self) -> Pos {
        self.pos
    }
    /// Set the cursor position.
    ///
    /// The position set may be different to ensure consistency
    /// (for example if it's after the end, it will be set back).
    pub fn set_cursor_pos(&mut self, new_pos: Pos) {
        self.pos = self.make_valid_pos(new_pos);
    }
    /// Return the given pos, maybe modified to be valid for the content
    pub fn make_valid_pos(&self, mut pos: Pos) -> Pos {
        if pos.y >= self.lines.len() {
            self.end()
        } else {
            pos.x = pos.x.min(self.lines[pos.y].chars.len());
            pos
        }
    }
    /// Set the selection tail to the current pos if there's no selection
    pub fn make_selection(&mut self) {
        if self.selection_tail.is_none() {
            self.selection_tail = Some(self.pos);
        }
    }
    pub fn unselect(&mut self) {
        self.selection_tail = None;
    }
    pub fn set_selection_tail(&mut self, sel_tail: Pos) {
        if sel_tail.y >= self.lines.len() {
            self.selection_tail = Some(self.end());
        } else {
            self.selection_tail = Some(Pos {
                y: sel_tail.y,
                x: sel_tail.x.min(self.lines[self.pos.y].chars.len()),
            });
        }
    }
    fn fix_pos(&mut self) {
        self.pos = self.make_valid_pos(self.pos);
    }
    fn fix_selection_tail(&mut self) {
        if let Some(sel_tail) = self.selection_tail {
            self.selection_tail = Some(self.make_valid_pos(sel_tail));
        }
    }
    fn fix_selection(&mut self) {
        self.fix_pos();
        self.fix_selection_tail();
    }
    pub fn selection(&self) -> Range {
        if let Some(sel_tail) = self.selection_tail {
            if sel_tail < self.pos {
                Range { min: sel_tail, max: self.pos }
            } else {
                Range { min: self.pos, max: sel_tail }
            }
        } else {
            Range { min: self.pos, max: self.pos }
        }
    }
    /// return an iterator over the characters of the
    /// selection (including some newline chars maybe)
    pub fn selection_chars(&self) -> Chars<'_> {
        let Range { min, max } = self.selection();
        Chars {
            content: self,
            pos: min,
            end: max,
        }
    }
    pub fn selection_string(&self) -> String {
        self.selection_chars().collect()
    }
    pub fn is_empty(&self) -> bool {
        match self.lines.len() {
            1 => self.lines[0].chars.is_empty(),
            _ => false,
        }
    }
    pub const fn has_selection(&self) -> bool {
        self.selection_tail.is_some()
    }
    pub fn has_wide_selection(&self) -> bool {
        self.selection_tail.map_or(false, |sel_tail| sel_tail != self.pos)
    }
    /// return the position on end, where the cursor should be put
    /// initially
    pub fn end(&self) -> Pos {
        let y = self.lines.len() - 1;
        Pos { x:self.lines[y].chars.len(), y }
    }
    fn last_line(&mut self) -> &mut Line {
        let y = self.lines.len() - 1;
        &mut self.lines[y]
    }
    /// add a char at end, without updating the position.
    ///
    /// This shouldn't be used in normal event handling as
    /// characters are normally inserted on insertion point
    /// with insert_char.
    pub fn push_char(&mut self, c: char) {
        match c {
            '\n' => self.lines.push(Line::default()),
            '\r' | '\x08' /*backspacea*/ => {}
            _ => self.last_line().chars.push(c),
        }
    }
    /// Initialize from a string, with the cursor at end
    pub fn from<S: AsRef<str>>(s: S) -> Self {
        let mut content = Self::default();
        content.insert_str(s);
        content
    }
    pub fn clear(&mut self) {
        self.lines.clear();
        self.lines.push(Line::default());
        self.pos = Pos::default();
        self.selection_tail = None;
    }
    pub fn insert_new_line(&mut self) {
        let new_line = Line {
            chars: self.lines[self.pos.y].chars.split_off(self.pos.x),
        };
        self.pos.x = 0;
        self.pos.y += 1;
        self.lines.insert(self.pos.y, new_line);
        self.fix_selection();
    }
    /// Insert a character at the current position, updating
    /// this position
    pub fn insert_char(&mut self, c: char) {
        if c == '\n' {
            self.insert_new_line();
        } else if c == '\r' || c == '\x08' {
            // skipping
        } else {
            self.lines[self.pos.y].chars.insert(self.pos.x, c);
            self.pos.x += 1;
        }
    }
    /// Insert the string on cursor point, as if it was typed
    pub fn insert_str<S: AsRef<str>>(&mut self, s: S) {
        for c in s.as_ref().chars() {
            self.insert_char(c);
        }
    }
    /// Tell whether the content of the input is equal to the argument,
    /// comparing char by char
    pub fn is_str(&self, s: &str) -> bool {
        let mut ia = self.into_iter();
        let mut ib = s.chars();
        loop {
            match (ia.next(), ib.next()) {
                (Some(a), Some(b)) if a == b => { continue }
                (None, None) => { return true; }
                _ => { return false; }
            }
        }
    }
    /// Change the content to the new one and put the cursor at the end **if** the
    ///  content is different from the previous one.
    ///
    ///  Don't move the cursor if the string content didn't change.
    pub fn set_str<S: AsRef<str>>(&mut self, s: S) {
        if self.is_str(s.as_ref()) {
            return;
        }
        self.clear();
        self.insert_str(s);
    }
    /// Remove the char left of the cursor, if any.
    pub fn del_char_left(&mut self) -> bool {
        if self.pos.x > 0 {
            self.pos.x -= 1;
            if !self.lines[self.pos.y].chars.is_empty() {
                self.lines[self.pos.y].chars.remove(self.pos.x);
            }
            self.fix_selection();
            true
        } else if self.pos.y > 0 && self.lines.len() > 1 {
            let mut removed_line = self.lines.remove(self.pos.y);
            self.pos.y -= 1;
            self.pos.x = self.lines[self.pos.y].chars.len();
            self.lines[self.pos.y].chars.append(&mut removed_line.chars);
            self.fix_selection();
            true
        } else {
            false
        }
    }

    /// make the word around the current pos, if any, the current selection
    pub fn select_word_around(&mut self) -> bool {
        let chars = &self.lines[self.pos.y].chars;
        let mut start = self.pos.x;
        if start >= chars.len() || !is_word_char(chars[start]) {
            return false;
        }
        while start > 0 && is_word_char(chars[start-1]) {
            start -= 1;
        }
        let mut end = self.pos.x;
        while end + 1 < chars.len() && is_word_char(chars[end+1]) {
            end += 1;
        }
        self.selection_tail = Some(Pos::new(start, self.pos.y));
        self.pos.x = end;
        true
    }

    /// Remove the char at cursor position, if any.
    ///
    /// Cursor position is unchanged
    pub fn del_char_below(&mut self) -> bool {
        let line_len = self.current_line().chars.len();
        if line_len == 0 {
            if self.lines.len() > 1 {
                self.lines.remove(self.pos.y);
                self.fix_selection();
                true
            } else {
                false
            }
        } else if self.pos.x < line_len {
            self.lines[self.pos.y].chars.remove(self.pos.x);
            self.fix_selection();
            true
        } else if self.lines.len() > self.pos.y + 1 {
            let mut removed_line = self.lines.remove(self.pos.y + 1);
            self.lines[self.pos.y].chars.append(&mut removed_line.chars);
            self.fix_selection();
            true
        } else {
            false
        }
    }

    pub fn del_selection(&mut self) -> bool {
        let Range { min, max } = self.selection();
        if min.y == max.y {
            if min.x == max.x {
                return self.del_char_below();
            }
            if max.x == self.lines[min.y].chars.len() {
                if min.x == 0 {
                    // we remove the whole line
                    self.lines.drain(min.y..min.y+1);
                    if self.lines.is_empty() {
                        self.lines.push(Line::default());
                    }
                } else {
                    self.lines[min.y].chars.drain(min.x..);
                }
            } else {
                self.lines[min.y].chars.drain(min.x..max.x+1);
            }
        } else {
            let min_y = if min.x > 0 {
                self.lines[min.y].chars.truncate(min.x);
                min.y + 1
            } else {
                min.y
            };
            let max_y = if max.x < self.lines[max.y].chars.len() {
                self.lines[max.y].chars.drain(0..max.x);
                max.y - 1
            } else {
                max.y
            };
            if max_y > min_y {
                self.lines.drain(min_y..(max_y+1).min(self.lines.len()));
                if self.lines.is_empty() {
                    self.lines.push(Line::default());
                }
            }
        }
        self.set_cursor_pos(min);
        self.selection_tail = None;
        true
    }
    /// Swap two lines. Return false if one of the indices is out of
    /// range or if the two indices are the same
    pub fn swap_lines(&mut self, ya: usize, yb: usize) -> bool {
        if ya != yb && ya < self.lines.len() && yb < self.lines.len() {
            self.lines.swap(ya, yb);
            self.fix_selection();
            true
        } else {
            false
        }
    }

    /// Swap the current line with the line before, if possible
    pub fn move_current_line_up(&mut self) -> bool {
        if self.pos.y > 0 && self.swap_lines(self.pos.y - 1, self.pos.y) {
            self.pos.y -= 1;
            self.fix_selection();
            true
        } else {
            false
        }
    }

    /// Swap the current line with the line after, if possible
    pub fn move_current_line_down(&mut self) -> bool {
        if self.swap_lines(self.pos.y + 1, self.pos.y) {
            self.pos.y += 1;
            self.fix_selection();
            true
        } else {
            false
        }
    }

    /// Move the cursor to the right (or to the line below
    /// if it's a the end of a non-last line)
    pub fn move_right(&mut self) -> bool {
        if self.pos.x < self.lines[self.pos.y].chars.len() {
            self.pos.x += 1;
            true
        } else if self.pos.y < self.lines.len() - 1 {
            self.pos.y += 1;
            self.pos.x = 0;
            true
        } else {
            false
        }
    }
    /// Move the cursor up
    pub fn move_lines_up(&mut self, lines: usize) -> bool {
        if self.pos.y > 0 {
            let cols = self.lines[self.pos.y].char_idx_to_col(self.pos.x);
            self.pos.y -= lines.min(self.pos.y);
            let line = &self.lines[self.pos.y];
            self.pos.x = line.col_to_char_idx(cols).unwrap_or(line.chars.len());
            true
        } else {
            false
        }
    }
    /// Move the cursor one line up
    pub fn move_up(&mut self) -> bool {
        self.move_lines_up(1)
    }
    /// Move the cursor down
    pub fn move_lines_down(&mut self, lines: usize) -> bool {
        if self.pos.y + 1 < self.lines.len() {
            let cols = self.lines[self.pos.y].char_idx_to_col(self.pos.x);
            self.pos.y += lines.min(self.lines.len() - self.pos.y - 1);
            let line = &self.lines[self.pos.y];
            self.pos.x = line.col_to_char_idx(cols).unwrap_or(line.chars.len());
            true
        } else {
            false
        }
    }
    pub fn move_down(&mut self) -> bool {
        self.move_lines_down(1)
    }
    pub fn move_left(&mut self) -> bool {
        if self.pos.x > 0 {
            self.pos.x -= 1;
            true
        } else if self.pos.y > 0 {
            self.pos.y -= 1;
            self.pos.x = self.lines[self.pos.y].chars.len();
            true
        } else {
            false
        }
    }
    pub fn move_to_end(&mut self) -> bool {
        let pos = self.end();
        if pos == self.pos {
            false
        } else {
            self.pos = pos;
            true
        }
    }
    pub fn move_to_start(&mut self) -> bool {
        let pos = Pos { x: 0, y: 0 };
        if pos == self.pos {
            false
        } else {
            self.pos = pos;
            true
        }
    }
    pub fn move_to_line_end(&mut self) -> bool {
        let line_len = self.lines[self.pos.y].chars.len();
        if self.pos.x < line_len {
            self.pos.x = line_len;
            true
        } else {
            false
        }
    }
    pub fn move_to_line_start(&mut self) -> bool {
        if self.pos.x > 0 {
            self.pos.x = 0;
            true
        } else {
            false
        }
    }
    pub fn move_word_left(&mut self) -> bool {
        if self.pos.x > 0 {
            let chars = &self.lines[self.pos.y].chars;
            loop {
                self.pos.x -= 1;
                if self.pos.x == 0 || !chars[self.pos.x-1].is_alphanumeric() {
                    break;
                }
            }
            true
        } else {
            false
        }
    }
    pub fn move_word_right(&mut self) -> bool {
        if self.pos.x < self.lines[self.pos.y].chars.len() {
            let chars = &self.lines[self.pos.y].chars;
            loop {
                self.pos.x += 1;
                if self.pos.x +1 >= chars.len() || !chars[self.pos.x+1].is_alphanumeric() {
                    break;
                }
            }
            true
        } else {
            false
        }
    }
    pub fn del_word_left(&mut self) -> bool {
        if self.pos.x > 0 {
            let chars = &mut self.lines[self.pos.y].chars;
            loop {
                self.pos.x -= 1;
                chars.remove(self.pos.x);
                if self.pos.x == 0 || !chars[self.pos.x-1].is_alphanumeric() {
                    break;
                }
            }
            self.fix_selection();
            true
        } else {
            false
        }
    }
    /// Delete the word rigth of the cursor.
    ///
    // I'm not yet sure of what should be the right behavior but all changes
    // should be discussed from cases defined as in the unit tests below
    pub fn del_word_right(&mut self) -> bool {
        let chars = &mut self.lines[self.pos.y].chars;
        if self.pos.x < chars.len() {
            loop {
                let deleted_is_an = chars[self.pos.x].is_alphanumeric();
                chars.remove(self.pos.x);
                if !deleted_is_an {
                    break;
                }
                if self.pos.x == chars.len() {
                    if self.pos.x > 0 {
                        self.pos.x -= 1;
                    }
                    break;
                }
            }
            self.fix_selection();
            true
        } else if self.pos.x == self.current_line().chars.len() && self.pos.x > 0 {
            self.pos.x -= 1;
            true
        } else {
            false
        }
    }

    /// Return the number of columns taken by a char. It's
    /// assumed the char isn't '\r', `\n', or backspace
    /// (none of those can be in the inputfield lines)
    pub fn char_width(c: char) -> usize {
        match c {
            '\t' => TAB_REPLACEMENT.len(),
            _ => UnicodeWidthChar::width(c).unwrap_or(0),
        }
    }

}

#[test]
fn test_char_iterator() {
    let texts = vec![
        "this has\nthree lines\n",
        "",
        "123",
        "\n\n",
    ];
    for text in texts {
        assert!(InputFieldContent::from(text).is_str(text));
    }
}

#[cfg(test)]
mod input_content_edit_monoline_tests {

    use super::*;

    /// make an input for tests from two strings:
    /// - the content string (no wide chars)
    /// - a cursor position specified as a string with a caret
    fn make_content(value: &str, cursor_pos: &str) -> InputFieldContent {
        let mut content = InputFieldContent::from(value);
        content.pos = Pos {
            x: cursor_pos.chars().position(|c| c=='^').unwrap(),
            y: 0,
        };
        content
    }

    fn check(a: &InputFieldContent, value: &str, cursor_pos: &str) {
        let b = make_content(value, cursor_pos);
        assert_eq!(a, &b);
    }

    /// test the behavior of new line insertion
    #[test]
    fn test_new_line() {
        let mut con = make_content(
            "12345",
            "  ^  "
        );
        con.insert_char('6');
        check(
            &con,
            "126345",
            "   ^  ",
        );
        con.insert_new_line();
        assert!(con.is_str("126\n345"));
        let mut con = InputFieldContent::default();
        con.insert_char('1');
        con.insert_char('2');
        con.insert_new_line();
        con.insert_char('3');
        con.insert_char('4');
        assert!(con.is_str("12\n34"));
    }

    /// test the behavior of del_word_right
    #[test]
    fn test_del_word_right() {
        let mut con = make_content(
            "aaa bbb ccc",
            "     ^     ",
        );
        con.del_word_right();
        check(
            &con,
            "aaa bccc",
            "     ^  ",
        );
        con.del_word_right();
        check(
            &con,
            "aaa b",
            "    ^",
        );
        con.del_word_right();
        check(
            &con,
            "aaa ",
            "   ^",
        );
        con.del_word_right();
        check(
            &con,
            "aaa",
            "   ^",
        );
        con.del_word_right();
        check(
            &con,
            "aaa",
            "  ^",
        );
        con.del_word_right();
        check(
            &con,
            "aa",
            " ^",
        );
        con.del_word_right();
        check(
            &con,
            "a",
            "^",
        );
        con.del_word_right();
        check(
            &con,
            "",
            "^",
        );
        con.del_word_right();
        check(
            &con,
            "",
            "^",
        );
    }
    /// test wide_select->clear->del_selection
    #[test]
    fn test_select_clear_del_selection() {
        let mut con = make_content(
            "aaa bbb ccc",
            "     ^     ",
        );
        con.set_selection_tail(con.end());
        con.clear();
        con.del_selection();
    }
    /// test wide_select->del_char_left->del_selection
    #[test]
    fn test_select_del_char_left_del_selection() {
        let mut con = make_content(
            "aaa bbb ccc",
            "     ^     ",
        );
        con.set_selection_tail(con.end());
        con.del_char_left();
        con.del_selection();
    }
}


fn is_word_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_'
}