Skip to content

Commit 9de213c

Browse files
committed
Swap, tweak comments
1 parent fcbe73e commit 9de213c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

lib/line-index/src/lib.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,30 @@ pub struct LineIndex {
9393
impl LineIndex {
9494
/// Returns a `LineIndex` for the `text`.
9595
pub fn new(text: &str) -> LineIndex {
96+
let mut newlines = Vec::with_capacity(16);
9697
let mut line_wide_chars = IntMap::default();
98+
9799
let mut wide_chars = Vec::new();
100+
let mut cur_row = TextSize::from(0);
101+
let mut cur_col = TextSize::from(0);
102+
let mut line = 0;
98103

99-
let mut newlines = Vec::with_capacity(16);
100104
newlines.push(TextSize::from(0));
101105

102-
let mut cur_row = 0.into();
103-
let mut cur_col = 0.into();
104-
let mut line = 0;
105106
for c in text.chars() {
106107
let c_len = TextSize::of(c);
107108
cur_row += c_len;
108109
if c == '\n' {
109110
newlines.push(cur_row);
110111

111-
// Save any utf-16 characters seen in the previous line
112+
// Save any wide characters seen in the previous line
112113
if !wide_chars.is_empty() {
113-
line_wide_chars
114-
.insert(line, std::mem::take(&mut wide_chars).into_boxed_slice());
114+
let cs = std::mem::take(&mut wide_chars).into_boxed_slice();
115+
line_wide_chars.insert(line, cs);
115116
}
116117

117118
// Prepare for processing the next line
118-
cur_col = 0.into();
119+
cur_col = TextSize::from(0);
119120
line += 1;
120121
continue;
121122
}
@@ -127,7 +128,7 @@ impl LineIndex {
127128
cur_col += c_len;
128129
}
129130

130-
// Save any utf-16 characters seen in the last line
131+
// Save any wide characters seen in the last line
131132
if !wide_chars.is_empty() {
132133
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
133134
}
@@ -136,6 +137,10 @@ impl LineIndex {
136137
}
137138

138139
/// Transforms the `TextSize` into a `LineCol`.
140+
///
141+
/// # Panics
142+
///
143+
/// If the offset is invalid.
139144
pub fn line_col(&self, offset: TextSize) -> LineCol {
140145
let line = self.newlines.partition_point(|&it| it <= offset) - 1;
141146
let line_start_offset = self.newlines[line];

0 commit comments

Comments
 (0)