Skip to content

Commit f40c33e

Browse files
committed
fix(lazygit): nvim opening over itself
Makes workarounds like this no longer necessary: https://github.com/BirdeeHub/nix-wrapper-modules/blob/48b4a74b16a800f57c4ef4c1b10cfe46bb7847d3/templates/neovim/init.lua#L306-L347
1 parent fe7cfe9 commit f40c33e

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

lua/snacks/lazygit.lua

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ M.meta = {
1010
desc = "Open LazyGit in a float, auto-configure colorscheme and integration with Neovim",
1111
}
1212

13+
---Handles the backend of closing lazygit and opening nvim to a particular file and line
14+
---@param path string
15+
---@param line integer?
16+
function M.handle_remote(path, line)
17+
local prev = vim.fn.bufnr("#")
18+
local prev_win = vim.fn.bufwinid(prev)
19+
vim.api.nvim_feedkeys("q", "n", false)
20+
if line then
21+
vim.api.nvim_buf_call(prev, function()
22+
vim.cmd.edit(path)
23+
local buf = vim.api.nvim_get_current_buf()
24+
vim.schedule(function()
25+
if buf then
26+
vim.api.nvim_win_set_buf(prev_win, buf)
27+
vim.api.nvim_win_set_cursor(0, { line or 0, 0})
28+
end
29+
end)
30+
end)
31+
else
32+
vim.api.nvim_buf_call(prev, function()
33+
vim.cmd.edit(path)
34+
local buf = vim.api.nvim_get_current_buf()
35+
vim.schedule(function()
36+
if buf then
37+
vim.api.nvim_win_set_buf(prev_win, buf)
38+
end
39+
end)
40+
end)
41+
end
42+
end
43+
1344
---@alias snacks.lazygit.Color {fg?:string, bg?:string, bold?:boolean}
1445

1546
---@class snacks.lazygit.Theme: table<number, snacks.lazygit.Color>
@@ -34,7 +65,13 @@ local defaults = {
3465
-- snacks does NOT have a full yaml parser, so if you need `"test"` to appear with the quotes
3566
-- you need to double quote it: `"\"test\""`
3667
config = {
37-
os = { editPreset = "nvim-remote" },
68+
os = {
69+
-- This prevents nvim from opening over itself and requiring closing twice in scenarios where nvim command launches something other than the current neovim program
70+
editPreset = "nvim-remote",
71+
edit = vim.v.progpath .. [[ --server "$NVIM" --remote-send '<cmd>lua require("snacks").lazygit.handle_remote({{filename}})<CR>']],
72+
editAtLine = vim.v.progpath .. [[ --server "$NVIM" --remote-send '<cmd>lua require("snacks").lazygit.handle_remote({{filename}}, {{line}})<CR>']],
73+
openDirInEditor = vim.v.progpath .. [[ --server "$NVIM" --remote-send '<cmd>lua require("snacks").lazygit.handle_remote({{dir}})<CR>']],
74+
},
3875
gui = {
3976
-- set to an empty string "" to disable icons
4077
nerdFontsVersion = "3",

scripts/docs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#!/bin/env bash
1+
#!/usr/bin/env bash
22

33
nvim -u tests/minit.lua --headless +'lua require("snacks.meta.docs").build()' +qa

scripts/docs-post

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#!/bin/env bash
1+
#!/usr/bin/env bash
22

33
nvim -u tests/minit.lua --headless +'lua require("snacks.meta.docs").fix_titles()' +qa

0 commit comments

Comments
 (0)