Skip to content

Commit 84a6cb3

Browse files
committed
Inline
1 parent 02e8bb0 commit 84a6cb3

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

lib/line-index/src/lib.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,33 +157,9 @@ impl LineIndex {
157157

158158
/// Transforms the `LineCol` with the given `WideEncoding` into a `WideLineCol`.
159159
pub fn to_wide(&self, enc: WideEncoding, line_col: LineCol) -> Option<WideLineCol> {
160-
let col = self.utf8_to_wide_col(enc, line_col.line, line_col.col.into());
161-
Some(WideLineCol { line: line_col.line, col: col as u32 })
162-
}
163-
164-
/// Transforms the `WideLineCol` with the given `WideEncoding` into a `LineCol`.
165-
pub fn to_utf8(&self, enc: WideEncoding, line_col: WideLineCol) -> Option<LineCol> {
166-
let col = self.wide_to_utf8_col(enc, line_col.line, line_col.col);
167-
Some(LineCol { line: line_col.line, col: col.into() })
168-
}
169-
170-
/// Returns an iterator over the ranges for the lines.
171-
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
172-
let lo = self.newlines.partition_point(|&it| it < range.start());
173-
let hi = self.newlines.partition_point(|&it| it <= range.end());
174-
let all = std::iter::once(range.start())
175-
.chain(self.newlines[lo..hi].iter().copied())
176-
.chain(std::iter::once(range.end()));
177-
178-
all.clone()
179-
.zip(all.skip(1))
180-
.map(|(lo, hi)| TextRange::new(lo, hi))
181-
.filter(|it| !it.is_empty())
182-
}
183-
184-
fn utf8_to_wide_col(&self, enc: WideEncoding, line: u32, col: TextSize) -> usize {
160+
let col: TextSize = line_col.col.into();
185161
let mut res: usize = col.into();
186-
if let Some(wide_chars) = self.line_wide_chars.get(&line) {
162+
if let Some(wide_chars) = self.line_wide_chars.get(&line_col.line) {
187163
for c in wide_chars.iter() {
188164
if c.end <= col {
189165
res -= usize::from(c.len()) - c.wide_len(enc);
@@ -194,11 +170,13 @@ impl LineIndex {
194170
}
195171
}
196172
}
197-
res
173+
Some(WideLineCol { line: line_col.line, col: res as u32 })
198174
}
199175

200-
fn wide_to_utf8_col(&self, enc: WideEncoding, line: u32, mut col: u32) -> TextSize {
201-
if let Some(wide_chars) = self.line_wide_chars.get(&line) {
176+
/// Transforms the `WideLineCol` with the given `WideEncoding` into a `LineCol`.
177+
pub fn to_utf8(&self, enc: WideEncoding, line_col: WideLineCol) -> Option<LineCol> {
178+
let mut col = line_col.col;
179+
if let Some(wide_chars) = self.line_wide_chars.get(&line_col.line) {
202180
for c in wide_chars.iter() {
203181
if col > u32::from(c.start) {
204182
col += u32::from(c.len()) - c.wide_len(enc) as u32;
@@ -209,7 +187,20 @@ impl LineIndex {
209187
}
210188
}
211189
}
190+
Some(LineCol { line: line_col.line, col: col.into() })
191+
}
192+
193+
/// Returns an iterator over the ranges for the lines.
194+
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
195+
let lo = self.newlines.partition_point(|&it| it < range.start());
196+
let hi = self.newlines.partition_point(|&it| it <= range.end());
197+
let all = std::iter::once(range.start())
198+
.chain(self.newlines[lo..hi].iter().copied())
199+
.chain(std::iter::once(range.end()));
212200

213-
col.into()
201+
all.clone()
202+
.zip(all.skip(1))
203+
.map(|(lo, hi)| TextRange::new(lo, hi))
204+
.filter(|it| !it.is_empty())
214205
}
215206
}

0 commit comments

Comments
 (0)