diff options
| author | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 00:59:56 -0700 |
|---|---|---|
| committer | Ariel Davis <ariel.z.davis@icloud.com> | 2023-05-06 00:59:56 -0700 |
| commit | 84a6cb3bfc98ec9692eb82a8e2016d69d95b3209 (patch) | |
| tree | cca05f17df184006037e9ae35b4b8e884eaad646 | |
| parent | 02e8bb0c6e3854485e72f896f889673a4f3ad762 (diff) | |
| download | rust-84a6cb3bfc98ec9692eb82a8e2016d69d95b3209.tar.gz rust-84a6cb3bfc98ec9692eb82a8e2016d69d95b3209.zip | |
Inline
| -rw-r--r-- | lib/line-index/src/lib.rs | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/lib/line-index/src/lib.rs b/lib/line-index/src/lib.rs index 3c10fbe20c5..eecc1edb135 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()) } } |
