Unnamed repository; edit this file 'description' to name the repository.
Inline
Ariel Davis 2023-05-06
parent 02e8bb0 · commit 84a6cb3
-rw-r--r--lib/line-index/src/lib.rs51
1 files changed, 21 insertions, 30 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs
index 3c10fbe20c..eecc1edb13 100644
--- a/lib/line-index/src/lib.rs
+++ b/lib/line-index/src/lib.rs
@@ -157,33 +157,9 @@ impl LineIndex {
/// Transforms the `LineCol` with the given `WideEncoding` into a `WideLineCol`.
pub fn to_wide(&self, enc: WideEncoding, line_col: LineCol) -> Option<WideLineCol> {
- let col = self.utf8_to_wide_col(enc, line_col.line, line_col.col.into());
- Some(WideLineCol { line: line_col.line, col: col as u32 })
- }
-
- /// Transforms the `WideLineCol` with the given `WideEncoding` into a `LineCol`.
- pub fn to_utf8(&self, enc: WideEncoding, line_col: WideLineCol) -> Option<LineCol> {
- let col = self.wide_to_utf8_col(enc, line_col.line, line_col.col);
- Some(LineCol { line: line_col.line, col: col.into() })
- }
-
- /// Returns an iterator over the ranges for the lines.
- pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
- let lo = self.newlines.partition_point(|&it| it < range.start());
- let hi = self.newlines.partition_point(|&it| it <= range.end());
- let all = std::iter::once(range.start())
- .chain(self.newlines[lo..hi].iter().copied())
- .chain(std::iter::once(range.end()));
-
- all.clone()
- .zip(all.skip(1))
- .map(|(lo, hi)| TextRange::new(lo, hi))
- .filter(|it| !it.is_empty())
- }
-
- fn utf8_to_wide_col(&self, enc: WideEncoding, line: u32, col: TextSize) -> usize {
+ let col: TextSize = line_col.col.into();
let mut res: usize = col.into();
- if let Some(wide_chars) = self.line_wide_chars.get(&line) {
+ if let Some(wide_chars) = self.line_wide_chars.get(&line_col.line) {
for c in wide_chars.iter() {
if c.end <= col {
res -= usize::from(c.len()) - c.wide_len(enc);
@@ -194,11 +170,13 @@ impl LineIndex {
}
}
}
- res
+ Some(WideLineCol { line: line_col.line, col: res as u32 })
}
- fn wide_to_utf8_col(&self, enc: WideEncoding, line: u32, mut col: u32) -> TextSize {
- if let Some(wide_chars) = self.line_wide_chars.get(&line) {
+ /// Transforms the `WideLineCol` with the given `WideEncoding` into a `LineCol`.
+ pub fn to_utf8(&self, enc: WideEncoding, line_col: WideLineCol) -> Option<LineCol> {
+ let mut col = line_col.col;
+ if let Some(wide_chars) = self.line_wide_chars.get(&line_col.line) {
for c in wide_chars.iter() {
if col > u32::from(c.start) {
col += u32::from(c.len()) - c.wide_len(enc) as u32;
@@ -209,7 +187,20 @@ impl LineIndex {
}
}
}
+ Some(LineCol { line: line_col.line, col: col.into() })
+ }
+
+ /// Returns an iterator over the ranges for the lines.
+ pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
+ let lo = self.newlines.partition_point(|&it| it < range.start());
+ let hi = self.newlines.partition_point(|&it| it <= range.end());
+ let all = std::iter::once(range.start())
+ .chain(self.newlines[lo..hi].iter().copied())
+ .chain(std::iter::once(range.end()));
- col.into()
+ all.clone()
+ .zip(all.skip(1))
+ .map(|(lo, hi)| TextRange::new(lo, hi))
+ .filter(|it| !it.is_empty())
}
}