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
use super::chunk::Chunk;
use super::segment::Segment;
use crate::utils::span::SpannedText;
use std::rc::Rc;
use unicode_segmentation::UnicodeSegmentation as _;
use unicode_width::UnicodeWidthStr as _;
use xi_unicode::LineBreakLeafIter;
pub struct ChunkIterator<S> {
source: Rc<S>,
current_span: usize,
offset: usize,
}
impl<S> ChunkIterator<S> {
pub fn new(source: Rc<S>) -> Self {
ChunkIterator {
source,
current_span: 0,
offset: 0,
}
}
}
impl<S> Iterator for ChunkIterator<S>
where
S: SpannedText,
{
type Item = Chunk;
fn next(&mut self) -> Option<Self::Item> {
if self.current_span >= self.source.spans().len() {
return None;
}
if self.source.spans()[self.current_span].as_ref().is_empty() {
self.current_span += 1;
return self.next();
}
let mut span = self.source.spans()[self.current_span].as_ref();
let mut span_text = span.resolve(self.source.source());
let mut total_width = 0;
let mut segments: Vec<Segment> = Vec::new();
let mut iter = LineBreakLeafIter::new(span_text, self.offset);
loop {
let (pos, hard_stop) = iter.next(span_text);
let (width, ends_with_space) = if pos == 0 {
assert!(
self.current_span > 0,
"Cannot receive pos == 0 for the first span."
);
let prev_span_id = segments.last().unwrap().span_id;
let prev_span = self.source.spans()[prev_span_id].as_ref();
let prev_text = prev_span.resolve(self.source.source());
if hard_stop {
assert!(
!segments.is_empty(),
"Cannot receive pos == 0 in the first segment."
);
if let Some(to_remove) =
prev_text.graphemes(true).next_back().map(|g| g.len())
{
segments.last_mut().unwrap().end -= to_remove;
}
}
(0, prev_text.ends_with(' '))
} else {
let text = &span_text[self.offset..pos];
(text.width(), text.ends_with(' '))
};
if pos != 0 {
total_width += width;
let to_remove = if hard_stop {
let text = &span_text[self.offset..pos];
text.graphemes(true)
.next_back()
.map(|g| g.len())
.unwrap_or(0)
} else {
0
};
segments.push(Segment {
span_id: self.current_span,
start: self.offset,
end: pos - to_remove,
width,
});
}
if pos == span_text.len() {
assert!(
!hard_stop,
"Cannot have hard-break at the end of a span."
);
self.current_span += 1;
while let Some(true) =
self.source.spans().get(self.current_span).map(|span| {
span.as_ref().resolve(self.source.source()).is_empty()
})
{
self.current_span += 1;
}
if self.current_span >= self.source.spans().len() {
if span_text.ends_with('\n') {
segments.last_mut().unwrap().end -= 1;
}
return Some(Chunk {
width: total_width,
segments,
hard_stop,
ends_with_space,
});
}
span = self.source.spans()[self.current_span].as_ref();
span_text = span.resolve(self.source.source());
self.offset = 0;
continue;
}
self.offset = pos;
return Some(Chunk {
width: total_width,
segments,
hard_stop,
ends_with_space,
});
}
}
}