1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-22 11:22:46 +03:00

Fix CR LF handling

This commit is contained in:
oxalica 2023-09-08 20:55:26 +08:00
parent bd7e2e2de7
commit 4775e34c30

View File

@ -168,10 +168,10 @@ enum CodeUnitsDiff {
impl LineMap {
fn normalize(mut text: String) -> (String, Self) {
text.retain(|c| c != '\r');
// Must be valid for `TextSize`.
let text_len = u32::try_from(text.len()).expect("Text too long");
text.retain(|c| c != '\r');
let bytes = text.as_bytes();
let line_starts = Some(0)
@ -350,4 +350,15 @@ mod tests {
assert_eq!(map.end_col_for_line(2), 0);
assert_eq!(map.end_col_for_line(3), 3);
}
#[test]
fn cr_lf() {
let (_, map) = LineMap::normalize("hello\r\nworld!".into());
assert_eq!(map.last_line(), 1);
assert_eq!(map.end_col_for_line(0), 5);
assert_eq!(map.end_col_for_line(1), 6);
// `\r` should be stripped. Thus only 5 chars + 1 newline for the first line.
assert_eq!(map.line_col_for_pos(6.into()), (1, 0));
}
}