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

267 lines
8.5 KiB
Rust
Raw Normal View History

2020-02-06 13:43:46 +00:00
//! `LineIndex` maps flat `TextUnit` offsets into `(Line, Column)`
//! representation.
use std::iter;
use ra_syntax::{TextRange, TextUnit};
2018-11-15 16:34:05 +00:00
use rustc_hash::FxHashMap;
use superslice::Ext;
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 {
pub(crate) newlines: Vec<TextUnit>,
pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
2018-08-10 18:13:39 +00:00
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct LineCol {
/// Zero-based
2018-08-10 18:13:39 +00:00
pub line: u32,
/// Zero-based
2018-11-16 11:02:45 +00:00
pub col_utf16: u32,
2018-11-15 16:34:05 +00:00
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub(crate) struct Utf16Char {
pub(crate) start: TextUnit,
pub(crate) end: TextUnit,
2018-11-15 16:34:05 +00:00
}
impl Utf16Char {
fn len(&self) -> TextUnit {
self.end - self.start
}
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() {
2018-11-15 16:34:05 +00:00
curr_row += TextUnit::of_char(c);
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
let char_len = TextUnit::of_char(c);
2020-03-13 02:29:44 +00:00
if char_len > TextUnit::from_usize(1) {
2019-02-08 11:49:43 +00:00
utf16_chars.push(Utf16Char { start: curr_col, end: curr_col + char_len });
2018-11-15 16:34:05 +00:00
}
curr_col += char_len;
}
// 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
}
2018-08-10 19:23:17 +00:00
pub fn line_col(&self, offset: TextUnit) -> LineCol {
2018-08-10 18:13:39 +00:00
let line = self.newlines.upper_bound(&offset) - 1;
let line_start_offset = self.newlines[line];
let col = offset - line_start_offset;
2018-11-15 16:34:05 +00:00
2019-02-08 11:49:43 +00:00
LineCol { line: line as u32, col_utf16: self.utf8_to_utf16_col(line as u32, col) as u32 }
2018-08-10 19:23:17 +00:00
}
pub fn offset(&self, line_col: LineCol) -> TextUnit {
2019-03-23 07:53:48 +00:00
//FIXME: return Result
2018-11-16 11:02:45 +00:00
let col = self.utf16_to_utf8_col(line_col.line, line_col.col_utf16);
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 = self.newlines.lower_bound(&range.start());
let hi = self.newlines.upper_bound(&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))
.map(|(lo, hi)| TextRange::from_to(lo, hi))
.filter(|it| !it.is_empty())
}
2020-03-13 02:29:44 +00:00
fn utf8_to_utf16_col(&self, line: u32, col: TextUnit) -> usize {
2018-11-15 16:34:05 +00:00
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
2020-03-13 02:29:44 +00:00
let mut correction = 0;
2018-11-15 16:34:05 +00:00
for c in utf16_chars {
if col >= c.end {
2020-03-13 02:29:44 +00:00
correction += c.len().to_usize() - 1;
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-03-13 02:29:44 +00:00
col.to_usize() - correction
} else {
col.to_usize()
2018-11-15 16:34:05 +00:00
}
}
fn utf16_to_utf8_col(&self, line: u32, col: u32) -> TextUnit {
let mut col: TextUnit = col.into();
if let Some(utf16_chars) = self.utf16_lines.get(&line) {
for c in utf16_chars {
if col >= c.start {
col += c.len() - TextUnit::from_usize(1);
} 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;
}
}
}
col
2018-08-10 18:13:39 +00:00
}
}
2018-12-23 13:01:36 +00:00
#[cfg(test)]
mod test_line_index {
use super::*;
2018-08-10 18:13:39 +00:00
2018-12-23 13:01:36 +00:00
#[test]
fn test_line_index() {
let text = "hello\nworld";
let index = LineIndex::new(text);
2019-02-08 11:49:43 +00:00
assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
assert_eq!(index.line_col(1.into()), LineCol { line: 0, col_utf16: 1 });
assert_eq!(index.line_col(5.into()), LineCol { line: 0, col_utf16: 5 });
assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 0 });
assert_eq!(index.line_col(7.into()), LineCol { line: 1, col_utf16: 1 });
assert_eq!(index.line_col(8.into()), LineCol { line: 1, col_utf16: 2 });
assert_eq!(index.line_col(10.into()), LineCol { line: 1, col_utf16: 4 });
assert_eq!(index.line_col(11.into()), LineCol { line: 1, col_utf16: 5 });
assert_eq!(index.line_col(12.into()), LineCol { line: 1, col_utf16: 6 });
2018-12-23 13:01:36 +00:00
let text = "\nhello\nworld";
let index = LineIndex::new(text);
2019-02-08 11:49:43 +00:00
assert_eq!(index.line_col(0.into()), LineCol { line: 0, col_utf16: 0 });
assert_eq!(index.line_col(1.into()), LineCol { line: 1, col_utf16: 0 });
assert_eq!(index.line_col(2.into()), LineCol { line: 1, col_utf16: 1 });
assert_eq!(index.line_col(6.into()), LineCol { line: 1, col_utf16: 5 });
assert_eq!(index.line_col(7.into()), LineCol { line: 2, col_utf16: 0 });
2018-12-23 13:01:36 +00:00
}
2018-11-15 16:34:05 +00:00
#[test]
fn test_char_len() {
assert_eq!('メ'.len_utf8(), 3);
assert_eq!('メ'.len_utf16(), 1);
}
#[test]
fn test_empty_index() {
let col_index = LineIndex::new(
"
const C: char = 'x';
",
);
assert_eq!(col_index.utf16_lines.len(), 0);
}
#[test]
fn test_single_char() {
let col_index = LineIndex::new(
"
const C: char = 'メ';
",
);
assert_eq!(col_index.utf16_lines.len(), 1);
assert_eq!(col_index.utf16_lines[&1].len(), 1);
2019-02-08 11:49:43 +00:00
assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
2018-11-15 16:34:05 +00:00
// UTF-8 to UTF-16, no changes
assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
// UTF-8 to UTF-16
assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
// UTF-16 to UTF-8, no changes
assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from(15));
// UTF-16 to UTF-8
assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from(21));
}
#[test]
fn test_string() {
let col_index = LineIndex::new(
"
const C: char = \"メ メ\";
",
);
assert_eq!(col_index.utf16_lines.len(), 1);
assert_eq!(col_index.utf16_lines[&1].len(), 2);
2019-02-08 11:49:43 +00:00
assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() });
2018-11-15 16:34:05 +00:00
// UTF-8 to UTF-16
assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19);
assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21);
assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
// UTF-16 to UTF-8
assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextUnit::from_usize(15));
assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextUnit::from_usize(20));
assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextUnit::from_usize(23));
assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextUnit::from_usize(15));
}
#[test]
fn test_splitlines() {
fn r(lo: u32, hi: u32) -> TextRange {
TextRange::from_to(lo.into(), hi.into())
}
let text = "a\nbb\nccc\n";
let line_index = LineIndex::new(text);
let actual = line_index.lines(r(0, 9)).collect::<Vec<_>>();
let expected = vec![r(0, 2), r(2, 5), r(5, 9)];
assert_eq!(actual, expected);
let text = "";
let line_index = LineIndex::new(text);
let actual = line_index.lines(r(0, 0)).collect::<Vec<_>>();
let expected = vec![];
assert_eq!(actual, expected);
let text = "\n";
let line_index = LineIndex::new(text);
let actual = line_index.lines(r(0, 1)).collect::<Vec<_>>();
let expected = vec![r(0, 1)];
assert_eq!(actual, expected)
}
2018-08-10 18:13:39 +00:00
}