feat(document): implement delete support

This commit is contained in:
Harsh Shandilya 2022-08-11 00:25:40 +05:30
parent ca85741062
commit 279906df37
No known key found for this signature in database
GPG Key ID: 366D7BBAD1031E80
3 changed files with 21 additions and 0 deletions

View File

@ -32,6 +32,14 @@ impl Document {
}
}
pub fn delete(&mut self, at: &Position) {
if at.y >= self.len() {
return;
}
let row = self.rows.get_mut(at.y).unwrap();
row.delete(at.x);
}
pub fn open(filename: &str) -> Result<Self, std::io::Error> {
let contents = fs::read_to_string(filename)?;
let mut rows = vec![];

View File

@ -89,6 +89,7 @@ impl Editor {
self.document.insert(&self.cursor_position, c);
self.move_cursor(Key::Right);
}
Key::Delete => self.document.delete(&self.cursor_position),
Key::Up
| Key::Left
| Key::Right

View File

@ -61,4 +61,16 @@ impl Row {
}
self.update_len();
}
pub fn delete(&mut self, at: usize) {
if at >= self.len() {
return;
} else {
let mut result: String = self.contents[..].graphemes(true).take(at).collect();
let remainder: String = self.contents[..].graphemes(true).skip(at + 1).collect();
result.push_str(&remainder);
self.contents = result;
}
self.update_len();
}
}