Use openDirInEditor when editing a single dir

This commit is contained in:
Ivo Maceira 2025-05-05 22:22:14 +02:00 committed by ivomac
parent e6bd9d0ae6
commit 9ad88542bc
2 changed files with 25 additions and 2 deletions

View file

@ -90,8 +90,8 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
},
{
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.withItems(self.edit),
GetDisabledReason: self.require(self.itemsSelected(self.canEditFiles)),
Handler: self.withItems(self.editOrOpenDir),
GetDisabledReason: self.any(self.singleItemSelected(), self.itemsSelected(self.canEditFiles)),
Description: self.c.Tr.Edit,
Tooltip: self.c.Tr.EditFileTooltip,
DisplayOnScreen: true,
@ -913,6 +913,14 @@ func (self *FilesController) edit(nodes []*filetree.FileNode) error {
}))
}
func (self *FilesController) editOrOpenDir(nodes []*filetree.FileNode) error {
if len(nodes) == 1 && !nodes[0].IsFile() {
return self.c.Helpers().Files.OpenDirInEditor(nodes[0].GetPath())
} else {
return self.edit(nodes)
}
}
func (self *FilesController) canEditFiles(nodes []*filetree.FileNode) *types.DisabledReason {
if lo.NoneBy(nodes, func(node *filetree.FileNode) bool { return node.IsFile() }) {
return &types.DisabledReason{

View file

@ -44,6 +44,21 @@ func (self *ListControllerTrait[T]) require(callbacks ...func() *types.DisabledR
}
}
// Complement to require - returns nil if any of the provided callbacks return nil.
// If all callbacks return a non-nil DisabledReason, it returns the last one encountered.
func (self *ListControllerTrait[T]) any(callbacks ...func() *types.DisabledReason) func() *types.DisabledReason {
return func() *types.DisabledReason {
var disabledReason *types.DisabledReason
for _, callback := range callbacks {
if disabledReason := callback(); disabledReason == nil {
return disabledReason
}
}
return disabledReason
}
}
// Convenience function for enforcing that a single item is selected.
// Also takes callbacks for additional disabled reasons, and passes the selected
// item into each one.