From e5730cb80bffe835725f94d40e7ea28838f45f91 Mon Sep 17 00:00:00 2001 From: Ryooooooga Date: Fri, 22 Apr 2022 21:54:03 +0900 Subject: [PATCH] fix: improve default editCommandTemplate --- docs/Config.md | 6 +++--- pkg/commands/git_commands/file.go | 13 +++++++------ pkg/commands/git_commands/file_test.go | 26 ++++++++++++++++++++------ pkg/config/config_default_platform.go | 6 ++---- pkg/config/config_linux.go | 4 +--- pkg/config/config_windows.go | 4 +--- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/docs/Config.md b/docs/Config.md index 17ee21834..7e9bd5c98 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -83,7 +83,7 @@ git: diffContextSize: 3 # how many lines of context are shown around a change in diffs os: editCommand: '' # see 'Configuring File Editing' section - editCommandTemplate: '{{editor}} {{filename}}' + editCommandTemplate: '' openCommand: '' refresher: refreshInterval: 10 # File/submodule refresh interval in seconds. Auto-refresh can be disabled via option 'git.autoRefresh'. @@ -273,7 +273,7 @@ You can specify a line number you are currently at when in the line-by-line mode ```yaml os: editCommand: 'vim' - editCommandTemplate: '{{editor}} +{{line}} {{filename}}' + editCommandTemplate: '{{editor}} +{{line}} -- {{filename}}' ``` or @@ -281,7 +281,7 @@ or ```yaml os: editCommand: 'code' - editCommandTemplate: '{{editor}} --goto {{filename}}:{{line}}' + editCommandTemplate: '{{editor}} --goto -- {{filename}}:{{line}}' ``` `{{editor}}` in `editCommandTemplate` is replaced with the value of `editCommand`. diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 1837ee4a4..898c26e33 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -5,7 +5,6 @@ import ( "strconv" "github.com/go-errors/errors" - "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -59,14 +58,16 @@ func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string } editCmdTemplate := self.UserConfig.OS.EditCommandTemplate - if editCmdTemplate == config.DefaultEditCommandTemplate { + if len(editCmdTemplate) == 0 { switch editor { - case "emacs", "nano", "vi", "vim": - editCmdTemplate = "{{editor}} +{{line}} {{filename}}" + case "emacs", "nano", "vi", "vim", "nvim": + editCmdTemplate = "{{editor}} +{{line}} -- {{filename}}" case "subl": - editCmdTemplate = "{{editor}} {{filename}}:{{line}}" + editCmdTemplate = "{{editor}} -- {{filename}}:{{line}}" case "code": - editCmdTemplate = "{{editor}} -r --goto {{filename}}:{{line}}" + editCmdTemplate = "{{editor}} -r --goto -- {{filename}}:{{line}}" + default: + editCmdTemplate = "{{editor}} -- {{filename}}" } } return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil diff --git a/pkg/commands/git_commands/file_test.go b/pkg/commands/git_commands/file_test.go index 61482054b..4b9128dfe 100644 --- a/pkg/commands/git_commands/file_test.go +++ b/pkg/commands/git_commands/file_test.go @@ -47,7 +47,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: nil, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `nano +1 "test"`, cmdStr) + assert.Equal(t, `nano "test"`, cmdStr) }, }, { @@ -61,7 +61,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: map[string]string{"core.editor": "nano"}, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `nano +1 "test"`, cmdStr) + assert.Equal(t, `nano "test"`, cmdStr) }, }, { @@ -79,7 +79,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: nil, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `nano +1 "test"`, cmdStr) + assert.Equal(t, `nano "test"`, cmdStr) }, }, { @@ -97,7 +97,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: nil, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `emacs +1 "test"`, cmdStr) + assert.Equal(t, `emacs "test"`, cmdStr) }, }, { @@ -112,7 +112,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: nil, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `vi +1 "test"`, cmdStr) + assert.Equal(t, `vi "test"`, cmdStr) }, }, { @@ -127,7 +127,7 @@ func TestEditFileCmdStr(t *testing.T) { gitConfigMockResponses: nil, test: func(cmdStr string, err error) { assert.NoError(t, err) - assert.Equal(t, `vi +1 "file/with space"`, cmdStr) + assert.Equal(t, `vi "file/with space"`, cmdStr) }, }, { @@ -144,6 +144,20 @@ func TestEditFileCmdStr(t *testing.T) { assert.Equal(t, `vim +1 "open file/at line"`, cmdStr) }, }, + { + filename: "default edit command template", + configEditCommand: "vim", + configEditCommandTemplate: "", + runner: oscommands.NewFakeRunner(t), + getenv: func(env string) string { + return "" + }, + gitConfigMockResponses: nil, + test: func(cmdStr string, err error) { + assert.NoError(t, err) + assert.Equal(t, `vim +1 -- "default edit command template"`, cmdStr) + }, + }, } for _, s := range scenarios { diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go index 32b76cbf0..6784f0ce2 100644 --- a/pkg/config/config_default_platform.go +++ b/pkg/config/config_default_platform.go @@ -3,14 +3,12 @@ package config -const DefaultEditCommandTemplate = `{{editor}} {{filename}}` - // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ EditCommand: ``, - EditCommandTemplate: DefaultEditCommandTemplate, - OpenCommand: "open {{filename}}", + EditCommandTemplate: "", + OpenCommand: "open -- {{filename}}", OpenLinkCommand: "open {{link}}", } } diff --git a/pkg/config/config_linux.go b/pkg/config/config_linux.go index 93baa1335..b9e195f9f 100644 --- a/pkg/config/config_linux.go +++ b/pkg/config/config_linux.go @@ -1,12 +1,10 @@ package config -const DefaultEditCommandTemplate = `{{editor}} {{filename}}` - // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ EditCommand: ``, - EditCommandTemplate: DefaultEditCommandTemplate, + EditCommandTemplate: "", OpenCommand: `xdg-open {{filename}} >/dev/null`, OpenLinkCommand: `xdg-open {{link}} >/dev/null`, } diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go index eb0b00728..12ecb8dff 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -1,12 +1,10 @@ package config -const DefaultEditCommandTemplate = `{{editor}} {{filename}}` - // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ EditCommand: ``, - EditCommandTemplate: DefaultEditCommandTemplate, + EditCommandTemplate: "", OpenCommand: `start "" {{filename}}`, OpenLinkCommand: `start "" {{link}}`, }