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
use std::{fmt, sync::Arc};
use serde::{Deserialize, Serialize};
use tendril::StrTendril;
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct Span {
pub line_start: usize,
pub line_stop: usize,
pub col_start: usize,
pub col_stop: usize,
pub path: Arc<String>,
#[serde(with = "crate::common::tendril_json")]
pub content: StrTendril,
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.line_start == self.line_stop {
write!(f, "{}:{}-{}", self.line_start, self.col_start, self.col_stop)
} else {
write!(
f,
"{}:{}-{}:{}",
self.line_start, self.col_start, self.line_stop, self.col_stop
)
}
}
}
impl<'ast> From<pest::Span<'ast>> for Span {
fn from(span: pest::Span) -> Self {
let start = span.start_pos().line_col();
let end = span.end_pos().line_col();
Span {
line_start: start.0,
line_stop: end.0,
col_start: start.1,
col_stop: end.1,
path: Arc::new(String::new()),
content: span.as_str().into(),
}
}
}
impl std::ops::Add for &Span {
type Output = Span;
fn add(self, other: &Span) -> Span {
self.clone() + other.clone()
}
}
impl std::ops::Add for Span {
type Output = Self;
#[allow(clippy::comparison_chain)]
fn add(self, other: Self) -> Self {
if self.line_start == other.line_stop {
Span {
line_start: self.line_start,
line_stop: self.line_stop,
col_start: self.col_start.min(other.col_start),
col_stop: self.col_stop.max(other.col_stop),
path: self.path,
content: self.content,
}
} else {
let mut new_content = vec![];
let self_lines = self.content.lines().collect::<Vec<_>>();
let other_lines = other.content.lines().collect::<Vec<_>>();
for line in self.line_start.min(other.line_start)..self.line_stop.max(other.line_stop) + 1 {
if line >= self.line_start && line <= self.line_stop {
new_content.push(self_lines.get(line - self.line_start).copied().unwrap_or_default());
} else if line >= other.line_start && line <= other.line_stop {
new_content.push(other_lines.get(line - other.line_start).copied().unwrap_or_default());
} else if new_content.last().map(|x| *x != "...").unwrap_or(true) {
new_content.push("...");
}
}
let new_content = new_content.join("\n").into();
if self.line_start < other.line_stop {
Span {
line_start: self.line_start,
line_stop: other.line_stop,
col_start: self.col_start,
col_stop: other.col_stop,
path: self.path,
content: new_content,
}
} else {
Span {
line_start: other.line_start,
line_stop: self.line_stop,
col_start: other.col_start,
col_stop: self.col_stop,
path: self.path,
content: new_content,
}
}
}
}
}