зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-07 16:05:50 +03:00
fix start position of EditKind::DeleteChunk is wrong when it is used in undo
this needs some refactoring later
Этот коммит содержится в:
@@ -8,8 +8,8 @@ pub enum EditKind {
|
|||||||
DeleteNewline(usize),
|
DeleteNewline(usize),
|
||||||
InsertStr(String, usize),
|
InsertStr(String, usize),
|
||||||
DeleteStr(String, usize),
|
DeleteStr(String, usize),
|
||||||
InsertChunk(Vec<String>, usize),
|
InsertChunk(Vec<String>, usize, usize),
|
||||||
DeleteChunk(Vec<String>, usize),
|
DeleteChunk(Vec<String>, usize, usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditKind {
|
impl EditKind {
|
||||||
@@ -40,8 +40,9 @@ impl EditKind {
|
|||||||
let end = *i + s.len();
|
let end = *i + s.len();
|
||||||
lines[row].replace_range(*i..end, "");
|
lines[row].replace_range(*i..end, "");
|
||||||
}
|
}
|
||||||
EditKind::InsertChunk(c, i) => {
|
EditKind::InsertChunk(c, row, i) => {
|
||||||
debug_assert!(c.len() > 1, "Chunk size must be > 1: {:?}", c);
|
debug_assert!(c.len() > 1, "Chunk size must be > 1: {:?}", c);
|
||||||
|
let row = *row;
|
||||||
|
|
||||||
// Handle first line of chunk
|
// Handle first line of chunk
|
||||||
let first_line = &mut lines[row];
|
let first_line = &mut lines[row];
|
||||||
@@ -55,8 +56,9 @@ impl EditKind {
|
|||||||
// Handle last line of chunk
|
// Handle last line of chunk
|
||||||
lines.splice(row + 1..row + 1, c[1..c.len() - 1].iter().cloned());
|
lines.splice(row + 1..row + 1, c[1..c.len() - 1].iter().cloned());
|
||||||
}
|
}
|
||||||
EditKind::DeleteChunk(c, i) => {
|
EditKind::DeleteChunk(c, row, i) => {
|
||||||
debug_assert!(c.len() > 1, "Chunk size must be > 1: {:?}", c);
|
debug_assert!(c.len() > 1, "Chunk size must be > 1: {:?}", c);
|
||||||
|
let row = *row;
|
||||||
|
|
||||||
lines[row].truncate(*i);
|
lines[row].truncate(*i);
|
||||||
let mut last_line = lines.drain(row + 1..row + c.len()).last().unwrap();
|
let mut last_line = lines.drain(row + 1..row + c.len()).last().unwrap();
|
||||||
@@ -75,8 +77,8 @@ impl EditKind {
|
|||||||
DeleteNewline(i) => InsertNewline(i),
|
DeleteNewline(i) => InsertNewline(i),
|
||||||
InsertStr(s, i) => DeleteStr(s, i),
|
InsertStr(s, i) => DeleteStr(s, i),
|
||||||
DeleteStr(s, i) => InsertStr(s, i),
|
DeleteStr(s, i) => InsertStr(s, i),
|
||||||
InsertChunk(c, i) => DeleteChunk(c, i),
|
InsertChunk(c, r, i) => DeleteChunk(c, r, i),
|
||||||
DeleteChunk(c, i) => InsertChunk(c, i),
|
DeleteChunk(c, r, i) => InsertChunk(c, r, i),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -530,7 +532,7 @@ mod tests {
|
|||||||
let mut lines: Vec<_> = before.iter().map(|s| s.to_string()).collect();
|
let mut lines: Vec<_> = before.iter().map(|s| s.to_string()).collect();
|
||||||
let chunk: Vec<_> = input.iter().map(|s| s.to_string()).collect();
|
let chunk: Vec<_> = input.iter().map(|s| s.to_string()).collect();
|
||||||
|
|
||||||
let edit = EditKind::InsertChunk(chunk.clone(), offset);
|
let edit = EditKind::InsertChunk(chunk.clone(), row, offset);
|
||||||
edit.apply(row, &mut lines);
|
edit.apply(row, &mut lines);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&lines, expected,
|
&lines, expected,
|
||||||
@@ -538,7 +540,7 @@ mod tests {
|
|||||||
before, pos, input,
|
before, pos, input,
|
||||||
);
|
);
|
||||||
|
|
||||||
let edit = EditKind::DeleteChunk(chunk, offset);
|
let edit = EditKind::DeleteChunk(chunk, row, offset);
|
||||||
edit.apply(row, &mut lines);
|
edit.apply(row, &mut lines);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
&lines, &before,
|
&lines, &before,
|
||||||
|
|||||||
@@ -676,7 +676,7 @@ impl<'a> TextArea<'a> {
|
|||||||
chunk[chunk.len() - 1].chars().count(),
|
chunk[chunk.len() - 1].chars().count(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let edit = EditKind::InsertChunk(chunk, i);
|
let edit = EditKind::InsertChunk(chunk, row, i);
|
||||||
edit.apply(row, &mut self.lines);
|
edit.apply(row, &mut self.lines);
|
||||||
|
|
||||||
self.push_history(edit, (row, col));
|
self.push_history(edit, (row, col));
|
||||||
@@ -794,7 +794,7 @@ impl<'a> TextArea<'a> {
|
|||||||
let edit = if deleted.len() == 1 {
|
let edit = if deleted.len() == 1 {
|
||||||
EditKind::DeleteStr(deleted.remove(0), first_start)
|
EditKind::DeleteStr(deleted.remove(0), first_start)
|
||||||
} else {
|
} else {
|
||||||
EditKind::DeleteChunk(deleted, first_start)
|
EditKind::DeleteChunk(deleted, row, first_start)
|
||||||
};
|
};
|
||||||
self.push_history(edit, self.cursor);
|
self.push_history(edit, self.cursor);
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user