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
//! Struct used to efficiently slice source code at (row, column) Locations.
use std::cell::OnceCell;
use ruff_source_file::{LineColumn, LineIndex, LineRanges, OneIndexed, SourceCode};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
#[derive(Debug)]
pub struct Locator<'a> {
contents: &'a str,
index: OnceCell<LineIndex>,
}
impl<'a> Locator<'a> {
pub const fn new(contents: &'a str) -> Self {
Self {
contents,
index: OnceCell::new(),
}
}
#[deprecated(
note = "This is expensive, avoid using outside of the diagnostic phase. Prefer the other `Locator` methods instead."
)]
pub(crate) fn compute_line_index(&self, offset: TextSize) -> OneIndexed {
self.to_index().line_index(offset)
}
#[deprecated(
note = "This is expensive, avoid using outside of the diagnostic phase. Prefer the other `Locator` methods instead."
)]
pub(crate) fn compute_source_location(&self, offset: TextSize) -> LineColumn {
self.to_source_code().line_column(offset)
}
pub fn to_index(&self) -> &LineIndex {
self.index
.get_or_init(|| LineIndex::from_source_text(self.contents))
}
pub fn to_source_code(&self) -> SourceCode<'_, '_> {
SourceCode::new(self.contents, self.to_index())
}
/// Take the source code up to the given [`TextSize`].
#[inline]
pub(crate) fn up_to(&self, offset: TextSize) -> &'a str {
&self.contents[TextRange::up_to(offset)]
}
/// Take the source code after the given [`TextSize`].
#[inline]
pub(crate) fn after(&self, offset: TextSize) -> &'a str {
&self.contents[usize::from(offset)..]
}
/// Finds the closest [`TextSize`] not exceeding the offset for which `is_char_boundary` is
/// `true`.
///
/// ## Examples
///
/// ```
/// # use ruff_text_size::{Ranged, TextRange, TextSize};
/// # use ruff_linter::Locator;
///
/// let locator = Locator::new("Hello");
///
/// assert_eq!(
/// locator.floor_char_boundary(TextSize::from(0)),
/// TextSize::from(0)
/// );
///
/// assert_eq!(
/// locator.floor_char_boundary(TextSize::from(5)),
/// TextSize::from(5)
/// );
///
/// let locator = Locator::new("α");
///
/// assert_eq!(
/// locator.floor_char_boundary(TextSize::from(0)),
/// TextSize::from(0)
/// );
///
/// assert_eq!(
/// locator.floor_char_boundary(TextSize::from(1)),
/// TextSize::from(0)
/// );
///
/// assert_eq!(
/// locator.floor_char_boundary(TextSize::from(2)),
/// TextSize::from(2)
/// );
/// ```
pub fn floor_char_boundary(&self, offset: TextSize) -> TextSize {
TextSize::try_from(self.contents.floor_char_boundary(offset.to_usize())).unwrap()
}
/// Take the source code between the given [`TextRange`].
#[inline]
pub(crate) fn slice<T: Ranged>(&self, ranged: T) -> &'a str {
&self.contents[ranged.range()]
}
/// Return the underlying source code.
pub const fn contents(&self) -> &'a str {
self.contents
}
/// Return the number of bytes in the source code.
pub(crate) const fn len(&self) -> usize {
self.contents.len()
}
pub(crate) fn text_len(&self) -> TextSize {
self.contents.text_len()
}
}
// Override the `_str` methods from [`LineRanges`] to extend the lifetime to `'a`.
impl<'a> Locator<'a> {
/// Returns the text of the `offset`'s line.
///
/// See [`LineRanges::full_lines_str`].
pub(crate) fn full_line_str(&self, offset: TextSize) -> &'a str {
self.contents.full_line_str(offset)
}
/// Returns the text of the `offset`'s line.
///
/// See [`LineRanges::line_str`].
pub(crate) fn line_str(&self, offset: TextSize) -> &'a str {
self.contents.line_str(offset)
}
/// Returns the text of all lines that include `range`.
///
/// See [`LineRanges::lines_str`].
pub(crate) fn lines_str(&self, range: TextRange) -> &'a str {
self.contents.lines_str(range)
}
}
// Allow calling [`LineRanges`] methods on [`Locator`] directly.
impl LineRanges for Locator<'_> {
#[inline]
fn line_start(&self, offset: TextSize) -> TextSize {
self.contents.line_start(offset)
}
#[inline]
fn bom_start_offset(&self) -> TextSize {
self.contents.bom_start_offset()
}
#[inline]
fn full_line_end(&self, offset: TextSize) -> TextSize {
self.contents.full_line_end(offset)
}
#[inline]
fn line_end(&self, offset: TextSize) -> TextSize {
self.contents.line_end(offset)
}
#[inline]
fn full_line_str(&self, offset: TextSize) -> &str {
self.contents.full_line_str(offset)
}
#[inline]
fn line_str(&self, offset: TextSize) -> &str {
self.contents.line_str(offset)
}
#[inline]
fn contains_line_break(&self, range: TextRange) -> bool {
self.contents.contains_line_break(range)
}
#[inline]
fn lines_str(&self, range: TextRange) -> &str {
self.contents.lines_str(range)
}
#[inline]
fn full_lines_str(&self, range: TextRange) -> &str {
self.contents.full_lines_str(range)
}
}