Skip to content

Commit 9975650

Browse files
committed
Auto merge of rust-lang#16041 - roife:fix-line-index-widechar, r=Veykril
Fix WideChar offsets calculation in `line-index` Fix rust-lang#15981. This PR addresses the issue with the WideChar's offset calculation, ensuring accurate line-specific positions during text analysis in the `lib/line-index` module. ## Changes Made - Corrected the calculation for `WideChar` offsets, ensuring they reflect positions within respective lines. - Added tests to verify the accuracy of `WideChar` offset calculations, and correct existing tests.
2 parents 421a0a4 + a011b6c commit 9975650

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

lib/line-index/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ fn analyze_source_file_generic(
363363
let c = src[i..].chars().next().unwrap();
364364
char_len = c.len_utf8();
365365

366-
let pos = TextSize::from(i as u32) + output_offset;
366+
// The last element of `lines` represents the offset of the start of
367+
// current line. To get the offset inside the line, we subtract it.
368+
let pos = TextSize::from(i as u32) + output_offset
369+
- lines.last().unwrap_or(&TextSize::default());
367370

368371
if char_len > 1 {
369372
assert!((2..=4).contains(&char_len));

lib/line-index/src/tests.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{LineIndex, TextSize, WideChar};
1+
use crate::{LineCol, LineIndex, TextSize, WideChar, WideEncoding, WideLineCol};
22

33
macro_rules! test {
44
(
@@ -102,7 +102,7 @@ test!(
102102
case: multi_byte_with_new_lines,
103103
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
104104
lines: vec![7, 27],
105-
multi_byte_chars: vec![(1, (13, 15)), (2, (29, 31))],
105+
multi_byte_chars: vec![(1, (6, 8)), (2, (2, 4))],
106106
);
107107

108108
test!(
@@ -118,3 +118,27 @@ test!(
118118
lines: vec![16],
119119
multi_byte_chars: vec![],
120120
);
121+
122+
#[test]
123+
fn test_try_line_col() {
124+
let text = "\n\n\n\n\n宽3456";
125+
assert_eq!(&text[5..8], "宽");
126+
assert_eq!(&text[11..12], "6");
127+
let line_index = LineIndex::new(text);
128+
let before_6 = TextSize::from(11);
129+
let line_col = line_index.try_line_col(before_6);
130+
assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
131+
}
132+
133+
#[test]
134+
fn test_to_wide() {
135+
let text = "\n\n\n\n\n宽3456";
136+
assert_eq!(&text[5..8], "宽");
137+
assert_eq!(&text[11..12], "6");
138+
let line_index = LineIndex::new(text);
139+
let before_6 = TextSize::from(11);
140+
let line_col = line_index.try_line_col(before_6);
141+
assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
142+
let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
143+
assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
144+
}

0 commit comments

Comments
 (0)