mirror of
https://github.com/msfjarvis/text-editor.git
synced 2024-06-03 04:18:57 +05:30
feat(document): implement delete support
This commit is contained in:
parent
ca85741062
commit
279906df37
|
@ -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![];
|
||||
|
|
|
@ -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
|
||||
|
|
12
src/row.rs
12
src/row.rs
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user