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
use crate::utils::span::{IndexedCow, Span, SpannedStr, SpannedText};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Segment {
pub span_id: usize,
pub start: usize,
pub end: usize,
pub width: usize,
}
impl Segment {
pub fn resolve<'a, T>(&self, source: &SpannedStr<'a, T>) -> Span<'a, T> {
let span = &source.spans_raw()[self.span_id];
let content = span.content.resolve(source.source());
let content = &content[self.start..self.end];
Span {
content,
attr: &span.attr,
width: self.width,
}
}
pub fn resolve_plain<'a, S>(&self, source: &'a S) -> &'a str
where
S: SpannedText,
{
let span = &source.spans()[self.span_id];
let content = span.as_ref().resolve(source.source());
&content[self.start..self.end]
}
pub fn source_indices<S>(&self, spans: &[S]) -> Option<(usize, usize)>
where
S: AsRef<IndexedCow>,
{
let span = spans[self.span_id].as_ref();
if let IndexedCow::Borrowed { start, .. } = *span {
Some((self.start + start, self.end + start))
} else {
None
}
}
}