From 8ae12973df4304c5c3a420211fb276b91fc022f3 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Wed, 25 Dec 2024 05:13:38 +0100 Subject: [PATCH] Try to simplify commandline change hooks --- src/editable_line.rs | 10 -------- src/pager.rs | 1 - src/reader.rs | 50 ++++++++++++++++++++------------------ src/tests/editable_line.rs | 2 +- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/editable_line.rs b/src/editable_line.rs index 1a8f9e209..e6e5c192f 100644 --- a/src/editable_line.rs +++ b/src/editable_line.rs @@ -159,16 +159,6 @@ impl EditableLine { self.text[start..end].trim_matches('\n') } - pub fn clear(&mut self) { - if self.is_empty() { - return; - } - self.push_edit( - Edit::new(0..self.len(), L!("").to_owned()), - /*allow_coalesce=*/ false, - ); - } - /// Modify the commandline according to @edit. Most modifications to the /// text should pass through this function. pub fn push_edit(&mut self, mut edit: Edit, allow_coalesce: bool) { diff --git a/src/pager.rs b/src/pager.rs index 95e9c9a5c..430182ace 100644 --- a/src/pager.rs +++ b/src/pager.rs @@ -989,7 +989,6 @@ impl Pager { self.selected_completion_idx = None; self.fully_disclosed = false; self.search_field_shown = false; - self.search_field_line.clear(); self.extra_progress_text.clear(); self.suggested_row_start = 0; } diff --git a/src/reader.rs b/src/reader.rs index f12e14038..3eac5663b 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -653,9 +653,8 @@ fn read_i(parser: &Parser) -> i32 { continue; } - data.command_line.clear(); + data.clear(EditableLineTag::Commandline); data.update_buff_pos(EditableLineTag::Commandline, None); - data.command_line_changed(EditableLineTag::Commandline); // OSC 133 End of command data.screen.write_bytes(b"\x1b]133;C\x07"); event::fire_generic(parser, L!("fish_preexec").to_owned(), vec![command.clone()]); @@ -1246,6 +1245,9 @@ impl ReaderData { ); return; } + if self.pager.is_empty() { + return; + } self.pager.refilter_completions(); self.pager_selection_changed(); } @@ -1311,13 +1313,6 @@ impl ReaderData { } } - fn maybe_refilter_pager(&mut self, elt: EditableLineTag) { - match elt { - EditableLineTag::Commandline => (), - EditableLineTag::SearchField => self.command_line_changed(elt), - } - } - /// Update the cursor position. fn update_buff_pos(&mut self, elt: EditableLineTag, mut new_pos: Option) -> bool { if self.cursor_end_mode == CursorEndMode::Inclusive { @@ -1607,20 +1602,17 @@ impl ReaderData { /// using syntax highlighting, etc. /// Returns true if the string changed. fn insert_string(&mut self, elt: EditableLineTag, s: &wstr) { - if !s.is_empty() { - let history_search_active = self.history_search.active(); - let el = self.edit_line_mut(elt); - el.push_edit( - Edit::new(el.position()..el.position(), s.to_owned()), - /*allow_coalesce=*/ !history_search_active, - ); - } - + let history_search_active = self.history_search.active(); + let el = self.edit_line(elt); + self.push_edit_internal( + elt, + Edit::new(el.position()..el.position(), s.to_owned()), + /*allow_coalesce=*/ !history_search_active, + ); if elt == EditableLineTag::Commandline { self.command_line_has_transient_edit = false; self.suppress_autosuggestion = false; } - self.maybe_refilter_pager(elt); } /// Erase @length characters starting at @offset. @@ -1628,6 +1620,13 @@ impl ReaderData { self.push_edit(elt, Edit::new(range, L!("").to_owned())); } + fn clear(&mut self, elt: EditableLineTag) { + let el = self.edit_line(elt); + if !el.is_empty() { + self.erase_substring(elt, 0..el.len()); + } + } + /// Replace the text of length @length at @offset by @replacement. fn replace_substring( &mut self, @@ -1639,9 +1638,7 @@ impl ReaderData { } fn push_edit(&mut self, elt: EditableLineTag, edit: Edit) { - self.edit_line_mut(elt) - .push_edit(edit, /*allow_coalesce=*/ false); - self.maybe_refilter_pager(elt); + self.push_edit_internal(elt, edit, /*allow_coalesce=*/ false); } /// Insert the character into the command line buffer and print it to the screen using syntax @@ -1662,6 +1659,11 @@ impl ReaderData { self.update_buff_pos(elt, Some(pos)); } + fn push_edit_internal(&mut self, elt: EditableLineTag, edit: Edit, allow_coalesce: bool) { + self.edit_line_mut(elt).push_edit(edit, allow_coalesce); + self.command_line_changed(elt); + } + /// Undo the transient edit und update commandline accordingly. fn clear_transient_edit(&mut self) { if !self.command_line_has_transient_edit { @@ -3475,7 +3477,7 @@ impl<'a> Reader<'a> { self.clear_pager(); } self.update_buff_pos(elt, None); - self.maybe_refilter_pager(elt); + self.command_line_changed(elt); } rl::BeginUndoGroup => { let (_elt, el) = self.active_edit_line_mut(); @@ -3697,6 +3699,7 @@ impl ReaderData { fn clear_pager(&mut self) { self.pager.clear(); self.history_pager = None; + self.clear(EditableLineTag::SearchField); self.command_line_has_transient_edit = false; } @@ -3812,7 +3815,6 @@ impl ReaderData { 0..self.command_line.len(), b.to_owned(), ); - self.command_line_changed(EditableLineTag::Commandline); // Don't set a position past the command line length. if pos > command_line_len { diff --git a/src/tests/editable_line.rs b/src/tests/editable_line.rs index 7198204ca..6f4df30bb 100644 --- a/src/tests/editable_line.rs +++ b/src/tests/editable_line.rs @@ -42,7 +42,7 @@ fn test_undo() { assert_eq!(line.position(), 5); // Undo coalesced edits - line.clear(); + line.push_edit(Edit::new(0..line.len(), L!("").to_owned()), false); line.push_edit(Edit::new(insert(&line), L!("a").to_owned()), true); line.push_edit(Edit::new(insert(&line), L!("b").to_owned()), true); line.push_edit(Edit::new(insert(&line), L!("c").to_owned()), true);