-
Notifications
You must be signed in to change notification settings - Fork 134
/
editor.rs
296 lines (238 loc) · 7.41 KB
/
editor.rs
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
use crate::core_editor::get_default_clipboard;
use super::{Clipboard, LineBuffer};
pub struct Editor {
line_buffer: LineBuffer,
cut_buffer: Box<dyn Clipboard>,
edits: Vec<LineBuffer>,
index_undo: usize,
}
impl Default for Editor {
fn default() -> Self {
Editor {
line_buffer: LineBuffer::new(),
cut_buffer: Box::new(get_default_clipboard()),
// Note: Using list-zipper we can reduce these to one field
edits: vec![LineBuffer::new()],
index_undo: 2,
}
}
}
impl Editor {
pub fn line_buffer(&mut self) -> &mut LineBuffer {
&mut self.line_buffer
}
pub fn set_line_buffer(&mut self, line_buffer: LineBuffer) {
self.line_buffer = line_buffer;
}
pub fn move_to_start(&mut self) {
self.line_buffer.move_to_start()
}
pub fn move_to_end(&mut self) {
self.line_buffer.move_to_end()
}
pub fn move_left(&mut self) {
self.line_buffer.move_left()
}
pub fn move_right(&mut self) {
self.line_buffer.move_right()
}
pub fn move_word_left(&mut self) {
self.line_buffer.move_word_left();
}
pub fn move_word_right(&mut self) {
self.line_buffer.move_word_right();
}
pub fn move_line_up(&mut self) {
self.line_buffer.move_line_up();
}
pub fn move_line_down(&mut self) {
self.line_buffer.move_line_down();
}
pub fn insert_char(&mut self, c: char) {
self.line_buffer.insert_char(c)
}
pub fn backspace(&mut self) {
self.line_buffer.delete_left_grapheme();
}
pub fn delete(&mut self) {
self.line_buffer.delete_right_grapheme();
}
pub fn backspace_word(&mut self) {
self.line_buffer.delete_word_left();
}
pub fn delete_word(&mut self) {
self.line_buffer.delete_word_right();
}
pub fn clear(&mut self) {
self.line_buffer.clear();
}
pub fn uppercase_word(&mut self) {
self.line_buffer.uppercase_word();
}
pub fn lowercase_word(&mut self) {
self.line_buffer.lowercase_word();
}
pub fn capitalize_char(&mut self) {
self.line_buffer.capitalize_char();
}
pub fn swap_words(&mut self) {
self.line_buffer.swap_words();
}
pub fn swap_graphemes(&mut self) {
self.line_buffer.swap_graphemes();
}
pub fn set_insertion_point(&mut self, pos: usize) {
self.line_buffer.set_insertion_point(pos)
}
pub fn get_buffer(&self) -> &str {
self.line_buffer.get_buffer()
}
pub fn set_buffer(&mut self, buffer: String) {
self.line_buffer.set_buffer(buffer)
}
pub fn clear_to_end(&mut self) {
self.line_buffer.clear_to_end()
}
pub fn clear_to_insertion_point(&mut self) {
self.line_buffer.clear_to_insertion_point()
}
pub fn clear_range<R>(&mut self, range: R)
where
R: std::ops::RangeBounds<usize>,
{
self.line_buffer.clear_range(range)
}
pub fn offset(&self) -> usize {
self.line_buffer.offset()
}
pub fn line(&self) -> usize {
self.line_buffer.line()
}
pub fn num_lines(&self) -> usize {
self.line_buffer.num_lines()
}
pub fn is_empty(&self) -> bool {
self.line_buffer.is_empty()
}
pub fn ends_with(&self, c: char) -> bool {
self.line_buffer.ends_with(c)
}
pub fn is_cursor_at_first_line(&self) -> bool {
self.line_buffer.is_cursor_at_first_line()
}
pub fn is_cursor_at_last_line(&self) -> bool {
self.line_buffer.is_cursor_at_last_line()
}
pub fn reset_undo_stack(&mut self) {
self.edits = vec![LineBuffer::new()];
self.index_undo = 2;
}
fn get_index_undo(&self) -> usize {
if let Some(c) = self.edits.len().checked_sub(self.index_undo) {
c
} else {
0
}
}
pub fn undo(&mut self) {
// NOTE: Try-blocks should help us get rid of this indirection too
self.undo_internal();
}
pub fn redo(&mut self) {
// NOTE: Try-blocks should help us get rid of this indirection too
self.redo_internal();
}
fn redo_internal(&mut self) -> Option<()> {
if self.index_undo > 2 {
self.index_undo = self.index_undo.checked_sub(2)?;
self.undo_internal()
} else {
None
}
}
fn undo_internal(&mut self) -> Option<()> {
self.line_buffer = self.edits.get(self.get_index_undo())?.clone();
if self.index_undo <= self.edits.len() {
self.index_undo = self.index_undo.checked_add(1)?;
}
Some(())
}
pub fn remember_undo_state(&mut self, is_after_action: bool) -> Option<()> {
self.reset_index_undo();
if self.edits.len() > 1
&& self.edits.last()?.word_count() == self.line_buffer.word_count()
&& !is_after_action
{
self.edits.pop();
}
self.edits.push(self.line_buffer.clone());
Some(())
}
fn reset_index_undo(&mut self) {
self.index_undo = 2;
}
pub fn cut_from_start(&mut self) {
let insertion_offset = self.line_buffer.offset();
if insertion_offset > 0 {
self.cut_buffer
.set(&self.line_buffer.get_buffer()[..insertion_offset]);
self.clear_to_insertion_point();
}
}
pub fn cut_from_end(&mut self) {
let cut_slice = &self.line_buffer.get_buffer()[self.line_buffer.offset()..];
if !cut_slice.is_empty() {
self.cut_buffer.set(cut_slice);
self.clear_to_end();
}
}
pub fn cut_word_left(&mut self) {
let insertion_offset = self.line_buffer.offset();
let left_index = self.line_buffer.word_left_index();
if left_index < insertion_offset {
let cut_range = left_index..insertion_offset;
self.cut_buffer
.set(&self.line_buffer.get_buffer()[cut_range.clone()]);
self.clear_range(cut_range);
self.line_buffer.set_insertion_point(left_index);
}
}
pub fn cut_word_right(&mut self) {
let insertion_offset = self.line_buffer.offset();
let right_index = self.line_buffer.word_right_index();
if right_index > insertion_offset {
let cut_range = insertion_offset..right_index;
self.cut_buffer
.set(&self.line_buffer.get_buffer()[cut_range.clone()]);
self.clear_range(cut_range);
}
}
pub fn insert_cut_buffer(&mut self) {
let cut_buffer = self.cut_buffer.get();
self.line_buffer.insert_str(&cut_buffer);
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_undo_initial_char() {
let mut editor = Editor::default();
editor.line_buffer().set_buffer(String::from("a"));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab"));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab "));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab c"));
editor.remember_undo_state(true);
assert_eq!(
vec![
LineBuffer::from(""),
LineBuffer::from("ab "),
LineBuffer::from("ab c")
],
editor.edits
);
}
}