1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-23 12:18:51 +02:00

Support opening worktree in editor

This commit is contained in:
Jesse Duffield 2023-07-17 15:45:10 +10:00
parent 7b05dacb98
commit 3a7468ecac
6 changed files with 39 additions and 1 deletions

View File

@ -323,6 +323,7 @@ os:
editAtLine: 'myeditor --line={{line}} {{filename}}' editAtLine: 'myeditor --line={{line}} {{filename}}'
editAtLineAndWait: 'myeditor --block --line={{line}} {{filename}}' editAtLineAndWait: 'myeditor --block --line={{line}} {{filename}}'
editInTerminal: true editInTerminal: true
openDirInEditor: 'myeditor {{dir}}'
``` ```
The `editInTerminal` option is used to decide whether lazygit needs to suspend The `editInTerminal` option is used to decide whether lazygit needs to suspend

View File

@ -131,6 +131,17 @@ func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber
return cmdStr 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 { func (self *FileCommands) guessDefaultEditor() string {
// Try to query a few places where editors get configured // Try to query a few places where editors get configured
editor := self.config.GetCoreEditor() editor := self.config.GetCoreEditor()

View File

@ -28,10 +28,20 @@ func GetEditAtLineAndWaitTemplate(osConfig *OSConfig, guessDefaultEditor func()
return template 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 { type editPreset struct {
editTemplate string editTemplate string
editAtLineTemplate string editAtLineTemplate string
editAtLineAndWaitTemplate string editAtLineAndWaitTemplate string
openDirInEditorTemplate string
editInTerminal bool editInTerminal bool
} }
@ -48,30 +58,35 @@ func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset
editTemplate: "hx -- {{filename}}", editTemplate: "hx -- {{filename}}",
editAtLineTemplate: "hx -- {{filename}}:{{line}}", editAtLineTemplate: "hx -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "hx -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "hx -- {{filename}}:{{line}}",
openDirInEditorTemplate: "hx {{dir}}",
editInTerminal: true, editInTerminal: true,
}, },
"vscode": { "vscode": {
editTemplate: "code --reuse-window -- {{filename}}", editTemplate: "code --reuse-window -- {{filename}}",
editAtLineTemplate: "code --reuse-window --goto -- {{filename}}:{{line}}", editAtLineTemplate: "code --reuse-window --goto -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "code --reuse-window --goto --wait -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "code --reuse-window --goto --wait -- {{filename}}:{{line}}",
openDirInEditorTemplate: "code {{dir}}",
editInTerminal: false, editInTerminal: false,
}, },
"sublime": { "sublime": {
editTemplate: "subl -- {{filename}}", editTemplate: "subl -- {{filename}}",
editAtLineTemplate: "subl -- {{filename}}:{{line}}", editAtLineTemplate: "subl -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "subl --wait -- {{filename}}:{{line}}", editAtLineAndWaitTemplate: "subl --wait -- {{filename}}:{{line}}",
openDirInEditorTemplate: "subl {{dir}}",
editInTerminal: false, editInTerminal: false,
}, },
"bbedit": { "bbedit": {
editTemplate: "bbedit -- {{filename}}", editTemplate: "bbedit -- {{filename}}",
editAtLineTemplate: "bbedit +{{line}} -- {{filename}}", editAtLineTemplate: "bbedit +{{line}} -- {{filename}}",
editAtLineAndWaitTemplate: "bbedit +{{line}} --wait -- {{filename}}", editAtLineAndWaitTemplate: "bbedit +{{line}} --wait -- {{filename}}",
openDirInEditorTemplate: "bbedit {{dir}}",
editInTerminal: false, editInTerminal: false,
}, },
"xcode": { "xcode": {
editTemplate: "xed -- {{filename}}", editTemplate: "xed -- {{filename}}",
editAtLineTemplate: "xed --line {{line}} -- {{filename}}", editAtLineTemplate: "xed --line {{line}} -- {{filename}}",
editAtLineAndWaitTemplate: "xed --line {{line}} --wait -- {{filename}}", editAtLineAndWaitTemplate: "xed --line {{line}} --wait -- {{filename}}",
openDirInEditorTemplate: "xed {{dir}}",
editInTerminal: false, editInTerminal: false,
}, },
} }
@ -107,6 +122,7 @@ func standardTerminalEditorPreset(editor string) *editPreset {
editTemplate: editor + " -- {{filename}}", editTemplate: editor + " -- {{filename}}",
editAtLineTemplate: editor + " +{{line}} -- {{filename}}", editAtLineTemplate: editor + " +{{line}} -- {{filename}}",
editAtLineAndWaitTemplate: editor + " +{{line}} -- {{filename}}", editAtLineAndWaitTemplate: editor + " +{{line}} -- {{filename}}",
openDirInEditorTemplate: editor + " {{dir}}",
editInTerminal: true, editInTerminal: true,
} }
} }

View File

@ -318,6 +318,9 @@ type OSConfig struct {
// Pointer to bool so that we can distinguish unset (nil) from false. // Pointer to bool so that we can distinguish unset (nil) from false.
EditInTerminal *bool `yaml:"editInTerminal,omitempty"` 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 // A built-in preset that sets all of the above settings. Supported presets
// are defined in the getPreset function in editor_presets.go. // are defined in the getPreset function in editor_presets.go.
EditPreset string `yaml:"editPreset,omitempty"` EditPreset string `yaml:"editPreset,omitempty"`

View File

@ -37,6 +37,13 @@ func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int)
return self.callEditor(cmdStr, true) 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 { func (self *FilesHelper) callEditor(cmdStr string, editInTerminal bool) error {
if editInTerminal { if editInTerminal {
return self.c.RunSubprocessAndRefresh( return self.c.RunSubprocessAndRefresh(

View File

@ -116,7 +116,7 @@ func (self *WorktreesController) enter(worktree *models.Worktree) error {
} }
func (self *WorktreesController) open(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 { func (self *WorktreesController) checkSelected(callback func(worktree *models.Worktree) error) func() error {