mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-15 11:56:37 +02:00
We pass all of them to a single editor command, hoping that the editor will be able to handle multiple files (VS Code and vim do). We ignore directories that happen to be in the selection range; this makes it easier to edit multiple files in different folders in tree view. We show an error if only directories are selected, though.
92 lines
2.2 KiB
Go
92 lines
2.2 KiB
Go
package helpers
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"github.com/samber/lo"
|
|
)
|
|
|
|
type IFilesHelper interface {
|
|
EditFiles(filenames []string) error
|
|
EditFileAtLine(filename string, lineNumber int) error
|
|
OpenFile(filename string) error
|
|
}
|
|
|
|
type FilesHelper struct {
|
|
c *HelperCommon
|
|
}
|
|
|
|
func NewFilesHelper(c *HelperCommon) *FilesHelper {
|
|
return &FilesHelper{
|
|
c: c,
|
|
}
|
|
}
|
|
|
|
var _ IFilesHelper = &FilesHelper{}
|
|
|
|
func (self *FilesHelper) EditFiles(filenames []string) error {
|
|
absPaths := lo.Map(filenames, func(filename string, _ int) string {
|
|
absPath, err := filepath.Abs(filename)
|
|
if err != nil {
|
|
return filename
|
|
}
|
|
return absPath
|
|
})
|
|
cmdStr, suspend := self.c.Git().File.GetEditCmdStr(absPaths)
|
|
return self.callEditor(cmdStr, suspend)
|
|
}
|
|
|
|
func (self *FilesHelper) EditFileAtLine(filename string, lineNumber int) error {
|
|
absPath, err := filepath.Abs(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmdStr, suspend := self.c.Git().File.GetEditAtLineCmdStr(absPath, lineNumber)
|
|
return self.callEditor(cmdStr, suspend)
|
|
}
|
|
|
|
func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int) error {
|
|
absPath, err := filepath.Abs(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmdStr := self.c.Git().File.GetEditAtLineAndWaitCmdStr(absPath, lineNumber)
|
|
|
|
// Always suspend, regardless of the value of the suspend config,
|
|
// since we want to prevent interacting with the UI until the editor
|
|
// returns, even if the editor doesn't use the terminal
|
|
return self.callEditor(cmdStr, true)
|
|
}
|
|
|
|
func (self *FilesHelper) OpenDirInEditor(path string) error {
|
|
absPath, err := filepath.Abs(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmdStr, suspend := self.c.Git().File.GetOpenDirInEditorCmdStr(absPath)
|
|
|
|
return self.callEditor(cmdStr, suspend)
|
|
}
|
|
|
|
func (self *FilesHelper) callEditor(cmdStr string, suspend bool) error {
|
|
if suspend {
|
|
return self.c.RunSubprocessAndRefresh(
|
|
self.c.OS().Cmd.NewShell(cmdStr),
|
|
)
|
|
}
|
|
|
|
return self.c.OS().Cmd.NewShell(cmdStr).Run()
|
|
}
|
|
|
|
func (self *FilesHelper) OpenFile(filename string) error {
|
|
absPath, err := filepath.Abs(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
self.c.LogAction(self.c.Tr.Actions.OpenFile)
|
|
if err := self.c.OS().OpenFile(absPath); err != nil {
|
|
return self.c.Error(err)
|
|
}
|
|
return nil
|
|
}
|