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
use rowan::{TextRange, TextSize};
use std::collections::BTreeMap;
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Default)]
pub struct Position {
pub line: u64,
pub character: u64,
}
impl Position {
pub fn new(line: u64, character: u64) -> Self {
Position { line, character }
}
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
pub struct Range {
pub start: Position,
pub end: Position,
}
pub type CharacterOffset = u64;
#[derive(Debug, Clone, Copy)]
pub struct CharacterRange(u64, u64);
#[derive(Debug, Clone)]
pub struct Mapper {
offset_to_position: BTreeMap<TextSize, Position>,
position_to_offset: BTreeMap<Position, TextSize>,
lines: usize,
end: Position,
}
impl Mapper {
pub fn new_utf16(source: &str, one_based: bool) -> Self {
Self::new_impl(source, true, if one_based { 1 } else { 0 })
}
pub fn new_utf8(source: &str, one_based: bool) -> Self {
Self::new_impl(source, false, if one_based { 1 } else { 0 })
}
pub fn offset(&self, position: Position) -> Option<TextSize> {
self.position_to_offset.get(&position).copied()
}
pub fn text_range(&self, range: Range) -> Option<TextRange> {
self.offset(range.start)
.and_then(|start| self.offset(range.end).map(|end| TextRange::new(start, end)))
}
pub fn position(&self, offset: TextSize) -> Option<Position> {
self.offset_to_position.get(&offset).copied()
}
pub fn range(&self, range: TextRange) -> Option<Range> {
self.position(range.start())
.and_then(|start| self.position(range.end()).map(|end| Range { start, end }))
}
pub fn mappings(&self) -> (&BTreeMap<TextSize, Position>, &BTreeMap<Position, TextSize>) {
(&self.offset_to_position, &self.position_to_offset)
}
pub fn line_count(&self) -> usize {
self.lines
}
pub fn all_range(&self) -> Range {
Range {
start: Position {
line: 0,
character: 0,
},
end: self.end,
}
}
fn new_impl(source: &str, utf16: bool, base: u64) -> Self {
let mut offset_to_position = BTreeMap::new();
let mut position_to_offset = BTreeMap::new();
let mut line: u64 = base;
let mut character: u64 = base;
let mut last_offset = 0;
for c in source.chars() {
let new_offset = last_offset + c.len_utf8();
let character_size = if utf16 { c.len_utf16() } else { 1 };
offset_to_position.extend(
(last_offset..new_offset)
.map(|b| (TextSize::from(b as u32), Position { line, character })),
);
position_to_offset.extend(
(last_offset..new_offset)
.map(|b| (Position { line, character }, TextSize::from(b as u32))),
);
last_offset = new_offset;
character += character_size as u64;
if c == '\n' {
line += 1;
character = base;
}
}
offset_to_position.insert(
TextSize::from(last_offset as u32),
Position { line, character },
);
position_to_offset.insert(
Position { line, character },
TextSize::from(last_offset as u32),
);
Self {
offset_to_position,
position_to_offset,
lines: line as usize,
end: Position { line, character },
}
}
}
pub trait SplitLines {
fn is_single_line(&self) -> bool;
fn split_lines(self, mapper: &Mapper) -> Vec<Range>;
}
impl SplitLines for Range {
fn split_lines(self, _mapper: &Mapper) -> Vec<Range> {
unimplemented!()
}
fn is_single_line(&self) -> bool {
self.start.line == self.end.line
}
}
pub fn relative_position(position: Position, to: Position) -> Position {
if position.line == to.line {
Position {
line: 0,
character: position.character - to.character,
}
} else {
Position {
line: position.line - to.line,
character: position.character,
}
}
}
pub fn relative_range(range: Range, to: Range) -> Range {
let line_diff = range.end.line - range.start.line;
let start = relative_position(range.start, to.start);
let end = if line_diff == 0 {
Position {
line: start.line,
character: start.character + range.end.character - range.start.character,
}
} else {
Position {
line: start.line + line_diff,
character: range.end.character,
}
};
Range { start, end }
}
#[cfg(test)]
#[test]
fn test_mapper() {
let s1 = r#"
line-2
line-3"#;
let mapper = Mapper::new_utf16(s1, false);
assert!(s1.len() == mapper.mappings().0.len() - 1);
assert!(
mapper.position(0.into()).unwrap()
== Position {
line: 0,
character: 0
}
);
assert!(
mapper
.position(TextSize::from(s1.len() as u32 - 1 as u32))
.unwrap()
== Position {
line: 2,
character: 5
}
)
}