Skip to content

Commit

Permalink
neovim: Add lazygit integration
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed Dec 22, 2024
1 parent 0fc300d commit 3639701
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
5 changes: 4 additions & 1 deletion dotfiles/lazygit/.config/lazygit/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ gui:
showFileTree: false
nerdFontsVersion: 3
mouseEvents: false
os:
edit: nvim {{filename}}
editInTerminal: true
customCommands:
- key: 'e'
- key: 'E'
context: files
command: "nvim --server $(dev tmux nvim-socket) --remote {{.SelectedFile.Name | quote}}"
- key: '<c-D>'
Expand Down
11 changes: 11 additions & 0 deletions dotfiles/nvim/.config/nvim/conf/auto/flatten.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require("flatten").setup({
window = {
open = "current",
},
callbacks = {
pre_open = function()
LazygitClose()
end,
},
pipe_path = require("flatten").default_pipe_path,
})
49 changes: 49 additions & 0 deletions dotfiles/nvim/.config/nvim/conf/auto/lazygit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
-- Do not show 'Process exited' message when closing lazygit buffer
local nvim_terminal_augroup = vim.api.nvim_create_augroup('nvim_terminal', { clear = false })

vim.api.nvim_create_autocmd({ 'TermClose' }, {
group = nvim_terminal_augroup,
desc = 'Automatically close buffers after running lazygit',
callback = function(args)
if vim.v.event.status == 0 then
-- TODO: Find a better way to detect lazygit, this also breaks when running :terminal
local info = vim.api.nvim_get_chan_info(vim.bo[args.buf].channel)
local argv = info.argv or {}
-- argv should be { ".../zsh", "-c", "lazygit" }
if #argv == 3 and argv[3] == 'lazygit' then
vim.cmd({ cmd = 'bdelete', args = { args.buf }, bang = true })
end
end
end,
})

-- Open Lazygit in a floating window
function Lazygit()
local width = vim.api.nvim_get_option("columns")
local height = vim.api.nvim_get_option("lines")

local buffer = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buffer, false, {
relative = 'editor',
row = 0,
col = 0,
width = width,
height = height - 1,
style = 'minimal',
zindex = 1000
})

-- override float background color to be the same as the normal background
vim.api.nvim_set_option_value('winhl', 'NormalFloat:Normal', {win = win})

vim.api.nvim_set_current_win(win)
vim.fn.termopen("lazygit")
vim.cmd("startinsert")
end

function LazygitClose()
local win = vim.api.nvim_get_current_win()
local buf = vim.api.nvim_win_get_buf(win)
vim.api.nvim_win_close(win, true)
vim.api.nvim_buf_delete(buf, { force = true })
end
3 changes: 3 additions & 0 deletions dotfiles/nvim/.config/nvim/lua/plugins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ vim.call('plug#begin')
Plug("MunifTanjim/nui.nvim")
Plug("nvim-lua/plenary.nvim")

-- Utilities
Plug("willothy/flatten.nvim") -- Opening files in remote instances

-- LSP
Plug('neovim/nvim-lspconfig')
Plug('j-hui/fidget.nvim') -- Status updates for LSP
Expand Down
1 change: 1 addition & 0 deletions dotfiles/nvim/.config/nvim/lua/shortcuts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ map('<leader>gp', ':<C-U>Gitsigns prev_hunk', 'Jump to previous hunk')
map('<Leader>gq', ':GLoadChanged', 'Git: load modified files into quickfix')
map('<Leader>gr', ':silent ! hub browse', 'Browse current branch in git repository')
map('<leader>gu', ':<C-U>Gitsigns reset_hunk', 'Reset hunk')
map('<leader>gg', ':lua Lazygit()', 'Open Lazygit')

-- lsp
map('<Leader>ld', ':lua vim.lsp.buf.definition()', 'LSP: Goto definition')
Expand Down

0 comments on commit 3639701

Please sign in to comment.