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
pub mod jump;

use frappe::Signal;
use ropey::Rope;
use Ropey;
use {Buffer, buffer};

/// 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 {
    positions: Vec<PositionSignal>
    // TODO Insert/overwrite mode?
}

type PositionSignal = (Signal<usize>, Option<Signal<usize>>);

/// The char index of the cursor and the selection start char index.
pub type Position = (usize, Option<usize>);

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

    pub fn positions(&self) -> Positions {
        Positions::new(self.positions.as_slice())
    }

    /// Handles a mutation of the cursor, leaving the buffer unchanged.
    pub fn handle_cur(&mut self, buf: &Buffer, event: CursorEvent) {
        match event {
            CursorEvent::Jump(mut positions) => {
                positions.sort();
                positions.dedup();

                self.set_positions(buf, positions.iter());
            }
        }
    }

    /// Handles a mutation of the buffer.
    pub fn handle_buf(&mut self, buf: &mut Buffer, event: BufferEvent) {
        let pos_lines = |text: &Rope, pos: Position| -> ::std::ops::Range<usize> {
            let (char_idx, sel_start) = pos;

            if let Some(sel_start) = sel_start {
                let sel = self::select((sel_start, char_idx));

                text.char_to_line(sel.start)..
                (text.char_to_line(sel.end) + 1)
                    .min(text.len_lines())
            } else {
                let line = text.char_to_line(char_idx)
                    .min(text.len_lines().saturating_sub(1));
                line..line + 1
            }
        };

        match event {
            BufferEvent::Insert(ref text) => {
                for &mut (ref char_idx_signal, ref mut sel_start) in &mut self.positions {
                    if sel_start.is_some() { // delete and clear selection
                        let start_idx = sel_start.as_ref().unwrap().sample();
                        let end_idx = char_idx_signal.sample();
                        *sel_start = None;

                        buf.handle(buffer::Event::Remove {
                            char_idx_range: select((start_idx, end_idx))
                        });
                    }

                    buf.handle(buffer::Event::Insert {
                        char_idx: char_idx_signal.sample(),
                        text: text.clone() // XXX
                    });
                }
            }
            BufferEvent::Backspace => {
                for &mut (ref char_idx_signal, ref mut sel_start) in &mut self.positions {
                    let char_idx = char_idx_signal.sample();
                    if sel_start.is_some() {
                        let start_idx = sel_start.as_ref().unwrap().sample();
                        *sel_start = None;

                        buf.handle(buffer::Event::Remove {
                            char_idx_range: select((start_idx, char_idx))
                        });
                    } else if let (char_idx, false) = char_idx.overflowing_sub(1) {
                        buf.handle(buffer::Event::Remove {
                            char_idx_range: char_idx..char_idx + 1
                        });
                    }
                }
            }
            BufferEvent::Delete => {
                for &mut (ref char_idx_signal, ref mut sel_start) in &mut self.positions {
                    let char_idx = char_idx_signal.sample();
                    if sel_start.is_some() {
                        let start_idx = sel_start.as_ref().unwrap().sample();
                        *sel_start = None;

                        buf.handle(buffer::Event::Remove {
                            char_idx_range: select((start_idx, char_idx))
                        });
                    } else if char_idx < buf.text().len_chars() {
                        buf.handle(buffer::Event::Remove {
                            char_idx_range: char_idx..char_idx + 1
                        });
                    }
                }
            }
            BufferEvent::Indent(indent) => indent.sample_with(|indent| {
                if buf.text().len_chars() == 0 {
                    /* An empty Rope causes weird math afterwards
                     * (see docs of `char_to_line()` and `line_to_char()`)
                     * so we just handle this simple case here. */
                    buf.handle(buffer::Event::Insert {
                        char_idx: 0,
                        text: (*indent).clone() // XXX
                    })
                } else {
                    for pos in self.positions() {
                        for line_idx in pos_lines(buf.text(), pos) {
                            let line_start_idx = buf.text().line_to_char(line_idx);
                            buf.handle(buffer::Event::Insert {
                                char_idx: line_start_idx,
                                text: (*indent).clone() // XXX
                            })
                        }
                    }
                }
            }),
            BufferEvent::Dedent(indent) => indent.sample_with(|indent| {
                if buf.text().len_chars() == 0 { return } // nothing to dedent

                for pos in self.positions() {
                    for line_idx in pos_lines(buf.text(), pos) {
                        if {
                            let line = buf.text().line(line_idx);
                            line.starts_with(&indent)
                        } {
                            let line_start_idx = buf.text().line_to_char(line_idx);
                            buf.handle(buffer::Event::Remove {
                                char_idx_range:
                                    line_start_idx..
                                    line_start_idx + indent.chars().count()
                            });
                        }
                    }
                }
            })
        }
    }

    fn set_positions<'a, I>(&mut self, buf: &Buffer, positions: I)
    where I: IntoIterator<Item=&'a Position> {
        let positions = positions.into_iter()
            .map(|&(char_idx, sel_start)| (
                buf.signal_char_idx(char_idx),
                sel_start.map(|sel_start| buf.signal_char_idx(sel_start))
            ));

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

pub fn select(range: (usize, usize)) -> ::std::ops::Range<usize> {
    let (start_idx, end_idx) = range;
    if start_idx < end_idx {
        start_idx..end_idx
    } else {
        end_idx..start_idx
    }
}

/// Unused in this module but provided for completeness.
pub enum Event {
    Cur(CursorEvent),
    Buf(BufferEvent)
}

/// An event that mutates the cursor.
pub enum CursorEvent {
    /// Positions will be deduped for you.
    Jump(Vec<Position>)
}

/// An event that mutates the buffer.
pub enum BufferEvent {
    Insert(String),
    Backspace,
    Delete,
    Indent(Signal<String>), Dedent(Signal<String>)
}

pub struct Positions<'a> {
    items: &'a [PositionSignal],
    idx: usize,
    idx_back: usize
}

impl<'a> Positions<'a> {
    pub fn new(positions: &'a [PositionSignal]) -> Self {
        Positions {
            items: positions,
            idx: 0,
            idx_back: 0
        }
    }
}

impl<'a> Iterator for Positions<'a> {
    type Item = Position;

    fn next(&mut self) -> Option<Self::Item> {
        if self.idx < self.items.len() - self.idx_back {
            let item = &self.items[self.idx];
            self.idx += 1;
            Some((
                item.0.sample(),
                match item.1 {
                    Some(ref signal) => Some(signal.sample()),
                    None             => None
                }
            ))
        } else {
            None
        }
    }
}

impl<'a> DoubleEndedIterator for Positions<'a> {
    fn next_back(&mut self) -> Option<Self::Item> {
        let len = self.items.len();

        if self.idx_back < len - self.idx {
            let item = &self.items[len - 1 - self.idx_back];
            self.idx_back += 1;
            Some((
                item.0.sample(),
                match item.1 {
                    Some(ref signal) => Some(signal.sample()),
                    None             => None
                }
            ))
        } else {
            None
        }
    }
}

impl<'a> ExactSizeIterator for Positions<'a> {
    fn len(&self) -> usize {
        self.items.len()
    }
}

#[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 positions: Vec<Position> = TEXT.lines().enumerate()
            .map(|(line_idx, _)| (buf.text().line_to_char(line_idx), None))
            .collect();
        let cur = Cursor::new(&mut buf, positions.iter());

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

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

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

        const LINE_IDC: [usize; 2] = [0, 1];
        let positions = LINE_IDC.iter()
            .map(|line_idx| (buf.text().line_to_char(*line_idx), None))
            .collect::<Vec<_>>();
        cur.set_positions(&mut buf, &positions);
        const PREFIX: &str = "->";
        cur.handle_buf(&mut buf, BufferEvent::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, None)]);

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

        let positions = [(buf.text().len_chars(), None)];
        cur.set_positions(&mut buf, &positions);
        cur.handle_buf(&mut buf, BufferEvent::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_buf(&mut buf, BufferEvent::Backspace);
        }
        assert_eq!("Right beside line", buf.text().line(line_idx).to_string());

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