mirror of
https://github.com/nushell/reedline.git
synced 2024-08-16 19:30:25 +03:00
[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:
parent
f03c036ba4
commit
de0f399650
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user