added: smart_split_open function for split windows; so Neovim asks if the user wants to re-open the already open buffer in the current tab as split window or just jump to the existing one

This commit is contained in:
psychhim
2025-10-05 22:12:30 +05:30
parent bb887551fe
commit 8ffc8d8ab6

View File

@@ -85,6 +85,63 @@ return {
end
end
-- Smart_open_split for split windows
local function smart_open_split(state, direction)
local node = state.tree:get_node()
if not node then
return
end
local path = node:get_id()
if node.type == 'directory' then
state.commands.toggle_node(state, node)
return
end
-- Check if file is open in another tab
local open_tab, open_win
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do
local buf = vim.api.nvim_win_get_buf(win)
if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_name(buf) == path then
open_tab = tab
open_win = win
break
end
end
if open_tab then
break
end
end
if open_tab then
local choice = vim.fn.confirm('File is already open in another tab. Open split here anyway?', '&Yes\n&No',
2)
if choice ~= 1 then
-- User chose No → jump to the tab where file is open
vim.api.nvim_set_current_tabpage(open_tab)
vim.api.nvim_set_current_win(open_win)
return
end
-- Else user chose Yes → continue to open split in current tab
end
-- Open split in current tab
if direction == 'v' then
state.commands.open_vsplit(state)
vim.schedule(function()
vim.cmd 'wincmd L'
end)
else
state.commands.open_split(state)
vim.schedule(function()
vim.cmd 'wincmd J'
end)
end
vim.cmd('edit ' .. vim.fn.fnameescape(path))
end
require('neo-tree').setup {
close_if_last_window = true,
popup_border_style = 'rounded',
@@ -101,28 +158,10 @@ return {
mappings = {
['<cr>'] = smart_open,
['v'] = function(state)
local node = state.tree:get_node()
if node.type == 'directory' then
state.commands.toggle_node(state, node)
return
end
local path = node:get_id()
state.commands.open_split(state) -- opens horizontal split (top)
vim.schedule(function()
vim.cmd 'wincmd J' -- move the new split to bottom
end)
smart_open_split(state, 'h')
end,
['h'] = function(state)
local node = state.tree:get_node()
if node.type == 'directory' then
state.commands.toggle_node(state, node)
return
end
local path = node:get_id()
state.commands.open_vsplit(state) -- opens vertical split (left)
vim.schedule(function()
vim.cmd 'wincmd L' -- move the new split to right
end)
smart_open_split(state, 'v')
end,
['t'] = 'noop',
},