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
use crate::buffer::operation::Operation;
use crate::buffer::{Buffer, Position, Range};
use std::clone::Clone;

/// A reversible buffer delete operation.
///
/// Deletes the content at the specified range. Tracks the deleted content and specified
/// range, and reverses the operation by (trivially) inserting the deleted content at
/// the start of the specified range.
///
/// If the buffer is configured with a `change_callback`, it will be called with
/// the starting position of this operation when it is run or reversed.
#[derive(Clone)]
pub struct Delete {
    content: Option<String>,
    range: Range,
}

impl Operation for Delete {
    fn run(&mut self, buffer: &mut Buffer) {
        // Fetch and store the content we're about to delete.
        self.content = buffer.data.borrow().read(&self.range);

        // Delete the data.
        buffer.data.borrow_mut().delete(&self.range);

        // Run the change callback, if present.
        if let Some(ref callback) = buffer.change_callback {
            callback(self.range.start())
        }
    }

    fn reverse(&mut self, buffer: &mut Buffer) {
        if let Some(ref content) = self.content {
            buffer
                .data
                .borrow_mut()
                .insert(content, &self.range.start());

            // Run the change callback, if present.
            if let Some(ref callback) = buffer.change_callback {
                callback(self.range.start())
            }
        }
    }

    fn clone_operation(&self) -> Box<dyn Operation> {
        Box::new(self.clone())
    }
}

impl Delete {
    /// Creates a new empty delete operation.
    pub fn new(range: Range) -> Delete {
        Delete {
            content: None,
            range,
        }
    }
}

impl Buffer {
    /// Deletes a character at the cursor position. If at the end
    /// of the current line, it'll try to delete a newline character
    /// (joining the lines), succeeding if there's a line below.
    ///
    /// # Examples
    ///
    /// ```
    /// use scribe::Buffer;
    ///
    /// let mut buffer = Buffer::new();
    /// buffer.insert("scribe");
    /// buffer.delete();
    /// assert_eq!(buffer.data(), "cribe");
    /// ```
    pub fn delete(&mut self) {
        // We need to specify a range to delete, so start at
        // the current offset and delete the character to the right.
        let mut end = Position {
            line: self.cursor.line,
            offset: self.cursor.offset + 1,
        };

        // If there isn't a character to the right,
        // delete the newline by jumping to the start
        // of the next line. If it doesn't exist, that's okay;
        // these values are bounds-checked by delete() anyway.
        if !self.data.borrow().in_bounds(&end) {
            end.line += 1;
            end.offset = 0;
        }

        // The range we're building is going to be consumed,
        // so create a clone of the cursor's current position.
        let start = self.cursor.position;

        // Now that we've established the range, defer.
        self.delete_range(Range::new(start, end));
    }

    /// Removes a range of characters from the buffer.
    ///
    /// # Examples
    ///
    /// ```
    /// use scribe::Buffer;
    /// use scribe::buffer::{Position, Range};
    ///
    /// // Set up an example buffer.
    /// let mut buffer = Buffer::new();
    /// buffer.insert("scribe library");
    ///
    /// // Set up the range we'd like to delete.
    /// let start = Position{ line: 0, offset: 6 };
    /// let end = Position{ line: 0, offset: 14 };
    /// let range = Range::new(start, end);
    ///
    /// buffer.delete_range(range);
    ///
    /// assert_eq!(buffer.data(), "scribe");
    /// ```
    pub fn delete_range(&mut self, range: Range) {
        // Build and run a delete operation.
        let mut op = Delete::new(range);
        op.run(self);

        // Store the operation in the history
        // object so that it can be undone.
        match self.operation_group {
            Some(ref mut group) => group.add(Box::new(op)),
            None => self.history.add(Box::new(op)),
        };
    }
}

#[cfg(test)]
mod tests {
    use super::Delete;
    use crate::buffer::operation::Operation;
    use crate::buffer::{Buffer, Position, Range};
    use std::cell::RefCell;
    use std::rc::Rc;

    #[test]
    fn run_and_reverse_remove_and_add_content_without_newlines_at_cursor_position() {
        // Set up a buffer with some data.
        let mut buffer = Buffer::new();
        buffer.insert("something else");

        // Set up a range that covers everything after the first word.
        let start = Position { line: 0, offset: 9 };
        let end = Position {
            line: 0,
            offset: 14,
        };
        let delete_range = Range::new(start, end);

        // Create the delete operation and run it.
        let mut delete_operation = Delete::new(delete_range);
        delete_operation.run(&mut buffer);

        assert_eq!(buffer.data(), "something");

        delete_operation.reverse(&mut buffer);

        assert_eq!(buffer.data(), "something else");
    }

    #[test]
    fn run_and_reverse_remove_and_add_content_with_newlines_at_cursor_position() {
        // Set up a buffer with some data.
        let mut buffer = Buffer::new();
        buffer.insert("\n something\n else\n entirely");

        // Set up a range that covers everything after the first word.
        let start = Position {
            line: 1,
            offset: 10,
        };
        let end = Position { line: 3, offset: 9 };
        let delete_range = Range::new(start, end);

        // Create the delete operation and run it.
        //
        // NOTE: The newline character ensures that the operation doesn't use a naive
        //       algorithm based purely on the content length.
        let mut delete_operation = Delete::new(delete_range);
        delete_operation.run(&mut buffer);

        assert_eq!(buffer.data(), "\n something");

        delete_operation.reverse(&mut buffer);

        assert_eq!(buffer.data(), "\n something\n else\n entirely");
    }

    #[test]
    fn run_calls_change_callback_with_position() {
        // Set up a buffer with some data.
        let mut buffer = Buffer::new();
        buffer.insert("something else");

        // Set up a range that covers everything after the first word.
        let start = Position { line: 0, offset: 9 };
        let end = Position {
            line: 0,
            offset: 14,
        };
        let delete_range = Range::new(start, end);

        // Create a position that we'll share with the callback.
        let tracked_position = Rc::new(RefCell::new(Position::new()));
        let callback_position = tracked_position.clone();

        // Set up the callback so that it updates the shared position.
        buffer.change_callback = Some(Box::new(move |change_position| {
            *callback_position.borrow_mut() = change_position
        }));

        // Create the delete operation and run it.
        let mut delete_operation = Delete::new(delete_range);
        delete_operation.run(&mut buffer);

        // Verify that the callback received the correct position.
        assert_eq!(*tracked_position.borrow(), Position { line: 0, offset: 9 });
    }

    #[test]
    fn reverse_calls_change_callback_with_position() {
        // Set up a buffer with some data.
        let mut buffer = Buffer::new();
        buffer.insert("something else");

        // Set up a range that covers everything after the first word.
        let start = Position { line: 0, offset: 9 };
        let end = Position {
            line: 0,
            offset: 14,
        };
        let delete_range = Range::new(start, end);

        // Create the delete operation and run it.
        let mut delete_operation = Delete::new(delete_range);
        delete_operation.run(&mut buffer);

        // Create a position that we'll share with the callback.
        let tracked_position = Rc::new(RefCell::new(Position::new()));
        let callback_position = tracked_position.clone();

        // Set up the callback so that it updates the shared position.
        buffer.change_callback = Some(Box::new(move |change_position| {
            *callback_position.borrow_mut() = change_position
        }));

        // Reverse the operation.
        delete_operation.reverse(&mut buffer);

        // Verify that the callback received the correct position.
        assert_eq!(*tracked_position.borrow(), Position { line: 0, offset: 9 });
    }
}