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
use frappe::Signal;
use buffer::Buffer;
use buffer;
use Samples;

/// Imagine a cursor as the primary actor through which to edit a buffer.
/// A cursor can have multiple positions, aka multi-cursor.
pub struct Cursor {
    char_idx_signals: Vec<Signal<usize>>
}

impl Cursor {
    pub fn new<'a, I>(buf: &Buffer, char_idc: I) -> Self
    where I: IntoIterator<Item=&'a usize> {
        let mut cur = Self {
            char_idx_signals: Vec::new(),
        };
        cur.set_char_idc(buf, char_idc);
        cur
    }

    pub fn char_idc(&self) -> Samples<usize> {
        Samples {
            items: self.char_idx_signals.as_slice(),
            idx: 0
        }
    }

    pub fn handle(&mut self, buf: &mut Buffer, event: Event) {
        match event {
            Event::Insert(text) => {
                for char_idx_signal in &self.char_idx_signals {
                    buf.handle(buffer::Event::Insert {
                        char_idx: char_idx_signal.sample(),
                        text: text.clone() // XXX
                    }, true);
                }
            },
            Event::Backspace => {
                for char_idx_signal in &self.char_idx_signals {
                    let char_idx = char_idx_signal.sample();
                    if let (char_idx, false) = char_idx.overflowing_sub(1) {
                        buf.handle(buffer::Event::Remove {
                            char_idx_range: char_idx .. char_idx + 1
                        }, true);
                    }
                }
            },
            Event::Delete => {
                for char_idx_signal in &self.char_idx_signals {
                    let char_idx = char_idx_signal.sample();
                    if char_idx < buf.text().len_chars() {
                        buf.handle(buffer::Event::Remove {
                            char_idx_range: char_idx .. char_idx + 1
                        }, true);
                    }
                }
            },
            Event::Jump(mut char_idc) => {
                char_idc.sort();
                char_idc.dedup();

                self.set_char_idc(buf, char_idc.iter());
            }
        }
    }

    fn set_char_idc<'a, I>(&mut self, buf: &Buffer, char_idc: I)
    where I: IntoIterator<Item=&'a usize> {
        let char_idx_signals = char_idc.into_iter()
            .map(|char_idx| buf.signal_char_idx(*char_idx));

        self.char_idx_signals.clear();
        self.char_idx_signals.extend(char_idx_signals);
    }
}

pub enum Event {
    Insert(String),
    Backspace,
    Delete,
    /// Positions will be deduped for you.
    Jump(Vec<usize>)
}

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

    const TEXT: &str = concat!(
        "This is line number 1\n",
        "Soon follows line number 2\n",
        "Which, in turn, is followed by line number 3\n",
        "In close call to line number 4\n",
        "Within proximity of line number 5\n",
        "Right beside line number 6\n"
    );

    #[test]
    fn new() {
        let mut buf = Buffer::from_str(TEXT);
        let char_idc: Vec<usize> = TEXT.lines().enumerate()
            .map(|(line_idx, _)| buf.text().line_to_char(line_idx))
            .collect();
        let cur = Cursor::new(&mut buf, char_idc.iter());

        assert_eq!(char_idc, cur.char_idc().collect::<Vec<_>>());
    }

    #[test]
    fn insert() {
        let mut buf = Buffer::from_str(TEXT);
        let mut cur = Cursor::new(&mut buf, &[0]);

        const CHARS: [char; 17] = [
            'L', 'i', 'n', 'e', 's', ' ',
            'b', 'e', 'g', 'i', 'n', ' ',
            'a', 't', ' ', '0', '\n'
        ];
        for char in &CHARS {
            cur.handle(&mut buf, Event::Insert(char.to_string()));
        }
        assert_eq!(CHARS.iter().collect::<String>(), buf.text().line(0).to_string());

        const LINE_IDC: [usize; 2] = [0, 1];
        let char_idc = LINE_IDC.iter()
            .map(|line_idx| buf.text().line_to_char(*line_idx))
            .collect::<Vec<_>>();
        cur.set_char_idc(&mut buf, &char_idc);
        const PREFIX: &str = "->";
        cur.handle(&mut buf, Event::Insert(PREFIX.to_owned()));
        for line_idx in &LINE_IDC {
            assert!(buf.text().line(*line_idx).to_string().starts_with(PREFIX));
        }
    }

    #[test]
    fn backspace() {
        let mut buf = Buffer::from_str(TEXT);
        let mut cur = Cursor::new(&mut buf, &[0]);

        cur.handle(&mut buf, Event::Backspace);
        assert_eq!("This is line number 1\n", buf.text().line(0).to_string());

        let char_idc = [buf.text().len_chars()];
        cur.set_char_idc(&mut buf, &char_idc);
        cur.handle(&mut buf, Event::Backspace);
        let line_idx = buf.text().len_lines() - 1;
        assert_eq!("Right beside line number 6", buf.text().line(line_idx).to_string());

        for _ in 0..9 {
            cur.handle(&mut buf, Event::Backspace);
        }
        assert_eq!("Right beside line", buf.text().line(line_idx).to_string());

        const LINE_IDC: [usize; 2] = [2, 3];
        let char_idc = LINE_IDC.iter()
            // set cursor to end of line
            .map(|line_idx| buf.text().line_to_char(*line_idx + 1) - 1)
            .collect::<Vec<_>>();
        cur.set_char_idc(&mut buf, &char_idc);
        cur.handle(&mut buf, Event::Backspace);
        cur.handle(&mut buf, Event::Backspace);
        for line_idx in &LINE_IDC {
            assert!(buf.text().line(*line_idx).to_string().ends_with("line number\n"));
        }
    }
}