From ba4ead6eada811d981e10b440301b9c3ea9ed87c Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Fri, 17 Jan 2025 09:33:24 +0100 Subject: [PATCH] Stop saving autosuggestions that we can't restore Sorry, commit 51adba6ee0 (Restore autosuggestion after corrected typo, 2025-01-10) was pushed too early. One issue is that it saves autosuggestions also when we edit in the middle, where we can't restore it. We'd still restore it in some cases, even though it doesn't apply. This breaks invariants that may cause various problems when interacting with the autosuggestion. Fix it by only saving the autosuggestion when we will be able to restore it correctly. --- src/reader.rs | 16 +++++++++++----- tests/checks/tmux-autosuggestion.fish | 11 ++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 96ab91f7b..db50263a3 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1257,11 +1257,9 @@ fn command_line_changed( use AutosuggestionUpdate::*; match autosuggestion_update { Preserve => (), - Remove => { - if !self.autosuggestion.is_empty() { - self.saved_autosuggestion = - Some(std::mem::take(&mut self.autosuggestion)) - } + Remove => self.autosuggestion.clear(), + RemoveAndSave => { + self.saved_autosuggestion = Some(std::mem::take(&mut self.autosuggestion)) } Restore => { self.autosuggestion = saved_autosuggestion.unwrap(); @@ -1669,6 +1667,7 @@ fn paint_layout(&mut self, reason: &wstr, is_final_rendering: bool) { enum AutosuggestionUpdate { Preserve, Remove, + RemoveAndSave, Restore, } @@ -1807,6 +1806,13 @@ fn push_edit_internal(&mut self, elt: EditableLineTag, edit: Edit, allow_coalesc let preserves_autosuggestion = self.try_apply_edit_to_autosuggestion(&edit); if preserves_autosuggestion { autosuggestion_update = AutosuggestionUpdate::Preserve + } else if !self.autosuggestion.is_empty() + && edit.range.start == self.autosuggestion.search_string_range.end + && edit.range.is_empty() + && !edit.replacement.is_empty() + { + // When inserting at the autosuggestion something that doesn't match, save it. + autosuggestion_update = AutosuggestionUpdate::RemoveAndSave; } else if self .saved_autosuggestion .as_ref() diff --git a/tests/checks/tmux-autosuggestion.fish b/tests/checks/tmux-autosuggestion.fish index ba6175a3a..245efc99e 100644 --- a/tests/checks/tmux-autosuggestion.fish +++ b/tests/checks/tmux-autosuggestion.fish @@ -31,8 +31,17 @@ tmux-sleep isolated-tmux capture-pane -p # CHECK: prompt 2> : ./COMPL +isolated-tmux send-keys C-u 'ech {' Left Left +tmux-sleep +isolated-tmux send-keys o C-e C-h 'still alive' Enter +tmux-sleep +isolated-tmux capture-pane -p +# CHECK: prompt {{\d+}}> echo still alive +# CHECK: still alive +# CHECK: prompt {{\d+}}> + isolated-tmux send-keys C-u 'echo (echo)' Enter isolated-tmux send-keys C-l 'echo (' tmux-sleep isolated-tmux capture-pane -p -# CHECK: prompt 3> echo (echo) +# CHECK: prompt {{\d+}}> echo (echo)