Try to simplify commandline change hooks

This commit is contained in:
Johannes Altmanninger 2024-12-25 05:13:38 +01:00
parent 459fc3c887
commit 8ae12973df
4 changed files with 27 additions and 36 deletions

View file

@ -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) {

View file

@ -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;
}

View file

@ -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<usize>) -> 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 {

View file

@ -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);