mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-03 13:21:56 +02:00
introduce edit command template to open a specifig line of a file
This commit is contained in:
parent
d626bcac00
commit
4f66093335
@ -64,7 +64,8 @@ git:
|
||||
disableForcePushing: false
|
||||
parseEmoji: false
|
||||
os:
|
||||
editCommand: '' # see 'Configuring File Editing' section
|
||||
editor: '' # see 'Configuring File Editing' section
|
||||
editCommand: '{{editor}} {{filename}}'
|
||||
openCommand: ''
|
||||
refresher:
|
||||
refreshInterval: 10 # file/submodule refresh interval in seconds
|
||||
@ -230,7 +231,7 @@ Lazygit will edit a file with the first set editor in the following:
|
||||
|
||||
```yaml
|
||||
os:
|
||||
editCommand: 'vim' # as an example
|
||||
editor: 'vim' # as an example
|
||||
```
|
||||
|
||||
2. \$(git config core.editor)
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
@ -321,8 +322,8 @@ func (c *GitCommand) ResetAndClean() error {
|
||||
return c.RemoveUntrackedFiles()
|
||||
}
|
||||
|
||||
func (c *GitCommand) EditFileCmdStr(filename string) (string, error) {
|
||||
editor := c.Config.GetUserConfig().OS.EditCommand
|
||||
func (c *GitCommand) EditFileCmdStr(filename string, lineNumber int) (string, error) {
|
||||
editor := c.Config.GetUserConfig().OS.Editor
|
||||
|
||||
if editor == "" {
|
||||
editor = c.GetConfigValue("core.editor")
|
||||
@ -346,5 +347,12 @@ func (c *GitCommand) EditFileCmdStr(filename string) (string, error) {
|
||||
return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s %s", editor, c.OSCommand.Quote(filename)), nil
|
||||
templateValues := map[string]string{
|
||||
"editor": editor,
|
||||
"filename": c.OSCommand.Quote(filename),
|
||||
"line": strconv.Itoa(lineNumber),
|
||||
}
|
||||
|
||||
editTemplate := c.Config.GetUserConfig().OS.EditCommand
|
||||
return utils.ResolvePlaceholderString(editTemplate, templateValues), nil
|
||||
}
|
||||
|
@ -742,6 +742,7 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) {
|
||||
func TestEditFileCmdStr(t *testing.T) {
|
||||
type scenario struct {
|
||||
filename string
|
||||
configEditor string
|
||||
configEditCommand string
|
||||
command func(string, ...string) *exec.Cmd
|
||||
getenv func(string) string
|
||||
@ -753,6 +754,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
return secureexec.Command("exit", "1")
|
||||
},
|
||||
@ -769,6 +771,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"nano",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, args ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("echo")
|
||||
@ -787,6 +790,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("exit", "1")
|
||||
@ -805,6 +809,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("exit", "1")
|
||||
@ -826,6 +831,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("exit", "1")
|
||||
@ -848,6 +854,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"test",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, arg ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("echo")
|
||||
@ -866,6 +873,7 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
{
|
||||
"file/with space",
|
||||
"",
|
||||
"{{editor}} {{filename}}",
|
||||
func(name string, args ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("echo")
|
||||
@ -881,14 +889,34 @@ func TestEditFileCmdStr(t *testing.T) {
|
||||
assert.Equal(t, "vi \"file/with space\"", cmdStr)
|
||||
},
|
||||
},
|
||||
{
|
||||
"open file/at line",
|
||||
"vim",
|
||||
"{{editor}} +{{line}} {{filename}}",
|
||||
func(name string, args ...string) *exec.Cmd {
|
||||
assert.Equal(t, "which", name)
|
||||
return secureexec.Command("echo")
|
||||
},
|
||||
func(env string) string {
|
||||
return ""
|
||||
},
|
||||
func(cf string) (string, error) {
|
||||
return "", nil
|
||||
},
|
||||
func(cmdStr string, err error) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "vim +1 \"open file/at line\"", cmdStr)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, s := range scenarios {
|
||||
gitCmd := NewDummyGitCommand()
|
||||
gitCmd.Config.GetUserConfig().OS.Editor = s.configEditor
|
||||
gitCmd.Config.GetUserConfig().OS.EditCommand = s.configEditCommand
|
||||
gitCmd.OSCommand.Command = s.command
|
||||
gitCmd.OSCommand.Getenv = s.getenv
|
||||
gitCmd.getGitConfigValue = s.getGitConfigValue
|
||||
s.test(gitCmd.EditFileCmdStr(s.filename))
|
||||
s.test(gitCmd.EditFileCmdStr(s.filename, 1))
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ package config
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() OSConfig {
|
||||
return OSConfig{
|
||||
EditCommand: ``,
|
||||
Editor: ``,
|
||||
EditCommand: `{{editor}} {{filename}}`,
|
||||
OpenCommand: "open {{filename}}",
|
||||
OpenLinkCommand: "open {{link}}",
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ package config
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() OSConfig {
|
||||
return OSConfig{
|
||||
EditCommand: ``,
|
||||
Editor: ``,
|
||||
EditCommand: `{{editor}} {{filename}}`,
|
||||
OpenCommand: `sh -c "xdg-open {{filename}} >/dev/null"`,
|
||||
OpenLinkCommand: `sh -c "xdg-open {{link}} >/dev/null"`,
|
||||
}
|
||||
|
@ -3,7 +3,8 @@ package config
|
||||
// GetPlatformDefaultConfig gets the defaults for the platform
|
||||
func GetPlatformDefaultConfig() OSConfig {
|
||||
return OSConfig{
|
||||
EditCommand: ``,
|
||||
Editor: ``,
|
||||
EditCommand: `{{editor}} {{filename}}`,
|
||||
OpenCommand: `cmd /c "start "" {{filename}}"`,
|
||||
OpenLinkCommand: `cmd /c "start "" {{link}}"`,
|
||||
}
|
||||
|
@ -252,7 +252,10 @@ type KeybindingSubmodulesConfig struct {
|
||||
|
||||
// OSConfig contains config on the level of the os
|
||||
type OSConfig struct {
|
||||
// EditCommand is the command for editing a file
|
||||
// Editor is the command for editing a file
|
||||
Editor string `yaml:"editor,omitempty"`
|
||||
|
||||
// EditCommand is the command template for editing a file
|
||||
EditCommand string `yaml:"editCommand,omitempty"`
|
||||
|
||||
// OpenCommand is the command for opening a file
|
||||
|
@ -474,13 +474,17 @@ func (gui *Gui) handleCommitEditorPress() error {
|
||||
}
|
||||
|
||||
func (gui *Gui) editFile(filename string) error {
|
||||
cmdStr, err := gui.GitCommand.EditFileCmdStr(filename)
|
||||
return gui.editFileAtLine(filename, 1)
|
||||
}
|
||||
|
||||
func (gui *Gui) editFileAtLine(filename string, lineNumber int) error {
|
||||
cmdStr, err := gui.GitCommand.EditFileCmdStr(filename, lineNumber)
|
||||
if err != nil {
|
||||
return gui.surfaceError(err)
|
||||
}
|
||||
|
||||
return gui.runSubprocessWithSuspenseAndRefresh(
|
||||
gui.OSCommand.WithSpan(gui.Tr.Spans.EditFile).PrepareShellSubProcess(cmdStr),
|
||||
gui.OSCommand.WithSpan(gui.Tr.Spans.EditFile).ShellCommandFromString(cmdStr),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1301,7 +1301,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
|
||||
ViewName: "main",
|
||||
Contexts: []string{string(MAIN_STAGING_CONTEXT_KEY)},
|
||||
Key: gui.getKey(config.Universal.Edit),
|
||||
Handler: gui.handleFileEdit,
|
||||
Handler: gui.handleLineByLineEdit,
|
||||
Description: gui.Tr.LcEditFile,
|
||||
},
|
||||
{
|
||||
|
@ -272,3 +272,14 @@ func (gui *Gui) withLBLActiveCheck(f func(*LblPanelState) error) error {
|
||||
|
||||
return f(state)
|
||||
}
|
||||
|
||||
func (gui *Gui) handleLineByLineEdit() error {
|
||||
file := gui.getSelectedFile()
|
||||
if file == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
lineNumber := gui.State.Panels.LineByLine.CurrentLineNumber()
|
||||
|
||||
return gui.editFileAtLine(file.Name, lineNumber)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user