5 let text = "hello\nworld";
18 let index = LineIndex::new(text);
19 for &(offset, line, col) in &table {
20 assert_eq!(index.line_col(offset.into()), LineCol { line, col });
23 let text = "\nhello\nworld";
24 let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)];
25 let index = LineIndex::new(text);
26 for &(offset, line, col) in &table {
27 assert_eq!(index.line_col(offset.into()), LineCol { line, col });
33 assert_eq!('メ'.len_utf8(), 3);
34 assert_eq!('メ'.len_utf16(), 1);
38 fn test_empty_index() {
39 let col_index = LineIndex::new(
44 assert_eq!(col_index.utf16_lines.len(), 0);
48 fn test_single_char() {
49 let col_index = LineIndex::new(
55 assert_eq!(col_index.utf16_lines.len(), 1);
56 assert_eq!(col_index.utf16_lines[&1].len(), 1);
57 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
59 // UTF-8 to UTF-16, no changes
60 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
63 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
65 // UTF-16 to UTF-8, no changes
66 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
69 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
71 let col_index = LineIndex::new("a𐐏b");
72 assert_eq!(col_index.utf16_to_utf8_col(0, 3), TextSize::from(5));
77 let col_index = LineIndex::new(
79 const C: char = \"メ メ\";
83 assert_eq!(col_index.utf16_lines.len(), 1);
84 assert_eq!(col_index.utf16_lines[&1].len(), 2);
85 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
86 assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() });
89 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
91 assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19);
92 assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21);
94 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
97 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
99 // メ UTF-8: 0xE3 0x83 0xA1, UTF-16: 0x30E1
100 assert_eq!(col_index.utf16_to_utf8_col(1, 17), TextSize::from(17)); // first メ at 17..20
101 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); // space
102 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); // second メ at 21..24
104 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15));
108 fn test_splitlines() {
109 fn r(lo: u32, hi: u32) -> TextRange {
110 TextRange::new(lo.into(), hi.into())
113 let text = "a\nbb\nccc\n";
114 let line_index = LineIndex::new(text);
116 let actual = line_index.lines(r(0, 9)).collect::<Vec<_>>();
117 let expected = vec![r(0, 2), r(2, 5), r(5, 9)];
118 assert_eq!(actual, expected);
121 let line_index = LineIndex::new(text);
123 let actual = line_index.lines(r(0, 0)).collect::<Vec<_>>();
124 let expected = vec![];
125 assert_eq!(actual, expected);
128 let line_index = LineIndex::new(text);
130 let actual = line_index.lines(r(0, 1)).collect::<Vec<_>>();
131 let expected = vec![r(0, 1)];
132 assert_eq!(actual, expected)