Skip to content

Commit feb5ca5

Browse files
committed
On blocking edit switch back to the previous tab (when the edit happend in another tab)
1 parent 912c95a commit feb5ca5

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

lua/server/server_functions.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ require("common.common_functions")
33
local response_sock = nil
44
local unception_quitpre_autocmd_id = nil
55
local unception_bufunload_autocmd_id = nil
6+
local unception_tabclosed_autocmd_id = nil
67
local filepath_to_check = nil
78
local blocked_terminal_buffer_id = nil
89
local last_replaced_buffer_id = nil
10+
local last_tabpage = nil
911

1012
local function unblock_client_and_reset_state()
1113
-- Remove the autocmds we made.
@@ -25,6 +27,11 @@ local function unblock_client_and_reset_state()
2527
last_replaced_buffer_id = nil
2628
end
2729

30+
local function unception_reset_last_tab()
31+
vim.api.nvim_del_autocmd(unception_tabclosed_autocmd_id)
32+
last_tabpage = nil
33+
end
34+
2835
function _G.unception_handle_bufunload(unloaded_buffer_filepath)
2936
unloaded_buffer_filepath = unception_get_absolute_filepath(unloaded_buffer_filepath)
3037
unloaded_buffer_filepath = unception_escape_special_chars(unloaded_buffer_filepath)
@@ -50,6 +57,14 @@ function _G.unception_handle_quitpre(quitpre_buffer_filepath)
5057
end
5158
end
5259

60+
function _G.unception_handle_tabclosed()
61+
-- only switch tab when needed this will depend on the config
62+
if last_tabpage ~= nil then
63+
vim.api.nvim_set_current_tabpage(last_tabpage)
64+
end
65+
unception_reset_last_tab()
66+
end
67+
5368
function _G.unception_notify_when_done_editing(pipe_to_respond_on, filepath)
5469
filepath_to_check = filepath
5570
blocked_terminal_buffer_id = last_replaced_buffer_id
@@ -60,6 +75,12 @@ function _G.unception_notify_when_done_editing(pipe_to_respond_on, filepath)
6075
-- Create an autocmd for BufUnload as a failsafe should QuitPre not get triggered on the target buffer (e.g. if a user runs :bdelete).
6176
unception_bufunload_autocmd_id = vim.api.nvim_create_autocmd("BufUnload",
6277
{ command = "lua unception_handle_bufunload(vim.fn.expand('<afile>:p'))" })
78+
79+
-- When done editing in another tab we can't use QuitPre becuase if we switch
80+
-- back to the previous tabpage within QuitPre it will prevent the tab to be closed
81+
-- So here we use the TabClosed event
82+
unception_tabclosed_autocmd_id = vim.api.nvim_create_autocmd("TabClosed",
83+
{ callback = unception_handle_tabclosed })
6384
end
6485

6586
local open_methods_table = {
@@ -136,6 +157,9 @@ function _G.unception_edit_files(file_args, options)
136157
local open_method = unception_detect_open_method(options)
137158

138159
if (#file_args > 0) then
160+
if options.open_in_new_tab or open_method == "tabnew" then
161+
last_tabpage = vim.api.nvim_get_current_tabpage()
162+
end
139163
-- if argadd is selected but we have only one file
140164
-- let's not use argadd so we can use line number specifier
141165
if (open_method ~= "argadd" or #file_args == 1) then

0 commit comments

Comments
 (0)