rust-analyzer/lib/line-index/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

198 lines
6.2 KiB
Rust
Raw Normal View History

2023-05-04 02:18:41 +00:00
//! See [`LineIndex`].
2023-05-04 23:21:42 +00:00
#![deny(missing_debug_implementations, missing_docs, rust_2018_idioms)]
2023-05-04 02:18:41 +00:00
#[cfg(test)]
mod tests;
2023-05-04 23:28:15 +00:00
use nohash_hasher::IntMap;
2023-05-04 23:21:29 +00:00
pub use text_size::{TextRange, TextSize};
2018-08-10 18:13:39 +00:00
2023-05-04 02:18:41 +00:00
/// Maps flat [`TextSize`] offsets into `(line, column)` representation.
2018-11-15 16:34:05 +00:00
#[derive(Clone, Debug, PartialEq, Eq)]
2018-08-10 18:13:39 +00:00
pub struct LineIndex {
/// Offset the beginning of each line, zero-based.
newlines: Box<[TextSize]>,
/// List of non-ASCII characters on each line.
line_wide_chars: IntMap<u32, Box<[WideChar]>>,
2018-08-10 18:13:39 +00:00
}
/// Line/Column information in native, utf8 format.
2018-08-10 18:13:39 +00:00
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LineCol {
2023-05-04 02:18:41 +00:00
/// Zero-based.
2018-08-10 18:13:39 +00:00
pub line: u32,
2023-05-04 02:18:41 +00:00
/// Zero-based UTF-8 offset.
2021-02-12 18:24:10 +00:00
pub col: u32,
2018-11-15 16:34:05 +00:00
}
2023-05-04 02:18:41 +00:00
/// A kind of wide character encoding.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum WideEncoding {
2023-05-04 02:18:41 +00:00
/// UTF-16.
Utf16,
2023-05-04 02:18:41 +00:00
/// UTF-32.
Utf32,
}
/// Line/Column information in legacy encodings.
2023-05-04 23:34:24 +00:00
//
// Deliberately not a generic type and different from `LineCol`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct WideLineCol {
2023-05-04 02:18:41 +00:00
/// Zero-based.
pub line: u32,
2023-05-04 02:18:41 +00:00
/// Zero-based.
pub col: u32,
}
2018-11-15 16:34:05 +00:00
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
2023-05-04 23:20:53 +00:00
struct WideChar {
/// Start offset of a character inside a line, zero-based
2023-05-04 23:20:53 +00:00
start: TextSize,
/// End offset of a character inside a line, zero-based
2023-05-04 23:20:53 +00:00
end: TextSize,
2018-11-15 16:34:05 +00:00
}
impl WideChar {
/// Returns the length in 8-bit UTF-8 code units.
2020-04-24 21:40:41 +00:00
fn len(&self) -> TextSize {
2018-11-15 16:34:05 +00:00
self.end - self.start
}
/// Returns the length in UTF-16 or UTF-32 code units.
fn wide_len(&self, enc: WideEncoding) -> usize {
match enc {
WideEncoding::Utf16 => {
if self.len() == TextSize::from(4) {
2
} else {
1
}
}
WideEncoding::Utf32 => 1,
}
}
2018-08-10 18:13:39 +00:00
}
impl LineIndex {
2023-05-04 02:18:41 +00:00
/// Returns a `LineIndex` for the `text`.
2018-08-10 18:13:39 +00:00
pub fn new(text: &str) -> LineIndex {
2023-05-04 23:28:15 +00:00
let mut line_wide_chars = IntMap::default();
let mut wide_chars = Vec::new();
2018-11-15 16:34:05 +00:00
let mut newlines = Vec::with_capacity(16);
newlines.push(TextSize::from(0));
2023-05-04 23:38:35 +00:00
let mut cur_row = 0.into();
let mut cur_col = 0.into();
2018-11-15 16:34:05 +00:00
let mut line = 0;
2018-08-10 18:13:39 +00:00
for c in text.chars() {
2020-04-24 22:17:50 +00:00
let c_len = TextSize::of(c);
2023-05-04 23:38:35 +00:00
cur_row += c_len;
2018-08-10 18:13:39 +00:00
if c == '\n' {
2023-05-04 23:38:35 +00:00
newlines.push(cur_row);
2018-11-15 16:34:05 +00:00
// Save any utf-16 characters seen in the previous line
if !wide_chars.is_empty() {
line_wide_chars
.insert(line, std::mem::take(&mut wide_chars).into_boxed_slice());
2018-11-15 16:34:05 +00:00
}
// Prepare for processing the next line
2023-05-04 23:38:35 +00:00
cur_col = 0.into();
2018-11-15 16:34:05 +00:00
line += 1;
continue;
2018-08-10 18:13:39 +00:00
}
2018-11-15 16:34:05 +00:00
2020-04-24 22:17:50 +00:00
if !c.is_ascii() {
2023-05-04 23:38:35 +00:00
wide_chars.push(WideChar { start: cur_col, end: cur_col + c_len });
2018-11-15 16:34:05 +00:00
}
2023-05-04 23:38:35 +00:00
cur_col += c_len;
2018-11-15 16:34:05 +00:00
}
// Save any utf-16 characters seen in the last line
if !wide_chars.is_empty() {
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
}
LineIndex { newlines: newlines.into_boxed_slice(), line_wide_chars }
2018-08-10 18:13:39 +00:00
}
2023-05-04 02:18:41 +00:00
/// Transforms the `TextSize` into a `LineCol`.
pub fn line_col(&self, offset: TextSize) -> LineCol {
2021-05-04 11:10:49 +00:00
let line = self.newlines.partition_point(|&it| it <= offset) - 1;
2018-08-10 18:13:39 +00:00
let line_start_offset = self.newlines[line];
let col = offset - line_start_offset;
LineCol { line: line as u32, col: col.into() }
}
2023-05-04 02:18:41 +00:00
/// Transforms the `LineCol` into a `TextSize`.
pub fn offset(&self, line_col: LineCol) -> Option<TextSize> {
self.newlines
.get(line_col.line as usize)
.map(|offset| offset + TextSize::from(line_col.col))
}
2018-11-15 16:34:05 +00:00
2023-05-04 02:18:41 +00:00
/// Transforms the `LineCol` with the given `WideEncoding` into a `WideLineCol`.
pub fn to_wide(&self, enc: WideEncoding, line_col: LineCol) -> WideLineCol {
let col = self.utf8_to_wide_col(enc, line_col.line, line_col.col.into());
WideLineCol { line: line_col.line, col: col as u32 }
2018-08-10 19:23:17 +00:00
}
2023-05-04 02:18:41 +00:00
/// Transforms the `WideLineCol` with the given `WideEncoding` into a `LineCol`.
pub fn to_utf8(&self, enc: WideEncoding, line_col: WideLineCol) -> LineCol {
let col = self.wide_to_utf8_col(enc, line_col.line, line_col.col);
LineCol { line: line_col.line, col: col.into() }
2018-11-15 16:34:05 +00:00
}
2023-05-04 02:18:41 +00:00
/// Returns an iterator over the ranges for the lines.
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
2021-05-04 11:10:49 +00:00
let lo = self.newlines.partition_point(|&it| it < range.start());
let hi = self.newlines.partition_point(|&it| it <= range.end());
2023-05-04 23:21:18 +00:00
let all = std::iter::once(range.start())
.chain(self.newlines[lo..hi].iter().copied())
2023-05-04 23:21:18 +00:00
.chain(std::iter::once(range.end()));
all.clone()
.zip(all.skip(1))
2020-04-24 21:40:41 +00:00
.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 {
2020-04-24 22:17:50 +00:00
let mut res: usize = col.into();
if let Some(wide_chars) = self.line_wide_chars.get(&line) {
for c in wide_chars.iter() {
2020-04-24 22:17:50 +00:00
if c.end <= col {
res -= usize::from(c.len()) - c.wide_len(enc);
2018-11-15 16:34:05 +00:00
} else {
// From here on, all utf16 characters come *after* the character we are mapping,
// so we don't need to take them into account
break;
}
}
}
2020-04-24 22:17:50 +00:00
res
2018-11-15 16:34:05 +00:00
}
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) {
for c in wide_chars.iter() {
if col > u32::from(c.start) {
col += u32::from(c.len()) - c.wide_len(enc) as u32;
2018-11-15 16:34:05 +00:00
} else {
// From here on, all utf16 characters come *after* the character we are mapping,
// so we don't need to take them into account
break;
}
}
}
2020-04-24 22:57:47 +00:00
col.into()
2018-08-10 18:13:39 +00:00
}
}