diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-04-13 21:22:17 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-13 21:22:17 +1000 |
commit | 04e0a9bb450113d6380ff82cd830d3c2f53090c9 (patch) | |
tree | a72d5a125680bd4bdf450da7bb7d882185805f76 | |
parent | caedf574845513d96f30252535941b6de9351308 (diff) | |
parent | 046b0d9daa46f2fa8da9f196dc50a5ae02239675 (diff) |
Merge pull request #2523 from stefanhaller/editor-config
-rw-r--r-- | docs/Config.md | 61 | ||||
-rw-r--r-- | pkg/commands/git_commands/file.go | 83 | ||||
-rw-r--r-- | pkg/commands/git_commands/file_test.go | 203 | ||||
-rw-r--r-- | pkg/commands/oscommands/os.go | 23 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_default_test.go (renamed from pkg/commands/oscommands/os_test_default.go) | 4 | ||||
-rw-r--r-- | pkg/commands/oscommands/os_windows_test.go (renamed from pkg/commands/oscommands/os_test_windows.go) | 13 | ||||
-rw-r--r-- | pkg/config/config_default_platform.go | 6 | ||||
-rw-r--r-- | pkg/config/config_linux.go | 12 | ||||
-rw-r--r-- | pkg/config/config_windows.go | 6 | ||||
-rw-r--r-- | pkg/config/editor_presets.go | 109 | ||||
-rw-r--r-- | pkg/config/editor_presets_test.go | 126 | ||||
-rw-r--r-- | pkg/config/user_config.go | 44 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/files_helper.go | 36 | ||||
-rw-r--r-- | pkg/gui/controllers/merge_conflicts_controller.go | 3 | ||||
-rw-r--r-- | pkg/gui/controllers/patch_building_controller.go | 3 | ||||
-rw-r--r-- | pkg/gui/controllers/staging_controller.go | 5 | ||||
-rw-r--r-- | pkg/gui/gui.go | 33 | ||||
-rw-r--r-- | pkg/i18n/english.go | 17 |
18 files changed, 697 insertions, 90 deletions
diff --git a/docs/Config.md b/docs/Config.md index 22f2a8e9b..df9003d23 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -96,9 +96,12 @@ git: parseEmoji: false diffContextSize: 3 # how many lines of context are shown around a change in diffs os: - editCommand: '' # see 'Configuring File Editing' section - editCommandTemplate: '' - openCommand: '' + editPreset: '' # see 'Configuring File Editing' section + edit: '' + editAtLine: '' + editAtLineAndWait: '' + open: '' + openLink: '' refresher: refreshInterval: 10 # File/submodule refresh interval in seconds. Auto-refresh can be disabled via option 'git.autoRefresh'. fetchInterval: 60 # Re-fetch interval in seconds. Auto-fetch can be disabled via option 'git.autoFetch'. @@ -268,40 +271,41 @@ os: ### Configuring File Editing -Lazygit will edit a file with the first set editor in the following: +There are two commands for opening files, `o` for "open" and `e` for "edit". `o` +acts as if the file was double-clicked in the Finder/Explorer, so it also works +for non-text files, whereas `e` opens the file in an editor. `e` can also jump +to the right line in the file if you invoke it from the staging panel, for +example. -1. config.yaml +To tell lazygit which editor to use for the `e` command, the easiest way to do +that is to provide an editPreset config, e.g. ```yaml os: - editCommand: 'vim' # as an example + editPreset: 'vscode' ``` -2. \$(git config core.editor) -3. \$GIT_EDITOR -4. \$VISUAL -5. \$EDITOR -6. \$(which vi) +Supported presets are `vim`, `emacs`, `nano`, `vscode`, `sublime`, `bbedit`, and +`xcode`. In many cases lazygit will be able to guess the right preset from your +$(git config core.editor), or an environment variable such as $VISUAL or $EDITOR. -Lazygit will log an error if none of these options are set. - -You can specify the current line number when you're in the patch explorer. +If for some reason you are not happy with the default commands from a preset, or +there simply is no preset for your editor, you can customize the commands by +setting the `edit`, `editAtLine`, and `editAtLineAndWait` options, e.g.: ```yaml os: - editCommand: 'vim' - editCommandTemplate: '{{editor}} +{{line}} -- {{filename}}' + edit: 'myeditor {{filename}}' + editAtLine: 'myeditor --line={{line}} {{filename}}' + editAtLineAndWait: 'myeditor --block --line={{line}} {{filename}}' + editInTerminal: true ``` -or - -```yaml -os: - editCommand: 'code' - editCommandTemplate: '{{editor}} --goto -- {{filename}}:{{line}}' -``` +The `editInTerminal` option is used to decide whether lazygit needs to suspend +itself to the background before calling the editor. -`{{editor}}` in `editCommandTemplate` is replaced with the value of `editCommand`. +Contributions of new editor presets are welcome; see the `getPreset` function in +[`editor_presets.go`](https://github.com/jesseduffield/lazygit/blob/master/pkg/config/editor_presets.go). ### Overriding default config file location @@ -317,15 +321,6 @@ or LG_CONFIG_FILE="$HOME/.base_lg_conf,$HOME/.light_theme_lg_conf" lazygit ``` -### Recommended Config Values - -for users of VSCode - -```yaml -os: - openCommand: 'code -rg {{filename}}' -``` - ## Color Attributes For color attributes you can choose an array of attributes (with max one color attribute) diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 111733bb7..94563be65 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -3,8 +3,10 @@ package git_commands import ( "os" "strconv" + "strings" "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -27,7 +29,7 @@ func (self *FileCommands) Cat(fileName string) (string, error) { return string(buf), nil } -func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) { +func (self *FileCommands) GetEditCmdStrLegacy(filename string, lineNumber int) (string, error) { editor := self.UserConfig.OS.EditCommand if editor == "" { @@ -72,3 +74,82 @@ func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string } return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil } + +func (self *FileCommands) GetEditCmdStr(filename string) (string, bool) { + // Legacy support for old config; to be removed at some point + if self.UserConfig.OS.Edit == "" && self.UserConfig.OS.EditCommandTemplate != "" { + if cmdStr, err := self.GetEditCmdStrLegacy(filename, 1); err == nil { + return cmdStr, true + } + } + + template, editInTerminal := config.GetEditTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr, editInTerminal +} + +func (self *FileCommands) GetEditAtLineCmdStr(filename string, lineNumber int) (string, bool) { + // Legacy support for old config; to be removed at some point + if self.UserConfig.OS.EditAtLine == "" && self.UserConfig.OS.EditCommandTemplate != "" { + if cmdStr, err := self.GetEditCmdStrLegacy(filename, lineNumber); err == nil { + return cmdStr, true + } + } + + template, editInTerminal := config.GetEditAtLineTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + "line": strconv.Itoa(lineNumber), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr, editInTerminal +} + +func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber int) string { + // Legacy support for old config; to be removed at some point + if self.UserConfig.OS.EditAtLineAndWait == "" && self.UserConfig.OS.EditCommandTemplate != "" { + if cmdStr, err := self.GetEditCmdStrLegacy(filename, lineNumber); err == nil { + return cmdStr + } + } + + template := config.GetEditAtLineAndWaitTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + "line": strconv.Itoa(lineNumber), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr +} + +func (self *FileCommands) guessDefaultEditor() string { + // Try to query a few places where editors get configured + editor := self.config.GetCoreEditor() + if editor == "" { + editor = self.os.Getenv("GIT_EDITOR") + } + if editor == "" { + editor = self.os.Getenv("VISUAL") + } + if editor == "" { + editor = self.os.Getenv("EDITOR") + } + + if editor != "" { + // At this point, it might be more than just the name of the editor; + // e.g. it might be "code -w" or "vim -u myvim.rc". So assume that + // everything up to the first space is the editor name. + editor = strings.Split(editor, " ")[0] + } + + return editor +} diff --git a/pkg/commands/git_commands/file_test.go b/pkg/commands/git_commands/file_test.go index 4b9128dfe..2367e4fcb 100644 --- a/pkg/commands/git_commands/file_test.go +++ b/pkg/commands/git_commands/file_test.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestEditFileCmdStr(t *testing.T) { +func TestEditFileCmdStrLegacy(t *testing.T) { type scenario struct { filename string configEditCommand string @@ -172,7 +172,206 @@ func TestEditFileCmdStr(t *testing.T) { getenv: s.getenv, }) - s.test(instance.GetEditCmdStr(s.filename, 1)) + s.test(instance.GetEditCmdStrLegacy(s.filename, 1)) s.runner.CheckForMissingCalls() } } + +func TestEditFileCmd(t *testing.T) { + type scenario struct { + filename string + osConfig config.OSConfig + expectedCmdStr string + expectedEditInTerminal bool + } + + scenarios := []scenario{ + { + filename: "test", + osConfig: config.OSConfig{}, + expectedCmdStr: `vim -- "test"`, + expectedEditInTerminal: true, + }, + { + filename: "test", + osConfig: config.OSConfig{ + Edit: "nano {{filename}}", + }, + expectedCmdStr: `nano "test"`, + expectedEditInTerminal: true, + }, + { + filename: "file/with space", + osConfig: config.OSConfig{ + EditPreset: "sublime", + }, + expectedCmdStr: `subl -- "file/with space"`, + expectedEditInTerminal: false, + }, + } + + for _, s := range scenarios { + userConfig := config.GetDefaultConfig() + userConfig.OS = s.osConfig + + instance := buildFileCommands(commonDeps{ + userConfig: userConfig, + }) + + cmdStr, editInTerminal := instance.GetEditCmdStr(s.filename) + assert.Equal(t, s.expectedCmdStr, cmdStr) + assert.Equal(t, s.expectedEditInTerminal, editInTerminal) + } +} + +func TestEditFileAtLineCmd(t *testing.T) { + type scenario struct { + filename string + lineNumber int + osConfig config.OSConfig + expectedCmdStr string + expectedEditInTerminal bool + } + + scenarios := []scenario{ + { + filename: "test", + lineNumber: 42, + osConfig: config.OSConfig{}, + expectedCmdStr: `vim +42 -- "test"`, + expectedEditInTerminal: true, + }, + { + filename: "test", + lineNumber: 35, + osConfig: config.OSConfig{ + EditAtLine: "nano +{{line}} {{filename}}", + }, + expectedCmdStr: `nano +35 "test"`, + expectedEditInTerminal: true, + }, + { + filename: "file/with space", + lineNumber: 12, + osConfig: config.OSConfig{ + EditPreset: "sublime", + }, + expectedCmdStr: `subl -- "file/with space":12`, + expectedEditInTerminal: false, + }, + } + + for _, s := range scenarios { + userConfig := config.GetDefaultConfig() + userConfig.OS = s.osConfig + + instance := buildFileCommands(commonDeps{ + userConfig: userConfig, + }) + + cmdStr, editInTerminal := instance.GetEditAtLineCmdStr(s.filename, s.lineNumber) + assert.Equal(t, s.expectedCmdStr, cmdStr) + assert.Equal(t, s.expectedEditInTerminal, editInTerminal) + } +} + +func TestEditFileAtLineAndWaitCmd(t *testing.T) { + type scenario struct { + filename string + lineNumber int + osConfig config.OSConfig + expectedCmdStr string + } + + scenarios := []scenario{ + { + filename: "test", + lineNumber: 42, + osConfig: config.OSConfig{}, + expectedCmdStr: `vim +42 -- "test"`, + }, + { + filename: "file/with space", + lineNumber: 12, + osConfig: config.OSConfig{ + EditPreset: "sublime", + }, + expectedCmdStr: `subl --wait -- "file/with space":12`, + }, + } + + for _, s := range scenarios { + userConfig := config.GetDefaultConfig() + userConfig.OS = s.osConfig + + instance := buildFileCommands(commonDeps{ + userConfig: userConfig, + }) + + cmdStr := instance.GetEditAtLineAndWaitCmdStr(s.filename, s.lineNumber) + assert.Equal(t, s.expectedCmdStr, cmdStr) + } +} + +func TestGuessDefaultEditor(t *testing.T) { + type scenario struct { + gitConfigMockResponses map[string]string + getenv func(string) string + expectedResult string + } + + scenarios := []scenario{ + { + gitConfigMockResponses: nil, + getenv: func(env string) string { + return "" + }, + expectedResult: "", + }, + { + gitConfigMockResponses: map[string]string{"core.editor": "nano"}, + getenv: func(env string) string { + return "" + }, + expectedResult: "nano", + }, + { + gitConfigMockResponses: map[string]string{"core.editor": "code -w"}, + getenv: func(env string) string { + return "" + }, + expectedResult: "code", + }, + { + gitConfigMockResponses: nil, + getenv: func(env string) string { + if env == "VISUAL" { + return "emacs" + } + + return "" + }, + expectedResult: "emacs", + }, + { + gitConfigMockResponses: nil, + getenv: func(env string) string { + if env == "EDITOR" { + return "bbedit -w" + } + + return "" + }, + expectedResult: "bbedit", + }, + } + + for _, s := range scenarios { + instance := buildFileCommands(commonDeps{ + gitConfig: git_config.NewFakeGitConfig(s.gitConfigMockResponses), + getenv: s.getenv, + }) + + assert.Equal(t, s.expectedResult, instance.guessDefaultEditor()) + } +} diff --git a/pkg/commands/oscommands/os.go b/pkg/commands/oscommands/os.go index 39149ce84..c8f38843b 100644 --- a/pkg/commands/oscommands/os.go +++ b/pkg/commands/oscommands/os.go @@ -78,21 +78,30 @@ func FileType(path string) string { } func (c *OSCommand) OpenFile(filename string) error { - return c.OpenFileAtLine(filename, 1) -} - -func (c *OSCommand) OpenFileAtLine(filename string, lineNumber int) error { - commandTemplate := c.UserConfig.OS.OpenCommand + commandTemplate := c.UserConfig.OS.Open + if commandTemplate == "" { + // Legacy support + commandTemplate = c.UserConfig.OS.OpenCommand + } + if commandTemplate == "" { + commandTemplate = config.GetPlatformDefaultConfig().Open + } templateValues := map[string]string{ "filename": c.Quote(filename), - "line": fmt.Sprintf("%d", lineNumber), } command := utils.ResolvePlaceholderString(commandTemplate, templateValues) return c.Cmd.NewShell(command).Run() } func (c *OSCommand) OpenLink(link string) error { - commandTemplate := c.UserConfig.OS.OpenLinkCommand + commandTemplate := c.UserConfig.OS.OpenLink + if commandTemplate == "" { + // Legacy support + commandTemplate = c.UserConfig.OS.OpenLinkCommand + } + if commandTemplate == "" { + commandTemplate = config.GetPlatformDefaultConfig().OpenLink + } templateValues := map[string]string{ "link": c.Quote(link), } diff --git a/pkg/commands/oscommands/os_test_default.go b/pkg/commands/oscommands/os_default_test.go index 39a1226d2..1f534f6d3 100644 --- a/pkg/commands/oscommands/os_test_default.go +++ b/pkg/commands/oscommands/os_default_test.go @@ -75,7 +75,7 @@ func TestOSCommandOpenFileDarwin(t *testing.T) { for _, s := range scenarios { oSCmd := NewDummyOSCommandWithRunner(s.runner) oSCmd.Platform.OS = "darwin" - oSCmd.UserConfig.OS.OpenCommand = "open {{filename}}" + oSCmd.UserConfig.OS.Open = "open {{filename}}" s.test(oSCmd.OpenFile(s.filename)) } @@ -135,7 +135,7 @@ func TestOSCommandOpenFileLinux(t *testing.T) { for _, s := range scenarios { oSCmd := NewDummyOSCommandWithRunner(s.runner) oSCmd.Platform.OS = "linux" - oSCmd.UserConfig.OS.OpenCommand = `xdg-open {{filename}} > /dev/null` + oSCmd.UserConfig.OS.Open = `xdg-open {{filename}} > /dev/null` s.test(oSCmd.OpenFile(s.filename)) } diff --git a/pkg/commands/oscommands/os_test_windows.go b/pkg/commands/oscommands/os_windows_test.go index cf9c1e68a..acb971b59 100644 --- a/pkg/commands/oscommands/os_test_windows.go +++ b/pkg/commands/oscommands/os_windows_test.go @@ -6,6 +6,7 @@ package oscommands import ( "testing" + "github.com/cli/safeexec" "github.com/go-errors/errors" "github.com/stretchr/testify/assert" ) @@ -19,11 +20,13 @@ func TestOSCommandOpenFileWindows(t *testing.T) { test func(error) } + fullCmdPath, _ := safeexec.LookPath("cmd") + scenarios := []scenario{ { filename: "test", runner: NewFakeRunner(t). - ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", errors.New("error")), + ExpectArgs([]string{fullCmdPath, "/c", "start", "", "test"}, "", errors.New("error")), test: func(err error) { assert.Error(t, err) }, @@ -31,7 +34,7 @@ func TestOSCommandOpenFileWindows(t *testing.T) { { filename: "test", runner: NewFakeRunner(t). - ExpectArgs([]string{"cmd", "/c", "start", "", "test"}, "", nil), + ExpectArgs([]string{fullCmdPath, "/c", "start", "", "test"}, "", nil), test: func(err error) { assert.NoError(t, err) }, @@ -39,7 +42,7 @@ func TestOSCommandOpenFileWindows(t *testing.T) { { filename: "filename with spaces", runner: NewFakeRunner(t). - ExpectArgs([]string{"cmd", "/c", "start", "", "filename with spaces"}, "", nil), + ExpectArgs([]string{fullCmdPath, "/c", "start", "", "filename with spaces"}, "", nil), test: func(err error) { assert.NoError(t, err) }, @@ -47,7 +50,7 @@ func TestOSCommandOpenFileWindows(t *testing.T) { { filename: "let's_test_with_single_quote", runner: NewFakeRunner(t). - ExpectArgs([]string{"cmd", "/c", "start", "", "let's_test_with_single_quote"}, "", nil), + ExpectArgs([]string{fullCmdPath, "/c", "start", "", "let's_test_with_single_quote"}, "", nil), test: func(err error) { assert.NoError(t, err) }, @@ -55,7 +58,7 @@ func TestOSCommandOpenFileWindows(t *testing.T) { { filename: "$USER.txt", runner: NewFakeRunner(t). - ExpectArgs([]string{"cmd", "/c", "start", "", "$USER.txt"}, "", nil), + ExpectArgs([]string{fullCmdPath, "/c", "start", "", "$USER.txt"}, "", nil), test: func(err error) { assert.NoError(t, err) }, diff --git a/pkg/config/config_default_platform.go b/pkg/config/config_default_platform.go index 6784f0ce2..d05201454 100644 --- a/pkg/config/config_default_platform.go +++ b/pkg/config/config_default_platform.go @@ -6,9 +6,7 @@ package config // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ - EditCommand: ``, - EditCommandTemplate: "", - OpenCommand: "open -- {{filename}}", - OpenLinkCommand: "open {{link}}", + Open: "open -- {{filename}}", + OpenLink: "open {{link}}", } } diff --git a/pkg/config/config_linux.go b/pkg/config/config_linux.go index 06c277061..7f6187ee3 100644 --- a/pkg/config/config_linux.go +++ b/pkg/config/config_linux.go @@ -29,17 +29,13 @@ func isContainer() bool { func GetPlatformDefaultConfig() OSConfig { if isWSL() && !isContainer() { return OSConfig{ - EditCommand: ``, - EditCommandTemplate: "", - OpenCommand: `powershell.exe start explorer.exe {{filename}} >/dev/null`, - OpenLinkCommand: `powershell.exe start {{link}} >/dev/null`, + Open: `powershell.exe start explorer.exe {{filename}} >/dev/null`, + OpenLink: `powershell.exe start {{link}} >/dev/null`, } } return OSConfig{ - EditCommand: ``, - EditCommandTemplate: "", - OpenCommand: `xdg-open {{filename}} >/dev/null`, - OpenLinkCommand: `xdg-open {{link}} >/dev/null`, + Open: `xdg-open {{filename}} >/dev/null`, + OpenLink: `xdg-open {{link}} >/dev/null`, } } diff --git a/pkg/config/config_windows.go b/pkg/config/config_windows.go index 12ecb8dff..96a810b50 100644 --- a/pkg/config/config_windows.go +++ b/pkg/config/config_windows.go @@ -3,9 +3,7 @@ package config // GetPlatformDefaultConfig gets the defaults for the platform func GetPlatformDefaultConfig() OSConfig { return OSConfig{ - EditCommand: ``, - EditCommandTemplate: "", - OpenCommand: `start "" {{filename}}`, - OpenLinkCommand: `start "" {{link}}`, + Open: `start "" {{filename}}`, + OpenLink: `start "" {{link}}`, } } diff --git a/pkg/config/editor_presets.go b/pkg/config/editor_presets.go new file mode 100644 index 000000000..146abe0f5 --- /dev/null +++ b/pkg/config/editor_presets.go @@ -0,0 +1,109 @@ +package config + +func GetEditTemplate(osConfig *OSConfig, guessDefaultEditor func() string) (string, bool) { + preset := getPreset(osConfig, guessDefaultEditor) + template := osConfig.Edit + if template == "" { + template = preset.editTemplate + } + + return template, getEditInTerminal(osConfig, preset) +} + +func GetEditAtLineTemplate(osConfig *OSConfig, guessDefaultEditor func() string) (string, bool) { + preset := getPreset(osConfig, guessDefaultEditor) + template := osConfig.EditAtLine + if template == "" { + template = preset.editAtLineTemplate + } + return template, getEditInTerminal(osConfig, preset) +} + +func GetEditAtLineAndWaitTemplate(osConfig *OSConfig, guessDefaultEditor func() string) string { + preset := getPreset(osConfig, guessDefaultEditor) + template := osConfig.EditAtLineAndWait + if template == "" { + template = preset.editAtLineAndWaitTemplate + } + return template +} + +type editPreset struct { + editTemplate string + editAtLineTemplate string + editAtLineAndWaitTemplate string + editInTerminal bool +} + +func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset { + presets := map[string]*editPreset{ + "vi": standardTerminalEditorPreset("vi"), + "vim": standardTerminalEditorPreset("vim"), + "nvim": standardTerminalEditorPreset("nvim"), + "emacs": standardTerminalEditorPreset("emacs"), + "nano": standardTerminalEditorPreset("nano"), + "vscode": { + editTemplate: "code --reuse-window -- {{filename}}", + editAtLineTemplate: "code --reuse-window --goto -- {{filename}}:{{line}}", + editAtLineAndWaitTemplate: "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + editInTerminal: false, + }, + "sublime": { + editTemplate: "subl -- {{filename}}", + editAtLineTemplate: "subl -- {{filename}}:{{line}}", + editAtLineAndWaitTemplate: "subl --wait -- {{filename}}:{{line}}", + editInTerminal: false, + }, + "bbedit": { + editTemplate: "bbedit -- {{filename}}", + editAtLineTemplate: "bbedit +{{line}} -- {{filename}}", + editAtLineAndWaitTemplate: "bbedit +{{line}} --wait -- {{filename}}", + editInTerminal: false, + }, + "xcode": { + editTemplate: "xed -- {{filename}}", + editAtLineTemplate: "xed --line {{line}} -- {{filename}}", + editAtLineAndWaitTemplate: "xed --line {{line}} --wait -- {{filename}}", + editInTerminal: false, + }, + } + + // Some of our presets have a different name than the editor they are using. + editorToPreset := map[string]string{ + "code": "vscode", + "subl": "sublime", + "xed": "xcode", + } + + presetName := osConfig.EditPreset + if presetName == "" { + defaultEditor := guessDefaultEditor() + if presets[defaultEditor] != nil { + presetName = defaultEditor + } else if p := editorToPreset[defaultEditor]; p != "" { + presetName = p + } + } + + if presetName == "" || presets[presetName] == nil { + presetName = "vim" + } + + return presets[presetName] +} + +func standardTerminalEditorPreset(editor string) *editPreset { + return &editPreset{ + editTemplate: editor + " -- {{filename}}", + editAtLineTemplate: editor + " +{{line}} -- {{filename}}", + editAtLineAndWaitTemplate: editor + " +{{line}} -- {{filename}}", + editInTerminal: true, + } +} + +func getEditInTerminal(osConfig *OSConfig, preset *editPreset) bool { + if osConfig.EditInTerminal != nil { + return *osConfig.EditInTerminal + } + return preset.editInTerminal +} diff --git a/pkg/config/editor_presets_test.go b/pkg/config/editor_presets_test.go new file mode 100644 index 000000000..1d73df8c6 --- /dev/null +++ b/pkg/config/editor_presets_test.go @@ -0,0 +1,126 @@ +package config + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetEditTemplate(t *testing.T) { + trueVal := true + + scenarios := []struct { + name string + osConfig *OSConfig + guessDefaultEditor func() string + expectedEditTemplate string + expectedEditAtLineTemplate string + expectedEditAtLineAndWaitTemplate string + expectedEditInTerminal bool + }{ + { + "Default template is vim", + &OSConfig{}, + func() string { return "" }, + "vim -- {{filename}}", + "vim +{{line}} -- {{filename}}", + "vim +{{line}} -- {{filename}}", + true, + }, + { + "Setting a preset", + &OSConfig{ + EditPreset: "vscode", + }, + func() string { return "" }, + "code --reuse-window -- {{filename}}", + "code --reuse-window --goto -- {{filename}}:{{line}}", + "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + false, + }, + { + "Setting a preset wins over guessed editor", + &OSConfig{ + EditPreset: "vscode", + }, + func() string { return "nano" }, + "code --reuse-window -- {{filename}}", + "code --reuse-window --goto -- {{filename}}:{{line}}", + "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + false, + }, + { + "Overriding a preset with explicit config (edit)", + &OSConfig{ + EditPreset: "vscode", + Edit: "myeditor {{filename}}", + EditInTerminal: &trueVal, + }, + func() string { return "" }, + "myeditor {{filename}}", + "code --reuse-window --goto -- {{filename}}:{{line}}", + "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + true, + }, + { + "Overriding a preset with explicit config (edit at line)", + &OSConfig{ + EditPreset: "vscode", + EditAtLine: "myeditor --line={{line}} {{filename}}", + EditInTerminal: &trueVal, + }, + func() string { return "" }, + "code --reuse-window -- {{filename}}", + "myeditor --line={{line}} {{filename}}", + "code --reuse-window --goto --wait -- {{filename}}:{{line}}", + true, + }, + { + "Overriding a preset with explicit config (edit at line and wait)", + &OSConfig{ + EditPreset: "vscode", + EditAtLineAndWait: "myeditor --line={{line}} -w {{filename}}", + EditInTerminal: &trueVal, + }, + func() string { return "" }, + "code --reuse-window -- {{filename}}", + "code --reuse-window --goto -- {{filename}}:{{line}}", + "myeditor --line={{line}} -w {{filename}}", + true, + }, + { + "Unknown preset name", + &OSConfig{ + EditPreset: "thisPresetDoesNotExist", + }, + func() string { return "" }, + "vim -- {{filename}}", + "vim +{{line}} -- {{filename}}", + "vim +{{line}} -- {{filename}}", + true, + }, + { + "Guessing a preset from guessed editor", + &OSConfig{}, + func() string { return "emacs" }, + "emacs -- {{filename}}", + "emacs +{{line}} -- {{filename}}", + "emacs +{{line}} -- {{filename}}", + true, + }, + } + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + template, editInTerminal := GetEditTemplate(s.osConfig, s.guessDefaultEditor) + assert.Equal(t, s.expectedEditTemplate, template) + assert.Equal(t, s.expectedEditInTerminal, editInTermina |