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
use super::*;
pub trait TextAdaptor {
fn text(&self) -> String;
fn count_bytes(&self) -> usize;
fn count_lines(&self) -> usize;
fn count_chars(&self) -> usize;
fn offset_to_position(&self, offset: Offset) -> Option<Position>;
fn line_range(&self, line: u32) -> Option<Range<Position>>;
fn sub_string(&self, range: Range<Position>) -> Option<&str>;
fn offset_range_to_position_range(&self, offsets: &OffsetRange) -> Option<PositionRange> {
let start = self.offset_to_position(offsets.start)?;
let end = self.offset_to_position(offsets.end)?;
Some(start..end)
}
fn offset_pair_to_position_range(&self, start: usize, end: usize) -> Option<Range<Position>> {
self.offset_range_to_position_range(&Range { start, end })
}
}
impl Position {
pub fn new(line: u32, column: u32) -> Self {
Self { line, column }
}
pub fn as_offset(&self, text: &TextIndex) -> Option<Offset> {
let line_range = text.line_ranges.get(self.line as usize)?;
Some(line_range.start as usize + (self.column as usize))
}
}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Position {
fn cmp(&self, other: &Self) -> Ordering {
let line_cmp = self.line.cmp(&other.line);
if line_cmp == Ordering::Equal { self.column.cmp(&other.column) } else { line_cmp }
}
}