Skip to content

Allow opening single directory in Files panel with editor #4536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ os:
# Whether lazygit suspends until an edit process returns
editInTerminal: false

# For opening a directory in an editor
# For opening a directory in an editor. Should contain "{{dir}}".
openDirInEditor: ""

# A built-in preset that sets all of the above settings. Supported presets
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ os:
# Whether lazygit suspends until an edit process returns
editInTerminal: false

# For opening a directory in an editor
# For opening a directory in an editor. Should contain "{{dir}}".
openDirInEditor: ""

# A built-in preset that sets all of the above settings. Supported presets
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ type OSConfig struct {
// [dev] We're naming this `editInTerminal` for backwards compatibility
SuspendOnEdit *bool `yaml:"editInTerminal,omitempty"`

// For opening a directory in an editor
// For opening a directory in an editor. Should contain "{{dir}}".
OpenDirInEditor string `yaml:"openDirInEditor,omitempty"`

// A built-in preset that sets all of the above settings. Supported presets
Expand Down
12 changes: 10 additions & 2 deletions pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
15 changes: 15 additions & 0 deletions pkg/gui/controllers/list_controller_trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@
},
"openDirInEditor": {
"type": "string",
"description": "For opening a directory in an editor"
"description": "For opening a directory in an editor. Should contain \"{{dir}}\"."
},
"editPreset": {
"type": "string",
Expand Down