Skip to content

Commit 0bad292

Browse files
authored
Return \n to end of line in case it's deleted (#245)
* Return \n to end of line in case it's deleted * Add test for changing content past end of line * Fix not appending new line to last line * Run fmt on contents.rs
1 parent a2c555d commit 0bad292

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

vhdl_lang/src/data/contents.rs

+18
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ impl Contents {
115115
}
116116
}
117117

118+
let last_line_index = self.lines.len() - 1;
119+
if (end.line as usize) < last_line_index
120+
&& merged_content.chars().last().unwrap_or('\0') != '\n'
121+
{
122+
merged_content.push('\n');
123+
}
124+
118125
let end_line = std::cmp::min(self.lines.len().saturating_sub(1), end_line);
119126
self.lines
120127
.splice(start_line..=end_line, split_lines(&merged_content))
@@ -536,6 +543,17 @@ mod tests {
536543
assert_eq!(contents.get_line(0).unwrap().to_string(), "helrld");
537544
}
538545

546+
#[test]
547+
fn change_past_end_of_line() {
548+
let mut contents = new("hello\nworld");
549+
assert_eq!(flatten(&contents), "hello\nworld");
550+
contents.change(&Range::new(Position::new(0, 3), Position::new(0, 7)), "");
551+
assert_eq!(flatten(&contents), "hel\nworld");
552+
assert_eq!(contents.num_lines(), 2);
553+
assert_eq!(contents.get_line(0).unwrap().to_string(), "hel\n");
554+
assert_eq!(contents.get_line(1).unwrap().to_string(), "world");
555+
}
556+
539557
#[test]
540558
fn change_to_more_lines() {
541559
let mut contents = new("hello\nworld");

0 commit comments

Comments
 (0)