зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 08:25:51 +03:00
remove duplicate in undo/redo implementation
Этот коммит содержится в:
96
src/edit.rs
96
src/edit.rs
@@ -6,6 +6,50 @@ pub enum EditKind {
|
|||||||
Insert(String, usize),
|
Insert(String, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl EditKind {
|
||||||
|
fn redo(&self, row: usize, lines: &mut Vec<String>) {
|
||||||
|
match self {
|
||||||
|
EditKind::InsertChar(c, i) => {
|
||||||
|
lines[row].insert(*i, *c);
|
||||||
|
}
|
||||||
|
EditKind::DeleteChar(_, i) => {
|
||||||
|
lines[row].remove(*i);
|
||||||
|
}
|
||||||
|
EditKind::InsertNewline(i) => {
|
||||||
|
let line = &mut lines[row];
|
||||||
|
let next_line = line[*i..].to_string();
|
||||||
|
line.truncate(*i);
|
||||||
|
line.push(' ');
|
||||||
|
lines.insert(row + 1, next_line);
|
||||||
|
}
|
||||||
|
EditKind::DeleteNewline(_) => {
|
||||||
|
if row > 0 {
|
||||||
|
let line = lines.remove(row);
|
||||||
|
let prev_line = &mut lines[row - 1];
|
||||||
|
prev_line.pop(); // Remove trailing space
|
||||||
|
prev_line.push_str(&line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EditKind::Insert(s, i) => {
|
||||||
|
lines[row].insert_str(*i, s.as_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn undo(&self, row: usize, lines: &mut Vec<String>) {
|
||||||
|
use EditKind::*;
|
||||||
|
match self {
|
||||||
|
InsertChar(c, i) => DeleteChar(*c, *i).redo(row, lines),
|
||||||
|
DeleteChar(c, i) => InsertChar(*c, *i).redo(row, lines),
|
||||||
|
InsertNewline(i) => DeleteNewline(*i).redo(row, lines),
|
||||||
|
DeleteNewline(i) => InsertNewline(*i).redo(row, lines),
|
||||||
|
Insert(s, i) => {
|
||||||
|
lines[row].replace_range(*i..s.len(), "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Edit {
|
pub struct Edit {
|
||||||
kind: EditKind,
|
kind: EditKind,
|
||||||
cursor_before: (usize, usize),
|
cursor_before: (usize, usize),
|
||||||
@@ -27,60 +71,12 @@ impl Edit {
|
|||||||
|
|
||||||
pub fn redo(&self, lines: &mut Vec<String>) {
|
pub fn redo(&self, lines: &mut Vec<String>) {
|
||||||
let (row, _) = self.cursor_before;
|
let (row, _) = self.cursor_before;
|
||||||
match self.kind {
|
self.kind.redo(row, lines)
|
||||||
EditKind::InsertChar(c, i) => {
|
|
||||||
lines[row].insert(i, c);
|
|
||||||
}
|
|
||||||
EditKind::DeleteChar(_, i) => {
|
|
||||||
lines[row].remove(i);
|
|
||||||
}
|
|
||||||
EditKind::InsertNewline(i) => {
|
|
||||||
let line = &mut lines[row];
|
|
||||||
let next_line = line[i..].to_string();
|
|
||||||
line.truncate(i);
|
|
||||||
line.push(' ');
|
|
||||||
lines.insert(row + 1, next_line);
|
|
||||||
}
|
|
||||||
EditKind::DeleteNewline(_) => {
|
|
||||||
if row > 0 {
|
|
||||||
let line = lines.remove(row);
|
|
||||||
let prev_line = &mut lines[row - 1];
|
|
||||||
prev_line.pop(); // Remove trailing space
|
|
||||||
prev_line.push_str(&line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EditKind::Insert(ref s, i) => {
|
|
||||||
lines[row].insert_str(i, s.as_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn undo(&self, lines: &mut Vec<String>) {
|
pub fn undo(&self, lines: &mut Vec<String>) {
|
||||||
let (row, _) = self.cursor_after;
|
let (row, _) = self.cursor_after;
|
||||||
match self.kind {
|
self.kind.undo(row, lines)
|
||||||
EditKind::InsertChar(_, i) => {
|
|
||||||
lines[row].remove(i);
|
|
||||||
}
|
|
||||||
EditKind::DeleteChar(c, i) => {
|
|
||||||
lines[row].insert(i, c);
|
|
||||||
}
|
|
||||||
EditKind::InsertNewline(_) => {
|
|
||||||
let line = lines.remove(row);
|
|
||||||
let prev_line = &mut lines[row - 1];
|
|
||||||
prev_line.pop(); // Remove trailing space
|
|
||||||
prev_line.push_str(&line);
|
|
||||||
}
|
|
||||||
EditKind::DeleteNewline(i) => {
|
|
||||||
let line = &mut lines[row];
|
|
||||||
let next_line = line[i..].to_string();
|
|
||||||
line.truncate(i);
|
|
||||||
line.push(' ');
|
|
||||||
lines.insert(row + 1, next_line);
|
|
||||||
}
|
|
||||||
EditKind::Insert(ref s, i) => {
|
|
||||||
lines[row].replace_range(i..s.len(), "");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cursor_before(&self) -> (usize, usize) {
|
pub fn cursor_before(&self) -> (usize, usize) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user