Skip to content

Commit 209f95e

Browse files
committed
fix: update with end_line out of range
1 parent e48f0a1 commit 209f95e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/text_document.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ impl FullTextDocument {
9292
let num_added_line_offsets = added_line_offsets.len();
9393

9494
let splice_start = start_line as usize + 1;
95+
let splice_end = std::cmp::min(end_line, self.line_count() - 1) as usize;
9596
self.line_offsets
96-
.splice(splice_start..=end_line as usize, added_line_offsets);
97+
.splice(splice_start..=splice_end, added_line_offsets);
9798

9899
let diff =
99100
(text.len() as i32).saturating_sub_unsigned(end_offset - start_offset);
@@ -638,6 +639,34 @@ mod tests {
638639
assert_eq!(text_document.version(), 1)
639640
}
640641

642+
#[test]
643+
fn test_update_new_content_at_end() {
644+
let mut text_document = full_text_document();
645+
let new_text = String::from("bar\nbaz");
646+
647+
let range = Range {
648+
start: Position {
649+
line: 4,
650+
character: 0,
651+
},
652+
end: Position {
653+
line: 5,
654+
character: 0,
655+
},
656+
};
657+
text_document.update(
658+
&[TextDocumentContentChangeEvent {
659+
range: Some(range),
660+
range_length: None,
661+
text: new_text,
662+
}],
663+
1,
664+
);
665+
666+
assert_eq!(&text_document.content, "he\nllo\nworld\r\nfoo\rbar\nbaz");
667+
assert_eq!(text_document.line_offsets, vec![0, 3, 7, 14, 18, 22]);
668+
}
669+
641670
#[test]
642671
#[should_panic(
643672
expected = "Start offset must be less than end offset. 2:0 (offset 7) is not <= 1:0 (offset 3)"

0 commit comments

Comments
 (0)