Allow editing a custom command from the suggestions list by pressing 'e'

For custom commands it is useful to select an earlier command and have it copied
to the prompt for further editing. This can be done by hitting 'e' now.

For other types of suggestion panels we don't enable this behavior, as you can't
create arbitrary new items there that don't already exist as a suggestion.
This commit is contained in:
Stefan Haller 2024-04-27 22:18:57 +02:00
parent da3e0f7147
commit a7041cf492
9 changed files with 64 additions and 0 deletions

View file

@ -20,6 +20,8 @@ type SuggestionsContextState struct {
OnDeleteSuggestion func() error OnDeleteSuggestion func() error
AsyncHandler *tasks.AsyncHandler AsyncHandler *tasks.AsyncHandler
AllowEditSuggestion bool
// FindSuggestions will take a string that the user has typed into a prompt // FindSuggestions will take a string that the user has typed into a prompt
// and return a slice of suggestions which match that string. // and return a slice of suggestions which match that string.
FindSuggestions func(string) []*types.Suggestion FindSuggestions func(string) []*types.Suggestion

View file

@ -18,6 +18,7 @@ func (self *CustomCommandAction) Call() error {
return self.c.Prompt(types.PromptOpts{ return self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.CustomCommand, Title: self.c.Tr.CustomCommand,
FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(), FindSuggestionsFunc: self.GetCustomCommandsHistorySuggestionsFunc(),
AllowEditSuggestion: true,
HandleConfirm: func(command string) error { HandleConfirm: func(command string) error {
if self.shouldSaveCommand(command) { if self.shouldSaveCommand(command) {
self.c.GetAppState().CustomCommandsHistory = utils.Limit( self.c.GetAppState().CustomCommandsHistory = utils.Limit(

View file

@ -223,6 +223,8 @@ func (self *ConfirmationHelper) CreatePopupPanel(ctx goContext.Context, opts typ
return err return err
} }
self.c.Contexts().Suggestions.State.AllowEditSuggestion = opts.AllowEditSuggestion
self.c.State().GetRepoState().SetCurrentPopupOpts(&opts) self.c.State().GetRepoState().SetCurrentPopupOpts(&opts)
return self.c.PushContext(self.c.Contexts().Confirmation) return self.c.PushContext(self.c.Contexts().Confirmation)

View file

@ -49,6 +49,21 @@ func (self *SuggestionsController) GetKeybindings(opts types.KeybindingsOpts) []
return self.context().State.OnDeleteSuggestion() return self.context().State.OnDeleteSuggestion()
}, },
}, },
{
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: func() error {
if self.context().State.AllowEditSuggestion {
if selectedItem := self.c.Contexts().Suggestions.GetSelected(); selectedItem != nil {
self.c.Contexts().Confirmation.GetView().TextArea.Clear()
self.c.Contexts().Confirmation.GetView().TextArea.TypeString(selectedItem.Value)
self.c.Contexts().Confirmation.GetView().RenderTextArea()
self.c.Contexts().Suggestions.RefreshSuggestions()
return self.c.ReplaceContext(self.c.Contexts().Confirmation)
}
}
return nil
},
},
} }
return bindings return bindings

View file

@ -111,6 +111,7 @@ func (self *PopupHandler) Prompt(opts types.PromptOpts) error {
HandleClose: opts.HandleClose, HandleClose: opts.HandleClose,
HandleDeleteSuggestion: opts.HandleDeleteSuggestion, HandleDeleteSuggestion: opts.HandleDeleteSuggestion,
FindSuggestionsFunc: opts.FindSuggestionsFunc, FindSuggestionsFunc: opts.FindSuggestionsFunc,
AllowEditSuggestion: opts.AllowEditSuggestion,
Mask: opts.Mask, Mask: opts.Mask,
}) })
} }

View file

@ -176,6 +176,7 @@ type CreatePopupPanelOpts struct {
FindSuggestionsFunc func(string) []*Suggestion FindSuggestionsFunc func(string) []*Suggestion
Mask bool Mask bool
AllowEditSuggestion bool
} }
type ConfirmOpts struct { type ConfirmOpts struct {
@ -193,6 +194,7 @@ type PromptOpts struct {
InitialContent string InitialContent string
FindSuggestionsFunc func(string) []*Suggestion FindSuggestionsFunc func(string) []*Suggestion
HandleConfirm func(string) error HandleConfirm func(string) error
AllowEditSuggestion bool
// CAPTURE THIS // CAPTURE THIS
HandleClose func() error HandleClose func() error
HandleDeleteSuggestion func(int) error HandleDeleteSuggestion func(int) error

View file

@ -91,3 +91,12 @@ func (self *PromptDriver) DeleteSuggestion(matcher *TextMatcher) *PromptDriver {
self.t.press(self.t.keys.Universal.Remove) self.t.press(self.t.keys.Universal.Remove)
return self return self
} }
func (self *PromptDriver) EditSuggestion(matcher *TextMatcher) *PromptDriver {
self.t.press(self.t.keys.Universal.TogglePanel)
self.t.Views().Suggestions().
IsFocused().
NavigateToLine(matcher)
self.t.press(self.t.keys.Universal.Edit)
return self
}

View file

@ -0,0 +1,31 @@
package custom_commands
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var EditHistory = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Edit an entry from the custom commands 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.ExecuteCustomCommand)
t.ExpectPopup().Prompt().
Title(Equals("Custom command:")).
Type("echo x").
Confirm()
t.GlobalPress(keys.Universal.ExecuteCustomCommand)
t.ExpectPopup().Prompt().
Title(Equals("Custom command:")).
Type("ec").
SuggestionLines(
Equals("echo x"),
).
EditSuggestion(Equals("echo x")).
InitialText(Equals("echo x"))
},
})

View file

@ -106,6 +106,7 @@ var tests = []*components.IntegrationTest{
custom_commands.CheckForConflicts, custom_commands.CheckForConflicts,
custom_commands.ComplexCmdAtRuntime, custom_commands.ComplexCmdAtRuntime,
custom_commands.DeleteFromHistory, custom_commands.DeleteFromHistory,
custom_commands.EditHistory,
custom_commands.FormPrompts, custom_commands.FormPrompts,
custom_commands.History, custom_commands.History,
custom_commands.MenuFromCommand, custom_commands.MenuFromCommand,