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::document::FileInfo;
use crate::event::{Error, Result, Status};
use crate::st;
use crate::utils::raw_indices;
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
#[derive(Debug, PartialEq, Clone)]
pub struct Row {
pub text: Vec<char>,
pub indices: Vec<usize>,
pub modified: bool,
pub info: *mut FileInfo,
}
impl Row {
#[cfg(not(tarpaulin_include))]
pub fn new<S: Into<String>>(raw: S) -> Self {
let raw = raw.into();
let text: Vec<char> = raw.chars().collect();
Self {
indices: Row::raw_to_indices(&text, 4),
text,
modified: false,
info: std::ptr::null_mut(),
}
}
pub fn link(mut self, link: *mut FileInfo) -> Self {
self.info = link;
let tabs = self.get_tab_width();
self.indices = Row::raw_to_indices(&self.text, tabs);
self
}
pub fn insert<S: Into<String>>(&mut self, start: usize, text: S) -> Result<Status> {
if start > self.width() {
return Err(Error::OutOfRange);
}
let text = text.into();
self.text.splice(start..start, text.chars());
let tabs = self.get_tab_width();
self.indices = Row::raw_to_indices(&self.text, tabs);
self.modified = true;
Ok(Status::None)
}
pub fn remove<R>(&mut self, range: R) -> Result<Status>
where
R: std::ops::RangeBounds<usize>,
{
if let std::ops::Bound::Included(start) = range.start_bound() {
if start > &self.width() {
return Err(Error::OutOfRange);
}
}
self.text.splice(range, []);
let tabs = self.get_tab_width();
self.indices = Row::raw_to_indices(&self.text, tabs);
self.modified = true;
Ok(Status::None)
}
pub fn split(&self, idx: usize) -> Result<(Row, Row)> {
let left = self
.text
.get(..idx)
.ok_or(Error::OutOfRange)?
.iter()
.fold(st!(""), |a, x| format!("{}{}", a, x));
let right = self
.text
.get(idx..)
.ok_or(Error::OutOfRange)?
.iter()
.fold(st!(""), |a, x| format!("{}{}", a, x));
let mut left = Row::new(left).link(self.info);
left.modified = true;
let right = Row::new(right).link(self.info);
Ok((left, right))
}
pub fn splice(&mut self, mut row: Row) -> Row {
let mut text = self.text.clone();
text.append(&mut row.text);
let indices = Row::raw_to_indices(&text, self.get_tab_width());
Row {
indices,
text,
modified: true,
info: self.info,
}
}
#[must_use]
pub fn words(&self) -> Vec<usize> {
crate::utils::words(self)
}
#[must_use]
pub fn render(&self, range: std::ops::RangeFrom<usize>) -> String {
let mut start = range.start;
let text = self.render_full();
if start >= text.width() {
return st!("");
}
let ind = raw_indices(&text, &self.indices);
let space = !ind.contains_key(&start);
while !ind.contains_key(&start) {
start += 1;
}
format!("{}{}", if space { " " } else { "" }, &text[ind[&start]..])
}
#[must_use]
pub fn render_full(&self) -> String {
let tabs = self.get_tab_width();
self.text.iter().fold(st!(""), |a, x| {
format!(
"{}{}",
a,
if x == &'\t' {
" ".repeat(tabs)
} else {
x.to_string()
}
)
})
}
#[must_use]
pub fn render_raw(&self) -> String {
self.text.iter().fold(st!(""), |a, x| format!("{}{}", a, x))
}
#[must_use]
pub fn len(&self) -> usize {
self.text.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[must_use]
pub fn width(&self) -> usize {
*self.indices.last().unwrap_or(&0)
}
#[must_use]
pub fn get_char_ptr(&self, x: usize) -> usize {
if x >= self.width() {
return self.len();
}
self.indices.iter().position(|i| &x == i).unwrap_or(0)
}
fn raw_to_indices(text: &[char], tab_width: usize) -> Vec<usize> {
let mut data = vec![&'\x00'];
data.splice(1.., text);
data.iter()
.map(|c| {
if c == &&'\t' {
tab_width
} else {
c.width().unwrap_or(0)
}
})
.scan(0, |a, x| {
*a += x;
Some(*a)
})
.collect()
}
#[must_use]
pub fn get_tab_width(&self) -> usize {
if self.info.is_null() {
4
} else {
unsafe { &*self.info }.tab_width
}
}
}