Fixed opening duplicate buffer/file on a new tab from Neo-tree i.e if the file is already open in a tab, trying to opening the same file on neo-tree will redirect to the already opened tab. Fixed some bugs. Added the same new-tab, empty-buffer functionalities to Telescope as well.

This commit is contained in:
psychhim
2025-09-22 00:09:24 +05:30
parent 4fb80c55df
commit 00885ca41c
2 changed files with 165 additions and 75 deletions

View File

@@ -1,86 +1,111 @@
-- Unless you are still migrating, remove the deprecated commands from v1.x
vim.cmd([[ let g:neo_tree_remove_legacy_commands = 1 ]])
vim.cmd [[ let g:neo_tree_remove_legacy_commands = 1 ]]
return {
"nvim-neo-tree/neo-tree.nvim",
version = "*",
'nvim-neo-tree/neo-tree.nvim',
version = '*',
dependencies = {
"nvim-lua/plenary.nvim",
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
"MunifTanjim/nui.nvim",
'nvim-lua/plenary.nvim',
'nvim-tree/nvim-web-devicons',
'MunifTanjim/nui.nvim',
},
config = function()
-- smart_open function: If there's an empty "No Name" buffer, replace it and open files in the same tab
local function smart_open(state)
local node = state.tree:get_node()
if not node then return end
local path = node:get_id()
local function smart_open(state)
local node = state.tree:get_node()
if not node then
return
end
local path = node:get_id()
-- Get the window in the current tab that is NOT Neo-tree
local wins = vim.api.nvim_tabpage_list_wins(0)
local target_buf = nil
for _, win in ipairs(wins) do
local buf = vim.api.nvim_win_get_buf(win)
local bufname = vim.api.nvim_buf_get_name(buf)
local buftype = vim.api.nvim_buf_get_option(buf, "buftype")
local modified = vim.api.nvim_buf_get_option(buf, "modified")
-- find a window with empty, unmodified buffer
if bufname == "" and buftype == "" and not modified then
target_buf = buf
vim.api.nvim_set_current_win(win)
break
end
end
-- Reuse already open buffer in any tab safely
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
if vim.api.nvim_tabpage_is_valid(tab) then
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(tab)) do
if vim.api.nvim_win_is_valid(win) then
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
vim.api.nvim_set_current_tabpage(tab)
vim.api.nvim_set_current_win(win)
-- close Neo-tree if open
for _, w in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_is_valid(w) then
local b = vim.api.nvim_win_get_buf(w)
if vim.api.nvim_buf_is_valid(b) and vim.api.nvim_buf_get_option(b, 'filetype') == 'neo-tree' then
vim.api.nvim_win_close(w, true)
end
end
end
return
end
end
end
end
end
if target_buf then
-- replace the empty buffer
vim.cmd("edit " .. path)
else
-- otherwise open in new tab
require("neo-tree.sources.filesystem.commands").open_tabnew(state)
end
end
-- Reuse empty buffer in current tab
local wins = vim.api.nvim_tabpage_list_wins(0)
local empty_buf = nil
for _, win in ipairs(wins) do
if vim.api.nvim_win_is_valid(win) then
local buf = vim.api.nvim_win_get_buf(win)
if vim.api.nvim_buf_is_valid(buf) then
local bufname = vim.api.nvim_buf_get_name(buf)
local buftype = vim.api.nvim_buf_get_option(buf, 'buftype')
local modified = vim.api.nvim_buf_get_option(buf, 'modified')
if bufname == '' and buftype == '' and not modified then
empty_buf = buf
vim.api.nvim_set_current_win(win)
break
end
end
end
end
if empty_buf then
vim.cmd('edit ' .. vim.fn.fnameescape(path))
else
vim.cmd('tabnew ' .. vim.fn.fnameescape(path))
end
-- Always close Neo-tree window if open
for _, win in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_is_valid(win) then
local buf = vim.api.nvim_win_get_buf(win)
if vim.api.nvim_buf_is_valid(buf) and vim.api.nvim_buf_get_option(buf, 'filetype') == 'neo-tree' then
vim.api.nvim_win_close(win, true)
end
end
end
end
require('neo-tree').setup {
-- Open Neo-tree in a floating window in middle at first launch when Neovim is opened inside a directory with "nvim ."
close_if_last_window = true, -- close Neo-tree if it's the last window
popup_border_style = "rounded",
enable_git_status = true,
enable_diagnostics = true,
default_component_configs = {
indent = {
padding = 1,
indent_size = 2,
},
icon = {
folder_closed = "",
folder_open = "",
folder_empty = "",
},
},
window = {
position = "float", -- this makes Neo-tree open in the middle
width = 40,
mapping_options = {
noremap = true,
nowait = true,
},
mappings = {
["<cr>"] = smart_open, -- Enter → always open file in new tab if there's no empty buffer
["t"] = "noop", -- disable t
},
},
filesystem = {
follow_current_file = true,
use_libuv_file_watcher = true,
hijack_netrw_behavior = "open_default",
filtered_items = {
visible = true, -- This is what you want: If you set this to `true`, all "hide" just mean "dimmed out"
close_if_last_window = true,
popup_border_style = 'rounded',
enable_git_status = true,
enable_diagnostics = true,
default_component_configs = {
indent = { padding = 1, indent_size = 2 },
icon = { folder_closed = '', folder_open = '', folder_empty = '' },
},
window = {
position = 'float',
width = 40,
mapping_options = { noremap = true, nowait = true },
mappings = {
['<cr>'] = smart_open,
['t'] = 'noop',
},
},
filesystem = {
follow_current_file = true,
use_libuv_file_watcher = true,
hijack_netrw_behavior = 'open_default',
filtered_items = {
visible = true,
hide_dotfiles = false,
hide_gitignored = true,
},
},
}
},
},
}
end,
}