summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/lib/line-index
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rust-analyzer/lib/line-index')
-rw-r--r--src/tools/rust-analyzer/lib/line-index/Cargo.toml4
-rw-r--r--src/tools/rust-analyzer/lib/line-index/src/lib.rs5
-rw-r--r--src/tools/rust-analyzer/lib/line-index/src/tests.rs28
3 files changed, 32 insertions, 5 deletions
diff --git a/src/tools/rust-analyzer/lib/line-index/Cargo.toml b/src/tools/rust-analyzer/lib/line-index/Cargo.toml
index 6c0d06f47..494a7fa97 100644
--- a/src/tools/rust-analyzer/lib/line-index/Cargo.toml
+++ b/src/tools/rust-analyzer/lib/line-index/Cargo.toml
@@ -1,11 +1,11 @@
[package]
name = "line-index"
-version = "0.1.0"
+version = "0.1.1"
description = "Maps flat `TextSize` offsets to/from `(line, column)` representation."
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-lang/rust-analyzer/tree/master/lib/line-index"
edition = "2021"
[dependencies]
-text-size = "1.1.0"
+text-size = "1.1.1"
nohash-hasher = "0.2.0"
diff --git a/src/tools/rust-analyzer/lib/line-index/src/lib.rs b/src/tools/rust-analyzer/lib/line-index/src/lib.rs
index 03371c9c8..58f266d67 100644
--- a/src/tools/rust-analyzer/lib/line-index/src/lib.rs
+++ b/src/tools/rust-analyzer/lib/line-index/src/lib.rs
@@ -363,7 +363,10 @@ fn analyze_source_file_generic(
let c = src[i..].chars().next().unwrap();
char_len = c.len_utf8();
- let pos = TextSize::from(i as u32) + output_offset;
+ // The last element of `lines` represents the offset of the start of
+ // current line. To get the offset inside the line, we subtract it.
+ let pos = TextSize::from(i as u32) + output_offset
+ - lines.last().unwrap_or(&TextSize::default());
if char_len > 1 {
assert!((2..=4).contains(&char_len));
diff --git a/src/tools/rust-analyzer/lib/line-index/src/tests.rs b/src/tools/rust-analyzer/lib/line-index/src/tests.rs
index 8f3762d19..981008e34 100644
--- a/src/tools/rust-analyzer/lib/line-index/src/tests.rs
+++ b/src/tools/rust-analyzer/lib/line-index/src/tests.rs
@@ -1,4 +1,4 @@
-use crate::{LineIndex, TextSize, WideChar};
+use crate::{LineCol, LineIndex, TextSize, WideChar, WideEncoding, WideLineCol};
macro_rules! test {
(
@@ -102,7 +102,7 @@ test!(
case: multi_byte_with_new_lines,
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
lines: vec![7, 27],
- multi_byte_chars: vec![(1, (13, 15)), (2, (29, 31))],
+ multi_byte_chars: vec![(1, (6, 8)), (2, (2, 4))],
);
test!(
@@ -118,3 +118,27 @@ test!(
lines: vec![16],
multi_byte_chars: vec![],
);
+
+#[test]
+fn test_try_line_col() {
+ let text = "\n\n\n\n\n宽3456";
+ assert_eq!(&text[5..8], "宽");
+ assert_eq!(&text[11..12], "6");
+ let line_index = LineIndex::new(text);
+ let before_6 = TextSize::from(11);
+ let line_col = line_index.try_line_col(before_6);
+ assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
+}
+
+#[test]
+fn test_to_wide() {
+ let text = "\n\n\n\n\n宽3456";
+ assert_eq!(&text[5..8], "宽");
+ assert_eq!(&text[11..12], "6");
+ let line_index = LineIndex::new(text);
+ let before_6 = TextSize::from(11);
+ let line_col = line_index.try_line_col(before_6);
+ assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
+ let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
+ assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
+}