From 061256662c1a1e64fc717d0013dd85690eec6403 Mon Sep 17 00:00:00 2001 From: thefux Date: Tue, 6 Sep 2022 15:34:26 +0200 Subject: [PATCH] #60 small refactoring: update teardown cleanup process --- lua/lazygit.lua | 12 ++++++++++-- lua/lazygit/window.lua | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lua/lazygit.lua b/lua/lazygit.lua index 7a773a1..9051ab8 100644 --- a/lua/lazygit.lua +++ b/lua/lazygit.lua @@ -9,6 +9,8 @@ LAZYGIT_BUFFER = nil LAZYGIT_LOADED = false vim.g.lazygit_opened = 0 local prev_win = -1 +local win = -1 +local buffer = -1 --- on_exit callback function to delete the open buffer when lazygit exits in a neovim terminal local function on_exit(job_id, code, event) @@ -16,14 +18,20 @@ local function on_exit(job_id, code, event) return end - vim.cmd('silent! :q') LAZYGIT_BUFFER = nil LAZYGIT_LOADED = false vim.g.lazygit_opened = 0 vim.cmd('silent! :checktime') + if vim.api.nvim_win_is_valid(prev_win) then + vim.api.nvim_win_close(win, true) vim.api.nvim_set_current_win(prev_win) prev_win = -1 + if vim.api.nvim_buf_is_valid(buffer) and vim.api.nvim_buf_is_loaded(buffer) then + vim.api.nvim_buf_delete(buffer, { force = true }) + end + buffer = -1 + win = -1 end end @@ -47,7 +55,7 @@ local function lazygit(path) prev_win = vim.api.nvim_get_current_win() - open_floating_window() + win, buffer = open_floating_window() local cmd = 'lazygit' diff --git a/lua/lazygit/window.lua b/lua/lazygit/window.lua index 9d2a5eb..952ed1d 100644 --- a/lua/lazygit/window.lua +++ b/lua/lazygit/window.lua @@ -65,7 +65,7 @@ local function open_floating_window() LAZYGIT_LOADED = true end -- create file window, enter the window, and use the options defined in opts - local _ = api.nvim_open_win(LAZYGIT_BUFFER, true, opts) + local win = api.nvim_open_win(LAZYGIT_BUFFER, true, opts) vim.bo[LAZYGIT_BUFFER].filetype = 'lazygit' @@ -78,6 +78,8 @@ local function open_floating_window() vim.cmd(cmd) cmd = [[autocmd WinLeave silent! execute 'silent bdelete! %s']] vim.cmd(cmd:format(border_buffer)) + + return win, border_window end return {