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

@@ -112,8 +112,9 @@ require('lazy').setup({
'rafamadriz/friendly-snippets', 'rafamadriz/friendly-snippets',
}, },
}, },
-- Useful plugin to show you pending keybinds. -- Useful plugin to show you pending keybinds.
{ 'folke/which-key.nvim', opts = {} }, { 'folke/which-key.nvim', opts = {} },
{ {
-- Adds git related signs to the gutter, as well as utilities for managing changes -- Adds git related signs to the gutter, as well as utilities for managing changes
'lewis6991/gitsigns.nvim', 'lewis6991/gitsigns.nvim',
@@ -338,7 +339,7 @@ vim.keymap.set('n', '<leader>t', '<Cmd>tabnew +term<CR>i')
-- Create an empty buffer in a new tab -- Create an empty buffer in a new tab
vim.keymap.set('n', '<Leader>e', function() vim.keymap.set('n', '<Leader>e', function()
vim.cmd 'tabnew' -- create a new tab vim.cmd 'tabnew' -- create a new tab
vim.cmd 'enew' -- create a new empty buffer in it vim.cmd 'enew' -- create a new empty buffer in it
end, { noremap = true, silent = true }) end, { noremap = true, silent = true })
-- Save current buffer (asks for filename if new/unsaved) -- Save current buffer (asks for filename if new/unsaved)
@@ -434,6 +435,71 @@ require('telescope').setup {
-- Enable telescope fzf native, if installed -- Enable telescope fzf native, if installed
pcall(require('telescope').load_extension, 'fzf') pcall(require('telescope').load_extension, 'fzf')
-- smart_open function for Telescope to check if the current tab has an empty "No Name" buffer. If it has, it replaces the empty buffer and open a file in the same tab
local actions = require 'telescope.actions'
local action_state = require 'telescope.actions.state'
local function smart_open(prompt_bufnr)
local entry = action_state.get_selected_entry()
if not entry then
return
end
local path = entry.path or entry.filename
if not path then
return
end
if prompt_bufnr and vim.api.nvim_buf_is_valid(prompt_bufnr) then
pcall(actions.close, prompt_bufnr)
end
-- 1. If file is already open → jump to it
local tabpages = vim.api.nvim_list_tabpages()
for _, tab in ipairs(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_get_name(buf) == path then
vim.api.nvim_set_current_tabpage(tab) -- jump to tab
vim.api.nvim_set_current_win(win) -- jump to window
return
end
end
end
-- 2. If current tab has an empty "No Name" buffer → reuse it
local wins = vim.api.nvim_tabpage_list_wins(0)
for _, win in ipairs(wins) do
local buf = vim.api.nvim_win_get_buf(win)
local name = 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 name == '' and buftype == '' and not modified then
vim.api.nvim_set_current_win(win)
vim.cmd('edit ' .. vim.fn.fnameescape(path))
return
end
end
-- 3. Otherwise → open in a new tab
vim.cmd('tabnew ' .. vim.fn.fnameescape(path))
end
-- Telescope keymap using Smart Open
vim.keymap.set('n', '<leader>sf', function()
require('telescope.builtin').find_files {
attach_mappings = function(_, map)
map('i', '<CR>', function(prompt_bufnr)
smart_open(prompt_bufnr)
end)
map('n', '<CR>', function(prompt_bufnr)
smart_open(prompt_bufnr)
end)
return true
end,
}
end, { desc = '[S]earch [F]iles (Smart Open)' })
-- Telescope live_grep in git root -- Telescope live_grep in git root
-- Function to find the git root directory based on the current buffer's path -- Function to find the git root directory based on the current buffer's path
local function find_git_root() local function find_git_root()
@@ -490,7 +556,6 @@ end
vim.keymap.set('n', '<leader>s/', telescope_live_grep_open_files, { desc = '[S]earch [/] in Open Files' }) vim.keymap.set('n', '<leader>s/', telescope_live_grep_open_files, { desc = '[S]earch [/] in Open Files' })
vim.keymap.set('n', '<leader>ss', require('telescope.builtin').builtin, { desc = '[S]earch [S]elect Telescope' }) vim.keymap.set('n', '<leader>ss', require('telescope.builtin').builtin, { desc = '[S]earch [S]elect Telescope' })
vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' }) vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' }) vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' }) vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' }) vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })

View File

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