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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use std::borrow::Cow;
use std::iter::FromIterator;
use unicode_width::UnicodeWidthStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SpannedString<T> {
source: String,
spans: Vec<IndexedSpan<T>>,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SpannedStr<'a, T> {
source: &'a str,
spans: &'a [IndexedSpan<T>],
}
pub trait SpannedText {
type S: AsRef<IndexedCow>;
fn source(&self) -> &str;
fn spans(&self) -> &[Self::S];
fn as_ref(&self) -> SpannedTextRef<'_, Self> {
SpannedTextRef { r: self }
}
}
pub struct SpannedTextRef<'a, C>
where
C: SpannedText + ?Sized,
{
r: &'a C,
}
impl<T> Default for SpannedString<T> {
fn default() -> Self {
SpannedString::new()
}
}
impl<'a, T> SpannedText for &'a SpannedString<T> {
type S = IndexedSpan<T>;
fn source(&self) -> &str {
&self.source
}
fn spans(&self) -> &[IndexedSpan<T>] {
&self.spans
}
}
impl<'a, C> SpannedText for SpannedTextRef<'a, C>
where
C: 'a + SpannedText + ?Sized,
{
type S = C::S;
fn source(&self) -> &str {
self.r.source()
}
fn spans(&self) -> &[C::S] {
self.r.spans()
}
}
impl<'a, T> SpannedText for SpannedStr<'a, T>
where
T: 'a,
{
type S = IndexedSpan<T>;
fn source(&self) -> &str {
self.source
}
fn spans(&self) -> &[IndexedSpan<T>] {
self.spans
}
}
impl<S, T> From<S> for SpannedString<T>
where
S: Into<String>,
T: Default,
{
fn from(value: S) -> Self {
Self::single_span(value.into(), T::default())
}
}
impl<'a, T> SpannedStr<'a, T>
where
T: 'a,
{
pub fn new(source: &'a str, spans: &'a [IndexedSpan<T>]) -> Self {
SpannedStr { source, spans }
}
pub fn spans<'b>(
&'b self,
) -> impl DoubleEndedIterator<Item = Span<'a, T>>
+ ExactSizeIterator<Item = Span<'a, T>>
+ 'b
where
'a: 'b,
{
let source = self.source;
self.spans.iter().map(move |span| span.resolve(source))
}
pub fn spans_raw(&self) -> &'a [IndexedSpan<T>] {
self.spans
}
pub fn source(&self) -> &'a str {
self.source
}
pub fn is_empty(&self) -> bool {
self.source.is_empty() || self.spans.is_empty()
}
}
impl<'a, T> Clone for SpannedStr<'a, T> {
fn clone(&self) -> Self {
SpannedStr {
source: self.source,
spans: self.spans,
}
}
}
impl SpannedString<()> {
pub fn plain<S>(content: S) -> Self
where
S: Into<String>,
{
Self::single_span(content, ())
}
}
impl<T> SpannedString<T> {
pub fn new() -> Self {
Self::with_spans(String::new(), Vec::new())
}
pub fn concatenate<I>(spans: I) -> Self
where
I: IntoIterator<Item = Self>,
{
spans.into_iter().collect()
}
pub fn with_spans<S>(source: S, spans: Vec<IndexedSpan<T>>) -> Self
where
S: Into<String>,
{
let source = source.into();
for span in &spans {
if let IndexedCow::Borrowed { end, .. } = span.content {
assert!(end <= source.len());
}
}
SpannedString { source, spans }
}
pub fn compact(&mut self) {
let mut source = String::new();
for span in &mut self.spans {
let start = source.len();
source.push_str(span.content.resolve(&self.source));
let end = source.len();
span.content = IndexedCow::Borrowed { start, end };
}
self.source = source;
}
pub fn trim_end(&mut self) {
if let Some(max) = self
.spans
.iter()
.filter_map(|s| s.content.as_borrowed())
.map(|(_start, end)| end)
.max()
{
self.source.truncate(max);
}
}
pub fn trim_start(&mut self) {
if let Some(min) = self
.spans
.iter()
.filter_map(|s| s.content.as_borrowed())
.map(|(start, _end)| start)
.min()
{
self.source.drain(..min);
for span in &mut self.spans {
span.content.rev_offset(min);
}
}
}
pub fn trim(&mut self) {
self.trim_end();
self.trim_start();
}
pub fn single_span<S>(source: S, attr: T) -> Self
where
S: Into<String>,
{
let source = source.into();
let spans = vec![IndexedSpan::simple_borrowed(&source, attr)];
Self::with_spans(source, spans)
}
pub fn append<S>(&mut self, other: S)
where
S: Into<Self>,
{
let other = other.into();
self.append_raw(&other.source, other.spans);
}
pub fn append_raw(&mut self, source: &str, spans: Vec<IndexedSpan<T>>) {
let offset = self.source.len();
let mut spans = spans;
for span in &mut spans {
span.content.offset(offset);
}
self.source.push_str(source);
self.spans.append(&mut spans);
}
pub fn remove_spans<R>(&mut self, range: R)
where
R: std::ops::RangeBounds<usize>,
{
self.spans.drain(range);
}
pub fn spans(
&self,
) -> impl DoubleEndedIterator<Item = Span<'_, T>>
+ ExactSizeIterator<Item = Span<'_, T>> {
let source = &self.source;
self.spans.iter().map(move |span| span.resolve(source))
}
pub fn spans_attr_mut(&mut self) -> impl Iterator<Item = SpanMut<'_, T>> {
let source = &self.source;
self.spans
.iter_mut()
.map(move |span| span.resolve_mut(source))
}
pub fn spans_raw(&self) -> &[IndexedSpan<T>] {
&self.spans
}
pub fn spans_raw_attr_mut(
&mut self,
) -> impl DoubleEndedIterator<Item = IndexedSpanRefMut<'_, T>>
+ ExactSizeIterator<Item = IndexedSpanRefMut<'_, T>> {
self.spans.iter_mut().map(IndexedSpan::as_ref_mut)
}
pub fn source(&self) -> &str {
&self.source
}
pub fn is_empty(&self) -> bool {
self.source.is_empty() || self.spans.is_empty()
}
pub fn width(&self) -> usize {
self.spans().map(|s| s.width).sum()
}
}
impl<T> FromIterator<SpannedString<T>> for SpannedString<T> {
fn from_iter<I: IntoIterator<Item = SpannedString<T>>>(
iter: I,
) -> SpannedString<T> {
let mut iter = iter.into_iter();
if let Some(first) = iter.next() {
iter.fold(first, |mut acc, s| {
acc.append(s);
acc
})
} else {
SpannedString::new()
}
}
}
impl<'a, T> From<&'a SpannedString<T>> for SpannedStr<'a, T> {
fn from(other: &'a SpannedString<T>) -> Self {
SpannedStr::new(&other.source, &other.spans)
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct IndexedSpanRefMut<'a, T> {
pub content: &'a IndexedCow,
pub attr: &'a mut T,
pub width: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IndexedSpan<T> {
pub content: IndexedCow,
pub attr: T,
pub width: usize,
}
impl<T> AsRef<IndexedCow> for IndexedSpan<T> {
fn as_ref(&self) -> &IndexedCow {
&self.content
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SpanMut<'a, T> {
pub content: &'a str,
pub attr: &'a mut T,
pub width: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Span<'a, T> {
pub content: &'a str,
pub attr: &'a T,
pub width: usize,
}
impl<T> IndexedSpan<T> {
pub fn resolve<'a>(&'a self, source: &'a str) -> Span<'a, T>
where
T: 'a,
{
Span {
content: self.content.resolve(source),
attr: &self.attr,
width: self.width,
}
}
pub fn resolve_mut<'a>(&'a mut self, source: &'a str) -> SpanMut<'a, T>
where
T: 'a,
{
SpanMut {
content: self.content.resolve(source),
attr: &mut self.attr,
width: self.width,
}
}
pub fn as_ref_mut(&mut self) -> IndexedSpanRefMut<'_, T> {
IndexedSpanRefMut {
content: &self.content,
attr: &mut self.attr,
width: self.width,
}
}
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}
pub fn simple_borrowed(content: &str, attr: T) -> Self {
IndexedSpan {
content: IndexedCow::Borrowed {
start: 0,
end: content.len(),
},
attr,
width: content.width(),
}
}
pub fn simple_owned(content: String, attr: T) -> Self {
let width = content.width();
IndexedSpan {
content: IndexedCow::Owned(content),
attr,
width,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum IndexedCow {
Borrowed {
start: usize,
end: usize,
},
Owned(String),
}
impl IndexedCow {
pub fn resolve<'a>(&'a self, source: &'a str) -> &'a str {
match *self {
IndexedCow::Borrowed { start, end } => &source[start..end],
IndexedCow::Owned(ref content) => content,
}
}
pub fn as_borrowed(&self) -> Option<(usize, usize)> {
if let IndexedCow::Borrowed { start, end } = *self {
Some((start, end))
} else {
None
}
}
pub fn as_owned(&self) -> Option<&str> {
if let IndexedCow::Owned(ref content) = *self {
Some(content)
} else {
None
}
}
pub fn from_str(value: &str, source: &str) -> Self {
let source_pos = source.as_ptr() as usize;
let value_pos = value.as_ptr() as usize;
assert!(value_pos >= source_pos);
assert!(value_pos + value.len() <= source_pos + source.len());
let start = value_pos - source_pos;
let end = start + value.len();
IndexedCow::Borrowed { start, end }
}
pub fn from_cow(cow: Cow<'_, str>, source: &str) -> Self {
match cow {
Cow::Owned(value) => IndexedCow::Owned(value),
Cow::Borrowed(value) => IndexedCow::from_str(value, source),
}
}
pub fn is_empty(&self) -> bool {
match *self {
IndexedCow::Borrowed { start, end } => start == end,
IndexedCow::Owned(ref content) => content.is_empty(),
}
}
pub fn offset(&mut self, offset: usize) {
if let IndexedCow::Borrowed {
ref mut start,
ref mut end,
} = *self
{
*start += offset;
*end += offset;
}
}
pub fn rev_offset(&mut self, offset: usize) {
if let IndexedCow::Borrowed {
ref mut start,
ref mut end,
} = *self
{
*start = start.saturating_sub(offset);
*end = end.saturating_sub(offset);
}
}
}