[BugFix] undo function (#143)

* [BugFix] undo function

This commit solves the problem that when the undo function is performed,
for some reason the buffer remained with 1 character.

The FIX consists in correcting the logic of the current implementation
by bringing it to the initial design state; Where the vector of the
buffer had been initialized with an empty entry so that when performing
many times the undo brought you back to an initial condition with
the blank buffer rather than show you the first character.

* [Add] doc-test for set_previous_lines

This test was created specifically to check that the insertion
of characters and words in the Editor.edits vector works properly.

* Replace doctest with unittest

This does not require exposing private functionality as pub

* Fix naming according to #160

Co-authored-by: sholderbach <ho.steve@web.de>
This commit is contained in:
Marco Zanrosso 2021-10-03 23:01:01 +02:00 committed by GitHub
parent f03c036ba4
commit de0f399650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -17,7 +17,7 @@ impl Default for Editor {
cut_buffer: Box::new(get_default_clipboard()),
// Note: Using list-zipper we can reduce these to one field
edits: vec![],
edits: vec![LineBuffer::new()],
index_undo: 2,
}
}
@ -267,3 +267,30 @@ impl Editor {
self.line_buffer.insert_str(&cut_buffer);
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_undo_initial_char() {
let mut editor = Editor::default();
editor.line_buffer().set_buffer(String::from("a"));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab"));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab "));
editor.remember_undo_state(false);
editor.line_buffer().set_buffer(String::from("ab c"));
editor.remember_undo_state(true);
assert_eq!(
vec![
LineBuffer::from(""),
LineBuffer::from("ab "),
LineBuffer::from("ab c")
],
editor.edits
);
}
}

View File

@ -1,4 +1,7 @@
use {std::ops::Range, unicode_segmentation::UnicodeSegmentation};
use {
std::{convert::From, ops::Range},
unicode_segmentation::UnicodeSegmentation,
};
/// Cursor coordinates relative to the Unicode representation of [`LineBuffer`]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -31,6 +34,14 @@ impl Default for LineBuffer {
}
}
impl From<&str> for LineBuffer {
fn from(input: &str) -> Self {
let mut line_buffer = LineBuffer::new();
line_buffer.insert_str(input);
line_buffer
}
}
impl LineBuffer {
pub fn new() -> LineBuffer {
LineBuffer {