summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorCharlie Moog <moogcharlie@gmail.com>2024-05-19 13:54:36 +1000
committerJesse Duffield <jessedduffield@gmail.com>2024-05-19 14:39:02 +1000
commit615ff09afacb8f6d385b793eb22364bcb3cf3b44 (patch)
tree3db169d00312f99bad4f6052ff81e1e7f7c3d9a8 /pkg
parent772388294a1dc9511d6ed0373f835e5f35f3a4e9 (diff)
improve `nvim-remote` mode
- If _not_ inside a neovim session, treat as a normal nvim session and suspend lazygit. - If inside a neovim session: - Do not try to suspend lazygit. - Send `q` keystroke to neovim session to quit lazygit. - Send filename/line/etc. to neovim session.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/config/editor_presets.go33
1 files changed, 20 insertions, 13 deletions
diff --git a/pkg/config/editor_presets.go b/pkg/config/editor_presets.go
index a718e6e17..ca784c135 100644
--- a/pkg/config/editor_presets.go
+++ b/pkg/config/editor_presets.go
@@ -1,5 +1,7 @@
package config
+import "os"
+
func GetEditTemplate(osConfig *OSConfig, guessDefaultEditor func() string) (string, bool) {
preset := getPreset(osConfig, guessDefaultEditor)
template := osConfig.Edit
@@ -42,9 +44,11 @@ type editPreset struct {
editAtLineTemplate string
editAtLineAndWaitTemplate string
openDirInEditorTemplate string
- suspend bool
+ suspend func() bool
}
+func returnBool(a bool) func() bool { return (func() bool { return a }) }
+
// IF YOU ADD A PRESET TO THIS FUNCTION YOU MUST UPDATE THE `Supported presets` SECTION OF docs/Config.md
func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset {
presets := map[string]*editPreset{
@@ -52,12 +56,15 @@ func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset
"vim": standardTerminalEditorPreset("vim"),
"nvim": standardTerminalEditorPreset("nvim"),
"nvim-remote": {
- editTemplate: `nvim --server "$NVIM" --remote-tab {{filename}}`,
- editAtLineTemplate: `nvim --server "$NVIM" --remote-tab {{filename}}; [ -z "$NVIM" ] || nvim --server "$NVIM" --remote-send ":{{line}}<CR>"`,
+ editTemplate: `[ -z "$NVIM" ] && (nvim -- {{filename}}) || (nvim --server "$NVIM" --remote-send "q" && nvim --server "$NVIM" --remote-tab {{filename}})`,
+ editAtLineTemplate: `[ -z "$NVIM" ] && (nvim +{{line}} -- {{filename}}) || (nvim --server "$NVIM" --remote-send "q" && nvim --server "$NVIM" --remote-tab {{filename}} && nvim --server "$NVIM" --remote-send ":{{line}}<CR>")`,
// No remote-wait support yet. See https://github.com/neovim/neovim/pull/17856
editAtLineAndWaitTemplate: `nvim +{{line}} {{filename}}`,
- openDirInEditorTemplate: `nvim --server "$NVIM" --remote-tab {{dir}}`,
- suspend: false,
+ openDirInEditorTemplate: `[ -z "$NVIM" ] && (nvim -- {{dir}}) || (nvim --server "$NVIM" --remote-send "q" && nvim --server "$NVIM" --remote-tab {{dir}})`,
+ suspend: func() bool {
+ _, ok := os.LookupEnv("NVIM")
+ return !ok
+ },
},
"lvim": standardTerminalEditorPreset("lvim"),
"emacs": standardTerminalEditorPreset("emacs"),
@@ -69,42 +76,42 @@ func getPreset(osConfig *OSConfig, guessDefaultEditor func() string) *editPreset
editAtLineTemplate: "helix -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "helix -- {{filename}}:{{line}}",
openDirInEditorTemplate: "helix -- {{dir}}",
- suspend: true,
+ suspend: returnBool(true),
},
"helix (hx)": {
editTemplate: "hx -- {{filename}}",
editAtLineTemplate: "hx -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "hx -- {{filename}}:{{line}}",
openDirInEditorTemplate: "hx -- {{dir}}",
- suspend: true,
+ suspend: returnBool(true),
},
"vscode": {
editTemplate: "code --reuse-window -- {{filename}}",
editAtLineTemplate: "code --reuse-window --goto -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "code --reuse-window --goto --wait -- {{filename}}:{{line}}",
openDirInEditorTemplate: "code -- {{dir}}",
- suspend: false,
+ suspend: returnBool(false),
},
"sublime": {
editTemplate: "subl -- {{filename}}",
editAtLineTemplate: "subl -- {{filename}}:{{line}}",
editAtLineAndWaitTemplate: "subl --wait -- {{filename}}:{{line}}",
openDirInEditorTemplate: "subl -- {{dir}}",
- suspend: false,
+ suspend: returnBool(false),
},
"bbedit": {
editTemplate: "bbedit -- {{filename}}",
editAtLineTemplate: "bbedit +{{line}} -- {{filename}}",
editAtLineAndWaitTemplate: "bbedit +{{line}} --wait -- {{filename}}",
openDirInEditorTemplate: "bbedit -- {{dir}}",
- suspend: false,
+ suspend: returnBool(false),
},
"xcode": {
editTemplate: "xed -- {{filename}}",
editAtLineTemplate: "xed --line {{line}} -- {{filename}}",
editAtLineAndWaitTemplate: "xed --line {{line}} --wait -- {{filename}}",
openDirInEditorTemplate: "xed -- {{dir}}",
- suspend: false,
+ suspend: returnBool(false),
},
}
@@ -141,7 +148,7 @@ func standardTerminalEditorPreset(editor string) *editPreset {
editAtLineTemplate: editor + " +{{line}} -- {{filename}}",
editAtLineAndWaitTemplate: editor + " +{{line}} -- {{filename}}",
openDirInEditorTemplate: editor + " -- {{dir}}",
- suspend: true,
+ suspend: returnBool(true),
}
}
@@ -149,5 +156,5 @@ func getEditInTerminal(osConfig *OSConfig, preset *editPreset) bool {
if osConfig.SuspendOnEdit != nil {
return *osConfig.SuspendOnEdit
}
- return preset.suspend
+ return preset.suspend()
}