rust-analyzer/crates/ide_db/src/line_index.rs

153 lines
4.7 KiB
Rust
Raw Normal View History

2020-04-24 21:40:41 +00:00
//! `LineIndex` maps flat `TextSize` offsets into `(Line, Column)`
2020-02-06 13:43:46 +00:00
//! representation.
2020-04-24 22:57:47 +00:00
use std::iter;
2018-11-15 16:34:05 +00:00
use rustc_hash::FxHashMap;
use stdx::partition_point;
2020-08-12 16:26:51 +00:00
use syntax::{TextRange, TextSize};
2018-08-10 18:13:39 +00:00
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 the beginning of each line, zero-based
2020-04-24 21:40:41 +00:00
pub(crate) newlines: Vec<TextSize>,
/// List of non-ASCII characters on each line
pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
2018-08-10 18:13:39 +00:00
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
2021-02-12 18:24:10 +00:00
pub struct LineColUtf16 {
/// Zero-based
2018-08-10 18:13:39 +00:00
pub line: u32,
/// Zero-based
2021-02-12 18:24:10 +00:00
pub col: u32,
2018-11-15 16:34:05 +00:00
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct Utf16Char {
/// Start offset of a character inside a line, zero-based
2020-04-24 21:40:41 +00:00
pub(crate) start: TextSize,
/// End offset of a character inside a line, zero-based
2020-04-24 21:40:41 +00:00
pub(crate) end: TextSize,
2018-11-15 16:34:05 +00:00
}
impl Utf16Char {
/// 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 16-bit UTF-16 code units.
fn len_utf16(&self) -> usize {
if self.len() == TextSize::from(4) {
2
} else {
1
}
}
2018-08-10 18:13:39 +00:00
}
impl LineIndex {
pub fn new(text: &str) -> LineIndex {
2018-11-15 16:34:05 +00:00
let mut utf16_lines = FxHashMap::default();
let mut utf16_chars = Vec::new();
2018-08-10 18:13:39 +00:00
let mut newlines = vec![0.into()];
2018-11-15 16:34:05 +00:00
let mut curr_row = 0.into();
let mut curr_col = 0.into();
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);
curr_row += c_len;
2018-08-10 18:13:39 +00:00
if c == '\n' {
2018-11-15 16:34:05 +00:00
newlines.push(curr_row);
// Save any utf-16 characters seen in the previous line
2019-06-04 06:28:22 +00:00
if !utf16_chars.is_empty() {
2018-11-15 16:34:05 +00:00
utf16_lines.insert(line, utf16_chars);
utf16_chars = Vec::new();
}
// Prepare for processing the next line
curr_col = 0.into();
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() {
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + c_len });
2018-11-15 16:34:05 +00:00
}
2020-04-24 22:17:50 +00:00
curr_col += c_len;
2018-11-15 16:34:05 +00:00
}
// Save any utf-16 characters seen in the last line
2019-06-04 06:28:22 +00:00
if !utf16_chars.is_empty() {
utf16_lines.insert(line, utf16_chars);
}
2019-02-08 11:49:43 +00:00
LineIndex { newlines, utf16_lines }
2018-08-10 18:13:39 +00:00
}
2021-02-12 18:24:10 +00:00
pub fn line_col(&self, offset: TextSize) -> LineColUtf16 {
let line = partition_point(&self.newlines, |&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;
2018-11-15 16:34:05 +00:00
2021-02-12 18:24:10 +00:00
LineColUtf16 { line: line as u32, col: self.utf8_to_utf16_col(line as u32, col) as u32 }
2018-08-10 19:23:17 +00:00
}
2021-02-12 18:24:10 +00:00
pub fn offset(&self, line_col: LineColUtf16) -> TextSize {
2019-03-23 07:53:48 +00:00
//FIXME: return Result
2021-02-12 18:24:10 +00:00
let col = self.utf16_to_utf8_col(line_col.line, line_col.col);
2018-11-15 16:34:05 +00:00
self.newlines[line_col.line as usize] + col
}
pub fn lines(&self, range: TextRange) -> impl Iterator<Item = TextRange> + '_ {
let lo = partition_point(&self.newlines, |&it| it < range.start());
let hi = partition_point(&self.newlines, |&it| it <= range.end());
let all = iter::once(range.start())
.chain(self.newlines[lo..hi].iter().copied())
.chain(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())
}
2020-04-24 21:40:41 +00:00
fn utf8_to_utf16_col(&self, line: u32, col: TextSize) -> usize {
2020-04-24 22:17:50 +00:00
let mut res: usize = col.into();
2018-11-15 16:34:05 +00:00
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
for c in utf16_chars {
2020-04-24 22:17:50 +00:00
if c.end <= col {
res -= usize::from(c.len()) - c.len_utf16();
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
}
2020-04-24 22:57:47 +00:00
fn utf16_to_utf8_col(&self, line: u32, mut col: u32) -> TextSize {
2018-11-15 16:34:05 +00:00
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
for c in utf16_chars {
if col > u32::from(c.start) {
col += u32::from(c.len()) - c.len_utf16() 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
}
}
2018-12-23 13:01:36 +00:00
#[cfg(test)]
mod tests;