diff options
author | Mark Kopenga <mkopenga@gmail.com> | 2021-08-23 10:15:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-23 10:15:38 +0200 |
commit | 487ad196a7c03d8cb3e0abc41dea42dd96db8a62 (patch) | |
tree | 589f36ab1814b4242e44d47fa398132bb958ad89 /pkg/commands | |
parent | 508af269fbc255162d8a01c9788dae9445db0393 (diff) | |
parent | 44140adb921d7891778deb7f1aa2f813622302f4 (diff) |
Merge pull request #1413 from Ryooooooga/feature/edit-line
Make os.editCommand customizable using template
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/files.go | 12 | ||||
-rw-r--r-- | pkg/commands/files_test.go | 42 | ||||
-rw-r--r-- | pkg/commands/patch/patch_modifier.go | 9 |
3 files changed, 53 insertions, 10 deletions
diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 9fa5fb1bd..ac508941b 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "path/filepath" + "strconv" "time" "github.com/go-errors/errors" @@ -321,7 +322,7 @@ func (c *GitCommand) ResetAndClean() error { return c.RemoveUntrackedFiles() } -func (c *GitCommand) EditFileCmdStr(filename string) (string, error) { +func (c *GitCommand) EditFileCmdStr(filename string, lineNumber int) (string, error) { editor := c.Config.GetUserConfig().OS.EditCommand if 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), + } + + editCmdTemplate := c.Config.GetUserConfig().OS.EditCommandTemplate + return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil } diff --git a/pkg/commands/files_test.go b/pkg/commands/files_test.go index 01c1a6793..0410c95c2 100644 --- a/pkg/commands/files_test.go +++ b/pkg/commands/files_test.go @@ -741,18 +741,20 @@ func TestGitCommandRemoveUntrackedFiles(t *testing.T) { // TestEditFileCmdStr is a function. func TestEditFileCmdStr(t *testing.T) { type scenario struct { - filename string - configEditCommand string - command func(string, ...string) *exec.Cmd - getenv func(string) string - getGitConfigValue func(string) (string, error) - test func(string, error) + filename string + configEditCommand string + configEditCommandTemplate string + command func(string, ...string) *exec.Cmd + getenv func(string) string + getGitConfigValue func(string) (string, error) + test func(string, error) } scenarios := []scenario{ { "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.EditCommand = s.configEditCommand + gitCmd.Config.GetUserConfig().OS.EditCommandTemplate = s.configEditCommandTemplate 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)) } } diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go index c2bbc60f6..3ab13931b 100644 --- a/pkg/commands/patch/patch_modifier.go +++ b/pkg/commands/patch/patch_modifier.go @@ -138,7 +138,14 @@ func ModifiedPatchForLines(log *logrus.Entry, filename string, diffText string, // I want to know, given a hunk, what line a given index is on func (hunk *PatchHunk) LineNumberOfLine(idx int) int { - lines := hunk.bodyLines[0 : idx-hunk.FirstLineIdx-1] + n := idx - hunk.FirstLineIdx - 1 + if n < 0 { + n = 0 + } else if n >= len(hunk.bodyLines) { + n = len(hunk.bodyLines) - 1 + } + + lines := hunk.bodyLines[0:n] offset := nLinesWithPrefix(lines, []string{"+", " "}) |