diff options
-rw-r--r-- | docs/Config.md | 1 | ||||
-rw-r--r-- | pkg/commands/git_commands/file.go | 11 | ||||
-rw-r--r-- | pkg/config/editor_presets.go | 16 | ||||
-rw-r--r-- | pkg/config/user_config.go | 3 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/files_helper.go | 7 | ||||
-rw-r--r-- | pkg/gui/controllers/worktrees_controller.go | 2 |
6 files changed, 39 insertions, 1 deletions
diff --git a/docs/Config.md b/docs/Config.md index 1ba8720e6..bc4b5b92b 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -323,6 +323,7 @@ os: editAtLine: 'myeditor --line={{line}} {{filename}}' editAtLineAndWait: 'myeditor --block --line={{line}} {{filename}}' editInTerminal: true + openDirInEditor: 'myeditor {{dir}}' ``` The `editInTerminal` option is used to decide whether lazygit needs to suspend diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 6c90b91f2..7efdb567a 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -131,6 +131,17 @@ func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber return cmdStr } +func (self *FileCommands) GetOpenDirInEditorCmdStr(path string) string { + template := config.GetOpenDirInEditorTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "dir": self.cmd.Quote(path), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr +} + func (self *FileCommands) guessDefaultEditor() string { // Try to query a few places where editors get configured editor := self.config.GetCoreEditor() diff --git a/pkg/config/editor_presets.go b/pkg/config/editor_presets.go index d489c9283..78a490cc0 100644 --- a/pkg/config/editor_presets.go +++ b/pkg/config/editor_presets.go @@ -28,10 +28,20 @@ func GetEditAtLineAndWaitTemplate(osConfig *OSConfig, guessDefaultEditor func() return template } +func GetOpenDirInEditorTemplate(osConfig *OSConfig, guessDefaultEditor func() string) string { + preset := getPreset(osConfig, guessDefaultEditor) + template := osConfig.OpenDirInEditor + if template == "" { + template = preset.openDirInEditorTemplate + } + return template +} + type editPreset struct { editTemplate string editAtLineTemplate string editAtLineAndWaitTemplate string + openDirInEditorTemplate string editInTerminal bool } @@ -48,30 +58,35 @@ func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset editTemplate: "hx -- {{filename}}", editAtLineTemplate: "hx -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "hx -- {{filename}}:{{line}}", + openDirInEditorTemplate: "hx {{dir}}", editInTerminal: true, }, "vscode": { editTemplate: "code --reuse-window -- {{filename}}", editAtLineTemplate: "code --reuse-window --goto -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + openDirInEditorTemplate: "code {{dir}}", editInTerminal: false, }, "sublime": { editTemplate: "subl -- {{filename}}", editAtLineTemplate: "subl -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "subl --wait -- {{filename}}:{{line}}", + openDirInEditorTemplate: "subl {{dir}}", editInTerminal: false, }, "bbedit": { editTemplate: "bbedit -- {{filename}}", editAtLineTemplate: "bbedit +{{line}} -- {{filename}}", editAtLineAndWaitTemplate: "bbedit +{{line}} --wait -- {{filename}}", + openDirInEditorTemplate: "bbedit {{dir}}", editInTerminal: false, }, "xcode": { editTemplate: "xed -- {{filename}}", editAtLineTemplate: "xed --line {{line}} -- {{filename}}", editAtLineAndWaitTemplate: "xed --line {{line}} --wait -- {{filename}}", + openDirInEditorTemplate: "xed {{dir}}", editInTerminal: false, }, } @@ -107,6 +122,7 @@ func standardTerminalEditorPreset(editor string) *editPreset { editTemplate: editor + " -- {{filename}}", editAtLineTemplate: editor + " +{{line}} -- {{filename}}", editAtLineAndWaitTemplate: editor + " +{{line}} -- {{filename}}", + openDirInEditorTemplate: editor + " {{dir}}", editInTerminal: true, } } diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index efff609fe..6ac64b2b5 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -318,6 +318,9 @@ type OSConfig struct { // Pointer to bool so that we can distinguish unset (nil) from false. EditInTerminal *bool `yaml:"editInTerminal,omitempty"` + // For opening a directory in an editor + OpenDirInEditor string `yaml:"openDirInEditor,omitempty"` + // A built-in preset that sets all of the above settings. Supported presets // are defined in the getPreset function in editor_presets.go. EditPreset string `yaml:"editPreset,omitempty"` diff --git a/pkg/gui/controllers/helpers/files_helper.go b/pkg/gui/controllers/helpers/files_helper.go index 1baa0191b..8f9a816f8 100644 --- a/pkg/gui/controllers/helpers/files_helper.go +++ b/pkg/gui/controllers/helpers/files_helper.go @@ -37,6 +37,13 @@ func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int) return self.callEditor(cmdStr, true) } +func (self *FilesHelper) OpenDirInEditor(path string) error { + cmdStr := self.c.Git().File.GetOpenDirInEditorCmdStr(path) + + // Not editing in terminal because surely that's not a thing. + return self.callEditor(cmdStr, false) +} + func (self *FilesHelper) callEditor(cmdStr string, editInTerminal bool) error { if editInTerminal { return self.c.RunSubprocessAndRefresh( diff --git a/pkg/gui/controllers/worktrees_controller.go b/pkg/gui/controllers/worktrees_controller.go index 52b76546e..c38a679da 100644 --- a/pkg/gui/controllers/worktrees_controller.go +++ b/pkg/gui/controllers/worktrees_controller.go @@ -116,7 +116,7 @@ func (self *WorktreesController) enter(worktree *models.Worktree) error { } func (self *WorktreesController) open(worktree *models.Worktree) error { - return self.c.Helpers().Files.OpenFile(worktree.Path) + return self.c.Helpers().Files.OpenDirInEditor(worktree.Path) } func (self *WorktreesController) checkSelected(callback func(worktree *models.Worktree) error) func() error { |