From c3c346e430836906eb2cc5d4e7b43f7836b8f984 Mon Sep 17 00:00:00 2001 From: Uzair Aftab Date: Tue, 1 Sep 2026 12:22:51 +0200 Subject: [PATCH] Don't delete a suggestion when there is no suggestion selected Deleting the last entry in the shell command history leaves the suggestions list focused but empty. An empty list reports its selected index as -1 (ListCursor.clampValue), and the delete keybinding passes that index straight to HandleDeleteSuggestion, which subscripts GetItems() with it. Pressing delete a second time panics and takes lazygit down with it. To reproduce: press ":" and run any command, press ":" again, tab into the suggestions, then press "d" twice. The handler two lines below already returns early on a -1, with a comment saying it should never happen. That index comes from lo.IndexOf, so this one never got the same scrutiny. We guard the binding the way ConfirmSuggestion directly above it is already guarded, with singleItemSelected. The key is then disabled with the usual reason instead of firing on an empty list --- pkg/gui/controllers/suggestions_controller.go | 1 + .../delete_suggestion_when_none_selected.go | 36 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 3 files changed, 38 insertions(+) create mode 100644 pkg/integration/tests/shell_commands/delete_suggestion_when_none_selected.go diff --git a/pkg/gui/controllers/suggestions_controller.go b/pkg/gui/controllers/suggestions_controller.go index 0553050e509..fa447278685 100644 --- a/pkg/gui/controllers/suggestions_controller.go +++ b/pkg/gui/controllers/suggestions_controller.go @@ -49,6 +49,7 @@ func (self *SuggestionsController) GetKeybindings(opts types.KeybindingsOpts) [] Handler: func() error { return self.context().State.OnDeleteSuggestion() }, + GetDisabledReason: self.require(self.singleItemSelected()), }, { Keys: opts.GetKeys(opts.Config.Universal.Edit), diff --git a/pkg/integration/tests/shell_commands/delete_suggestion_when_none_selected.go b/pkg/integration/tests/shell_commands/delete_suggestion_when_none_selected.go new file mode 100644 index 00000000000..d7165a8417b --- /dev/null +++ b/pkg/integration/tests/shell_commands/delete_suggestion_when_none_selected.go @@ -0,0 +1,36 @@ +package shell_commands + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var DeleteSuggestionWhenNoneSelected = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Press the delete key again after deleting the last entry in the shell command history", + ExtraCmdArgs: []string{}, + Skip: false, + SetupRepo: func(shell *Shell) {}, + SetupConfig: func(cfg *config.AppConfig) {}, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.GlobalPress(keys.Universal.ExecuteShellCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Shell command:")). + Type("echo 1"). + Confirm() + + // Deleting the only suggestion leaves the list focused but empty, so + // there is nothing for a second delete to act on. + t.GlobalPress(keys.Universal.ExecuteShellCommand) + t.ExpectPopup().Prompt(). + Title(Equals("Shell command:")). + SuggestionLines(Contains("echo 1")). + DeleteSuggestion(Contains("echo 1")) + + t.Views().Suggestions(). + IsFocused(). + IsEmpty(). + Press(keys.Universal.Remove) + + t.ExpectToast(Equals("Disabled: No item selected")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 5ac4bdcba3d..044caaf3619 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -411,6 +411,7 @@ var tests = []*components.IntegrationTest{ shell_commands.BasicShellCommand, shell_commands.ComplexShellCommand, shell_commands.DeleteFromHistory, + shell_commands.DeleteSuggestionWhenNoneSelected, shell_commands.EditHistory, shell_commands.History, shell_commands.OmitFromHistory,