Commit 31634f14 authored by AdminXVII's avatar AdminXVII

Remove display after deletion

parent d23320eb
Pipeline #4519 passed with stage
in 1 minute and 56 seconds
......@@ -427,7 +427,11 @@ impl<'a, W: io::Write> Editor<'a, W> {
cur_buf_mut!(self).truncate(0);
self.cursor = 0;
}
_ => self.delete_word_before_cursor(false)?,
_ => {
self.delete_word_before_cursor(false);
self.no_newline = true;
self.display()?;
}
}
self.insert_str_after_cursor(&completions[i])?;
......@@ -435,8 +439,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
}
if self.show_completions_hint.is_some() {
self.no_newline = true;
self.display()?;
return Ok(());
return self.display();
}
let (word, completions) = {
......@@ -460,7 +463,9 @@ impl<'a, W: io::Write> Editor<'a, W> {
Ok(())
} else if completions.len() == 1 {
self.show_completions_hint = None;
self.delete_word_before_cursor(false)?;
self.delete_word_before_cursor(false);
self.no_newline = true;
self.display()?;
self.insert_str_after_cursor(completions[0].as_ref())
} else {
let common_prefix = util::find_longest_common_prefix(
......@@ -474,16 +479,16 @@ impl<'a, W: io::Write> Editor<'a, W> {
let s = p.iter().cloned().collect::<String>();
if s.len() > word.len() && s.starts_with(&word[..]) {
self.delete_word_before_cursor(false)?;
self.delete_word_before_cursor(false);
self.no_newline = true;
self.display()?;
return self.insert_str_after_cursor(s.as_ref());
}
}
self.show_completions_hint = Some((completions, None));
self.no_newline = true;
self.display()?;
Ok(())
self.display()
}
}
......@@ -515,20 +520,15 @@ impl<'a, W: io::Write> Editor<'a, W> {
/// this method ignores that space until it finds a word.
/// If `ignore_space_before_cursor` is false and there is space directly before the cursor,
/// nothing is deleted.
pub fn delete_word_before_cursor(
&mut self,
ignore_space_before_cursor: bool,
) -> io::Result<()> {
pub fn delete_word_before_cursor(&mut self, ignore_space_before_cursor: bool) {
if let Some((start, _)) = self.get_word_before_cursor(ignore_space_before_cursor) {
let moved = cur_buf_mut!(self).remove(start, self.cursor);
self.cursor -= moved;
}
self.no_newline = true;
self.display()
}
/// Clears the screen then prints the prompt and current buffer.
pub fn clear(&mut self) -> io::Result<()> {
pub fn clear(&mut self) {
write!(
&mut self.context.buf,
"{}{}",
......@@ -537,9 +537,7 @@ impl<'a, W: io::Write> Editor<'a, W> {
);
self.term_cursor_line = 1;
self.no_newline = true;
self.clear_search();
self.display()
}
/// Move up (backwards) in history.
......@@ -658,51 +656,38 @@ impl<'a, W: io::Write> Editor<'a, W> {
/// Deletes the character directly before the cursor, moving the cursor to the left.
/// If the cursor is at the start of the line, nothing happens.
pub fn delete_before_cursor(&mut self) -> io::Result<()> {
pub fn delete_before_cursor(&mut self) {
if self.cursor > 0 {
let buf = cur_buf_mut!(self);
buf.remove(self.cursor - 1, self.cursor);
self.cursor -= 1;
}
self.no_newline = true;
self.display()
}
/// Deletes the character directly after the cursor. The cursor does not move.
/// If the cursor is at the end of the line, nothing happens.
pub fn delete_after_cursor(&mut self) -> io::Result<()> {
{
let buf = cur_buf_mut!(self);
pub fn delete_after_cursor(&mut self) {
let buf = cur_buf_mut!(self);
if self.cursor < buf.num_chars() {
buf.remove(self.cursor, self.cursor + 1);
}
if self.cursor < buf.num_chars() {
buf.remove(self.cursor, self.cursor + 1);
}
self.no_newline = true;
self.display()
}
/// Deletes every character preceding the cursor until the beginning of the line.
pub fn delete_all_before_cursor(&mut self) -> io::Result<()> {
pub fn delete_all_before_cursor(&mut self) {
cur_buf_mut!(self).remove(0, self.cursor);
self.cursor = 0;
self.no_newline = true;
self.display()
}
/// Deletes every character after the cursor until the end of the line.
pub fn delete_all_after_cursor(&mut self) -> io::Result<()> {
{
let buf = cur_buf_mut!(self);
buf.truncate(self.cursor);
}
self.no_newline = true;
self.display()
pub fn delete_all_after_cursor(&mut self) {
let buf = cur_buf_mut!(self);
buf.truncate(self.cursor);
}
/// Deletes every character from the cursor until the given position.
pub fn delete_until(&mut self, position: usize) -> io::Result<()> {
pub fn delete_until(&mut self, position: usize) {
{
let buf = cur_buf_mut!(self);
buf.remove(
......@@ -711,22 +696,16 @@ impl<'a, W: io::Write> Editor<'a, W> {
);
self.cursor = cmp::min(self.cursor, position);
}
self.no_newline = true;
self.display()
}
/// Deletes every character from the cursor until the given position, inclusive.
pub fn delete_until_inclusive(&mut self, position: usize) -> io::Result<()> {
{
let buf = cur_buf_mut!(self);
buf.remove(
cmp::min(self.cursor, position),
cmp::max(self.cursor + 1, position + 1),
);
self.cursor = cmp::min(self.cursor, position);
}
self.no_newline = true;
self.display()
pub fn delete_until_inclusive(&mut self, position: usize) {
let buf = cur_buf_mut!(self);
buf.remove(
cmp::min(self.cursor, position),
cmp::max(self.cursor + 1, position + 1),
);
self.cursor = cmp::min(self.cursor, position);
}
/// Moves the cursor to the left by `count` characters.
......@@ -796,15 +775,13 @@ impl<'a, W: io::Write> Editor<'a, W> {
/// Accept autosuggestion and copy its content into current buffer
pub fn accept_autosuggestion(&mut self) -> io::Result<()> {
if self.show_autosuggestions {
{
let autosuggestion = self.autosuggestion.clone();
let search = self.is_search();
let buf = self.current_buffer_mut();
match autosuggestion {
Some(ref x) if search => buf.copy_buffer(x),
Some(ref x) => buf.insert_from_buffer(x),
None => (),
}
let autosuggestion = self.autosuggestion.clone();
let search = self.is_search();
let buf = self.current_buffer_mut();
match autosuggestion {
Some(ref x) if search => buf.copy_buffer(x),
Some(ref x) => buf.insert_from_buffer(x),
None => (),
}
}
self.clear_search();
......@@ -1110,8 +1087,7 @@ mod tests {
let mut ed = Editor::new(out, "prompt".to_owned(), None, &mut context).unwrap();
ed.insert_str_after_cursor("delete all of this").unwrap();
ed.move_cursor_to_start_of_line();
ed.no_newline = true;
ed.delete_all_after_cursor().unwrap();
ed.delete_all_after_cursor();
ed.undo().unwrap();
assert_eq!(String::from(ed), "delete all of this");
}
......@@ -1125,7 +1101,6 @@ mod tests {
assert_eq!(ed.cursor, 3);
ed.move_cursor_left(1);
ed.no_newline = true;
assert_eq!(ed.cursor, 2);
ed.insert_after_cursor('f').unwrap();
......@@ -1142,9 +1117,7 @@ mod tests {
assert_eq!(ed.cursor, 5);
ed.move_cursor_left(2);
ed.no_newline = true;
ed.move_cursor_right(1);
ed.no_newline = true;
assert_eq!(ed.cursor, 4);
}
......@@ -1156,7 +1129,8 @@ mod tests {
ed.insert_str_after_cursor("right").unwrap();
assert_eq!(ed.cursor, 5);
ed.delete_until(0).unwrap();
ed.delete_until(0);
ed.no_newline = true;
assert_eq!(ed.cursor, 0);
assert_eq!(String::from(ed), "");
}
......@@ -1169,7 +1143,8 @@ mod tests {
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 0;
ed.delete_until(5).unwrap();
ed.delete_until(5);
ed.no_newline = true;
assert_eq!(ed.cursor, 0);
assert_eq!(String::from(ed), "");
}
......@@ -1182,7 +1157,8 @@ mod tests {
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 4;
ed.delete_until(1).unwrap();
ed.delete_until(1);
ed.no_newline = true;
assert_eq!(ed.cursor, 1);
assert_eq!(String::from(ed), "rt");
}
......@@ -1195,7 +1171,8 @@ mod tests {
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 4;
ed.delete_until_inclusive(1).unwrap();
ed.delete_until_inclusive(1);
ed.no_newline = true;
assert_eq!(ed.cursor, 1);
assert_eq!(String::from(ed), "r");
}
......
......@@ -24,7 +24,12 @@ impl Emacs {
fn handle_ctrl_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) -> io::Result<()> {
match c {
'l' => ed.clear(),
'l' => {
ed.clear();
ed.no_newline = true;
ed.display()
}
'a' => {
ed.move_cursor_to_start_of_line();
......@@ -48,7 +53,12 @@ impl Emacs {
ed.no_newline = true;
ed.display()
}
'd' => ed.delete_after_cursor(),
'd' => {
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()
}
'p' => {
ed.move_up();
ed.display()
......@@ -57,9 +67,21 @@ impl Emacs {
ed.move_down();
ed.display()
}
'u' => ed.delete_all_before_cursor(),
'k' => ed.delete_all_after_cursor(),
'w' => ed.delete_word_before_cursor(true),
'u' => {
ed.delete_all_before_cursor();
ed.no_newline = true;
ed.display()
}
'k' => {
ed.delete_all_after_cursor();
ed.no_newline = true;
ed.display()
}
'w' => {
ed.delete_word_before_cursor(true);
ed.no_newline = true;
ed.display()
}
'x' => {
ed.undo()?;
Ok(())
......@@ -80,16 +102,18 @@ impl Emacs {
ed.no_newline = true;
ed.display()
}
'\x7F' => ed.delete_word_before_cursor(true),
'\x7F' => {
ed.delete_word_before_cursor(true);
ed.no_newline = true;
ed.display()
}
'f' => {
emacs_move_word(ed, EmacsMoveDir::Right);
ed.no_newline = true;
ed.display()
}
'b' => {
emacs_move_word(ed, EmacsMoveDir::Left);
ed.no_newline = true;
ed.display()
}
......@@ -121,7 +145,9 @@ impl Emacs {
if self.last_arg_fetch_index.is_some() {
let buffer_len = ed.current_buffer().num_chars();
if let Some(last_arg_len) = ed.current_buffer().last_arg().map(|x| x.len()) {
ed.delete_until(buffer_len - last_arg_len)?;
ed.delete_until(buffer_len - last_arg_len);
ed.no_newline = true;
ed.display()?;
}
}
......@@ -185,8 +211,18 @@ impl KeyMap for Emacs {
ed.no_newline = true;
ed.display()
}
Key::Backspace => ed.delete_before_cursor(),
Key::Delete => ed.delete_after_cursor(),
Key::Backspace => {
ed.delete_before_cursor();
ed.no_newline = true;
ed.display()
}
Key::Delete => {
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()
}
Key::Null => Ok(()),
_ => Ok(()),
}
......
......@@ -401,9 +401,11 @@ impl Vi {
Delete(start_pos) => {
// perform the delete operation
match move_type {
Exclusive => ed.delete_until(start_pos)?,
Inclusive => ed.delete_until_inclusive(start_pos)?,
Exclusive => ed.delete_until(start_pos),
Inclusive => ed.delete_until_inclusive(start_pos),
}
ed.no_newline = true;
ed.display()?;
// update the last state
mem::swap(&mut self.last_command, &mut self.current_command);
......@@ -499,7 +501,12 @@ impl Vi {
ed: &mut Editor<'a, W>,
) -> io::Result<()> {
match key {
Key::Ctrl('l') => ed.clear(),
Key::Ctrl('l') => {
ed.clear();
ed.no_newline = true;
ed.display()
}
Key::Left => {
ed.move_cursor_left(1);
ed.no_newline = true;
......@@ -528,8 +535,18 @@ impl Vi {
ed.no_newline = true;
ed.display()
}
Key::Backspace => ed.delete_before_cursor(),
Key::Delete => ed.delete_after_cursor(),
Key::Backspace => {
ed.delete_before_cursor();
ed.no_newline = true;
ed.display()
}
Key::Delete => {
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()
}
Key::Null => Ok(()),
_ => Ok(()),
}
......@@ -658,7 +675,9 @@ impl Vi {
self.last_insert = Some(key);
self.set_mode(Insert, ed);
let pos = ed.cursor() + self.move_count_right(ed);
ed.delete_until(pos)?;
ed.delete_until(pos);
ed.no_newline = true;
ed.display()?;
self.last_count = self.count;
self.count = 0;
Ok(())
......@@ -695,7 +714,9 @@ impl Vi {
self.count = 0;
self.last_count = 0;
ed.delete_all_after_cursor()
ed.delete_all_after_cursor();
ed.no_newline = true;
ed.display()
}
Key::Char('C') => {
// update the last command state
......@@ -706,7 +727,9 @@ impl Vi {
self.last_count = 0;
self.set_mode_preserve_last(Insert, ed);
ed.delete_all_after_cursor()
ed.delete_all_after_cursor();
ed.no_newline = true;
ed.display()
}
Key::Char('.') => {
// repeat the last command
......@@ -823,7 +846,9 @@ impl Vi {
self.last_count = self.count;
let pos = ed.cursor() + self.move_count_right(ed);
ed.delete_until(pos)?;
ed.delete_until(pos);
ed.no_newline = true;
ed.display()?;
self.count = 0;
Ok(())
}
......@@ -838,12 +863,18 @@ impl Vi {
for _ in 0..self.move_count_right(ed) {
let c = ed.current_buffer().char_after(ed.cursor()).unwrap();
if c.is_lowercase() {
ed.delete_after_cursor()?;
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()?;
for c in c.to_uppercase() {
ed.insert_after_cursor(c)?;
}
} else if c.is_uppercase() {
ed.delete_after_cursor()?;
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()?;
for c in c.to_lowercase() {
ed.insert_after_cursor(c)?;
}
......@@ -900,7 +931,9 @@ impl Vi {
// replace count characters
ed.current_buffer_mut().start_undo_group();
for _ in 0..self.move_count_right(ed) {
ed.delete_after_cursor()?;
ed.delete_after_cursor();
ed.no_newline = true;
ed.display()?;
ed.insert_after_cursor(c)?;
}
ed.current_buffer_mut().end_undo_group();
......@@ -961,9 +994,9 @@ impl Vi {
self.count = 0;
self.secondary_count = 0;
ed.move_cursor_to_start_of_line();
ed.delete_all_after_cursor();
ed.no_newline = true;
ed.display()?;
ed.delete_all_after_cursor()?;
// return to the previous mode
self.pop_mode(ed);
......@@ -1381,7 +1414,8 @@ mod tests {
assert_eq!(ed.cursor(), 12);
//simulate_keys(&mut map, &mut ed, [Ctrl('['), Char('u'), Char('i')].iter());
ed.delete_all_before_cursor().unwrap();
ed.delete_all_before_cursor();
ed.no_newline = true;
assert_eq!(ed.cursor(), 0);
//ed.insert_str_after_cursor("pat").unwrap();
//assert_eq!(ed.cursor(), 3);
......@@ -1410,7 +1444,8 @@ mod tests {
);
assert_eq!(ed.cursor(), 15);
ed.delete_all_before_cursor().unwrap();
ed.delete_all_before_cursor();
ed.no_newline = true;
assert_eq!(ed.cursor(), 0);
ed.insert_str_after_cursor("pat").unwrap();
assert_eq!(ed.cursor(), 3);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment