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
#[cfg(test)]
#[path = "span_test.rs"]
mod test;
use std::iter::Sum;
use std::ops::{Add, Sub};
use crate::db::FilesGroup;
use crate::ids::FileId;
#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextWidth(u32);
impl TextWidth {
pub fn from_char(c: char) -> Self {
Self(c.len_utf8() as u32)
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Self {
Self(s.len() as u32)
}
pub fn new_for_testing(value: u32) -> Self {
Self(value)
}
}
impl Add for TextWidth {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Sub for TextWidth {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl Sum for TextWidth {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
Self(iter.map(|x| x.0).sum())
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TextOffset(TextWidth);
impl TextOffset {
pub fn add_width(&self, width: TextWidth) -> Self {
TextOffset(self.0 + width)
}
pub fn sub_width(&self, width: TextWidth) -> Self {
TextOffset(self.0 - width)
}
pub fn take_from<'a>(&self, content: &'a str) -> &'a str {
&content[(self.0.0 as usize)..]
}
}
impl Sub for TextOffset {
type Output = TextWidth;
fn sub(self, rhs: Self) -> Self::Output {
TextWidth(self.0.0 - rhs.0.0)
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct TextSpan {
pub start: TextOffset,
pub end: TextOffset,
}
impl TextSpan {
pub fn width(&self) -> TextWidth {
self.end - self.start
}
pub fn contains(&self, other: Self) -> bool {
self.start <= other.start && self.end >= other.end
}
pub fn take<'b>(&self, content: &'b str) -> &'b str {
&content[(self.start.0.0 as usize)..(self.end.0.0 as usize)]
}
pub fn n_chars(&self, content: &str) -> usize {
self.take(content).chars().count()
}
pub fn after(&self) -> Self {
Self { start: self.end, end: self.end }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TextPosition {
pub line: usize,
pub col: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileSummary {
pub line_offsets: Vec<TextOffset>,
pub last_offset: TextOffset,
}
impl TextOffset {
pub fn get_line_number(&self, db: &dyn FilesGroup, file: FileId) -> Option<usize> {
let summary = db.file_summary(file)?;
assert!(
*self <= summary.last_offset,
"TextOffset out of range. {:?} > {:?}.",
self.0,
summary.last_offset.0
);
Some(summary.line_offsets.binary_search(self).unwrap_or_else(|x| x - 1))
}
pub fn position_in_file(&self, db: &dyn FilesGroup, file: FileId) -> Option<TextPosition> {
let summary = db.file_summary(file)?;
let line_number = self.get_line_number(db, file)?;
let line_offset = summary.line_offsets[line_number];
let content = db.file_content(file)?;
let col = TextSpan { start: line_offset, end: *self }.n_chars(&content);
Some(TextPosition { line: line_number, col })
}
}
impl FileSummary {
pub fn line_count(&self) -> usize {
self.line_offsets.len()
}
}