-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathedit_diff.rs
136 lines (133 loc) · 4.18 KB
/
edit_diff.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
use crate::row::Row;
#[derive(Debug, Clone, Copy)]
pub enum UndoRedo {
Undo,
Redo,
}
#[derive(Debug)]
pub enum EditDiff {
InsertChar(usize, usize, char),
DeleteChar(usize, usize, char),
Insert(usize, usize, String),
Append(usize, String),
Truncate(usize, String),
Remove(usize, usize, String),
Newline,
InsertLine(usize, String),
DeleteLine(usize, String),
}
impl EditDiff {
pub fn apply(&self, rows: &mut Vec<Row>, which: UndoRedo) -> (usize, usize) {
// Returns cursor's next position (x, y)
use UndoRedo::*;
match *self {
EditDiff::InsertChar(x, y, c) => match which {
Redo => {
rows[y].insert_char(x, c);
(x + 1, y)
}
Undo => {
rows[y].remove_char(x);
(x, y)
}
},
EditDiff::DeleteChar(x, y, c) => match which {
Redo => {
rows[y].remove_char(x - 1);
(x - 1, y)
}
Undo => {
rows[y].insert_char(x - 1, c);
(x, y)
}
},
EditDiff::Append(y, ref s) => match which {
Redo => {
let len = rows[y].len();
rows[y].append(s);
(len, y)
}
Undo => {
let count = s.chars().count();
let len = rows[y].len();
rows[y].remove(len - count, len);
(rows[y].len(), y)
}
},
EditDiff::Truncate(y, ref s) => match which {
Redo => {
let count = s.chars().count();
let len = rows[y].len();
rows[y].truncate(len - count);
(len - count, y)
}
Undo => {
rows[y].append(s);
let x = rows[y].len() - s.chars().count();
(x, y)
}
},
EditDiff::Insert(x, y, ref s) => match which {
Redo => {
rows[y].insert_str(x, s);
(x + s.chars().count(), y)
}
Undo => {
rows[y].remove(x, s.chars().count());
(x, y)
}
},
EditDiff::Remove(x, y, ref s) => match which {
Redo => {
let next_x = x - s.chars().count();
rows[y].remove(next_x, x);
(next_x, y)
}
Undo => {
let count = s.chars().count();
rows[y].insert_str(x - count, s);
(x, y)
}
},
EditDiff::Newline => match which {
Redo => {
rows.push(Row::empty());
(0, rows.len() - 1)
}
Undo => {
debug_assert_eq!(rows[rows.len() - 1].buffer(), "");
rows.pop();
(0, rows.len())
}
},
EditDiff::InsertLine(y, ref s) => match which {
Redo => {
rows.insert(y, Row::new(s).unwrap());
(0, y)
}
Undo => {
rows.remove(y);
(rows[y - 1].len(), y - 1)
}
},
EditDiff::DeleteLine(y, ref s) => match which {
Redo => {
if y == rows.len() - 1 {
rows.pop();
} else {
rows.remove(y);
}
(rows[y - 1].len(), y - 1)
}
Undo => {
if y == rows.len() {
rows.push(Row::new(s).unwrap());
} else {
rows.insert(y, Row::new(s).unwrap());
}
(0, y)
}
},
}
}
}