mirror of
https://github.com/hyzendust/KickestEnd.nvim.git
synced 2026-02-15 17:11:11 +01:00
Completely separate Telescope file with it's keymaps. Refactor <leader><leader> for open buffers. Same smart_open with <leader>? for old files.
This commit is contained in:
@@ -37,22 +37,18 @@ return {
|
|||||||
-- 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
|
-- 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 actions = require 'telescope.actions'
|
||||||
local action_state = require 'telescope.actions.state'
|
local action_state = require 'telescope.actions.state'
|
||||||
|
|
||||||
local function smart_open(prompt_bufnr)
|
local function smart_open(prompt_bufnr)
|
||||||
local entry = action_state.get_selected_entry()
|
local entry = action_state.get_selected_entry()
|
||||||
if not entry then
|
if not entry then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local path = entry.path or entry.filename
|
local path = entry.path or entry.filename
|
||||||
if not path then
|
if not path then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if prompt_bufnr and vim.api.nvim_buf_is_valid(prompt_bufnr) then
|
if prompt_bufnr and vim.api.nvim_buf_is_valid(prompt_bufnr) then
|
||||||
pcall(actions.close, prompt_bufnr)
|
pcall(actions.close, prompt_bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 1. If file is already open → jump to it
|
-- 1. If file is already open → jump to it
|
||||||
local tabpages = vim.api.nvim_list_tabpages()
|
local tabpages = vim.api.nvim_list_tabpages()
|
||||||
for _, tab in ipairs(tabpages) do
|
for _, tab in ipairs(tabpages) do
|
||||||
@@ -65,7 +61,6 @@ return {
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 2. If current tab has an empty "No Name" buffer → reuse it
|
-- 2. If current tab has an empty "No Name" buffer → reuse it
|
||||||
local wins = vim.api.nvim_tabpage_list_wins(0)
|
local wins = vim.api.nvim_tabpage_list_wins(0)
|
||||||
for _, win in ipairs(wins) do
|
for _, win in ipairs(wins) do
|
||||||
@@ -79,30 +74,24 @@ return {
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- 3. Otherwise → open in a new tab
|
-- 3. Otherwise → open in a new tab
|
||||||
vim.cmd('tabnew ' .. vim.fn.fnameescape(path))
|
vim.cmd('tabnew ' .. vim.fn.fnameescape(path))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Split option in Telescope file picker with smart_open
|
-- Split option in Telescope file picker with smart_open
|
||||||
local actions = require 'telescope.actions'
|
local actions = require 'telescope.actions'
|
||||||
local action_state = require 'telescope.actions.state'
|
local action_state = require 'telescope.actions.state'
|
||||||
|
|
||||||
local function smart_open_split(prompt_bufnr, split_type)
|
local function smart_open_split(prompt_bufnr, split_type)
|
||||||
local entry = action_state.get_selected_entry()
|
local entry = action_state.get_selected_entry()
|
||||||
if not entry then
|
if not entry then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local path = entry.path or entry.filename
|
local path = entry.path or entry.filename
|
||||||
if not path then
|
if not path then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if prompt_bufnr and vim.api.nvim_buf_is_valid(prompt_bufnr) then
|
if prompt_bufnr and vim.api.nvim_buf_is_valid(prompt_bufnr) then
|
||||||
pcall(actions.close, prompt_bufnr)
|
pcall(actions.close, prompt_bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if file is already open
|
-- Check if file is already open
|
||||||
local open_tab, open_win
|
local open_tab, open_win
|
||||||
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
|
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
|
||||||
@@ -118,7 +107,6 @@ return {
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if open_tab then
|
if open_tab then
|
||||||
local choice = vim.fn.confirm('File is already open in a tab. Open split here anyway?', '&Yes\n&No', 2)
|
local choice = vim.fn.confirm('File is already open in a tab. Open split here anyway?', '&Yes\n&No', 2)
|
||||||
if choice ~= 1 then
|
if choice ~= 1 then
|
||||||
@@ -129,7 +117,6 @@ return {
|
|||||||
end
|
end
|
||||||
-- Else user chose Yes → continue to open split in current tab
|
-- Else user chose Yes → continue to open split in current tab
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Open in split
|
-- Open in split
|
||||||
if split_type == 'v' then
|
if split_type == 'v' then
|
||||||
-- horizontal → always below
|
-- horizontal → always below
|
||||||
@@ -139,8 +126,9 @@ return {
|
|||||||
vim.cmd('vertical rightbelow split ' .. vim.fn.fnameescape(path))
|
vim.cmd('vertical rightbelow split ' .. vim.fn.fnameescape(path))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- Telescope keymaps for using Smart Open
|
||||||
|
|
||||||
-- Telescope keymap using Smart Open
|
-- <leader>sf for find files
|
||||||
vim.keymap.set('n', '<leader>sf', function()
|
vim.keymap.set('n', '<leader>sf', function()
|
||||||
require('telescope.builtin').find_files {
|
require('telescope.builtin').find_files {
|
||||||
attach_mappings = function(_, map)
|
attach_mappings = function(_, map)
|
||||||
@@ -161,7 +149,50 @@ return {
|
|||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
end, { desc = '[S]earch [F]iles (Smart Open)' })
|
end, { desc = '[S]earch [F]iles' })
|
||||||
|
-- <leader>? for old files
|
||||||
|
vim.keymap.set('n', '<leader>?', function()
|
||||||
|
require('telescope.builtin').oldfiles {
|
||||||
|
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)
|
||||||
|
-- Horizontal split with 'h'
|
||||||
|
map('n', 'h', function(prompt_bufnr)
|
||||||
|
smart_open_split(prompt_bufnr, 'h')
|
||||||
|
end)
|
||||||
|
-- Vertical split with 'v'
|
||||||
|
map('n', 'v', function(prompt_bufnr)
|
||||||
|
smart_open_split(prompt_bufnr, 'v')
|
||||||
|
end)
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end, { desc = '[?] Find recently opened files' })
|
||||||
|
|
||||||
|
-- Current buffers for Telescope (switch to already open buffer)
|
||||||
|
vim.keymap.set('n', '<leader><leader>', function()
|
||||||
|
require('telescope.builtin').buffers {
|
||||||
|
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)
|
||||||
|
map('n', 'h', function(prompt_bufnr)
|
||||||
|
smart_open_split(prompt_bufnr, 'h')
|
||||||
|
end)
|
||||||
|
map('n', 'v', function(prompt_bufnr)
|
||||||
|
smart_open_split(prompt_bufnr, 'v')
|
||||||
|
end)
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end, { desc = 'Switch to Open Buffers' })
|
||||||
|
|
||||||
-- 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
|
||||||
@@ -200,8 +231,6 @@ return {
|
|||||||
vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, {})
|
vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, {})
|
||||||
|
|
||||||
-- See `:help telescope.builtin`
|
-- See `:help telescope.builtin`
|
||||||
vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
|
|
||||||
vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
|
|
||||||
vim.keymap.set('n', '<leader>/', function()
|
vim.keymap.set('n', '<leader>/', function()
|
||||||
-- You can pass additional configuration to telescope to change theme, layout, etc.
|
-- You can pass additional configuration to telescope to change theme, layout, etc.
|
||||||
require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
|
require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
|
||||||
@@ -224,7 +253,6 @@ return {
|
|||||||
vim.notify('Not a git repository', vim.log.levels.WARN, { title = 'Telescope Git Files' })
|
vim.notify('Not a git repository', vim.log.levels.WARN, { title = 'Telescope Git Files' })
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
require('telescope.builtin').git_files {
|
require('telescope.builtin').git_files {
|
||||||
attach_mappings = function(_, map)
|
attach_mappings = function(_, map)
|
||||||
local actions = require 'telescope.actions'
|
local actions = require 'telescope.actions'
|
||||||
@@ -238,13 +266,12 @@ return {
|
|||||||
pcall(actions.close, prompt_bufnr)
|
pcall(actions.close, prompt_bufnr)
|
||||||
smart_open(prompt_bufnr)
|
smart_open(prompt_bufnr)
|
||||||
end
|
end
|
||||||
|
|
||||||
map('i', '<CR>', open_smart)
|
map('i', '<CR>', open_smart)
|
||||||
map('n', '<CR>', open_smart)
|
map('n', '<CR>', open_smart)
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
end, { desc = 'Search [G]it [F]iles (Smart Open)' })
|
end, { desc = 'Search [G]it [F]iles' })
|
||||||
vim.keymap.set('n', '<leader>si', require('telescope.builtin').help_tags, { desc = '[S]earch [I]nfo' })
|
vim.keymap.set('n', '<leader>si', require('telescope.builtin').help_tags, { desc = '[S]earch [I]nfo' })
|
||||||
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' })
|
||||||
|
|||||||
@@ -216,62 +216,6 @@ vim.keymap.set('x', '<leader>p', '"_dP', { desc = 'Paste over selection without
|
|||||||
-- [[ Redo ]]
|
-- [[ Redo ]]
|
||||||
vim.keymap.set('n', 'U', '<C-r>')
|
vim.keymap.set('n', 'U', '<C-r>')
|
||||||
|
|
||||||
-- [[ Smart Open current buffers for Telescope (switch to already open buffer) ]]
|
|
||||||
local actions = require 'telescope.actions'
|
|
||||||
local action_state = require 'telescope.actions.state'
|
|
||||||
local builtin = require 'telescope.builtin'
|
|
||||||
local function smart_open_buffer()
|
|
||||||
builtin.buffers {
|
|
||||||
attach_mappings = function(_, map)
|
|
||||||
local function open_selected(prompt_bufnr)
|
|
||||||
local entry = action_state.get_selected_entry()
|
|
||||||
if not entry then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
actions.close(prompt_bufnr)
|
|
||||||
local bufname = vim.api.nvim_buf_get_name(entry.bufnr)
|
|
||||||
if bufname == '' then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
-- Check all windows in current tab
|
|
||||||
local current_tab = vim.api.nvim_get_current_tabpage()
|
|
||||||
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(current_tab)) do
|
|
||||||
local buf = vim.api.nvim_win_get_buf(win)
|
|
||||||
if vim.api.nvim_buf_get_name(buf) == bufname then
|
|
||||||
vim.api.nvim_set_current_win(win)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- Check other tabs
|
|
||||||
for _, tab in ipairs(vim.api.nvim_list_tabpages()) do
|
|
||||||
if tab ~= current_tab then
|
|
||||||
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) == bufname then
|
|
||||||
-- Switch tab first, then window
|
|
||||||
vim.api.nvim_set_current_tabpage(tab)
|
|
||||||
vim.api.nvim_set_current_win(win)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- Not open anywhere → open in current window
|
|
||||||
vim.cmd('buffer ' .. entry.bufnr)
|
|
||||||
end
|
|
||||||
map('i', '<CR>', open_selected)
|
|
||||||
map('n', '<CR>', open_selected)
|
|
||||||
return true
|
|
||||||
end,
|
|
||||||
}
|
|
||||||
end
|
|
||||||
-- Map it to <leader><leader>
|
|
||||||
vim.keymap.set('n', '<leader><leader>', smart_open_buffer, { desc = 'Switch to Open Buffers' })
|
|
||||||
-- which-key, register it to show a description
|
|
||||||
require('which-key').register {
|
|
||||||
['<leader><leader>'] = { smart_open_buffer, 'Switch to Open Buffers' },
|
|
||||||
}
|
|
||||||
|
|
||||||
-- [[ Smart open a file path, reusing empty buffers or tabs if possible ]]
|
-- [[ Smart open a file path, reusing empty buffers or tabs if possible ]]
|
||||||
local function smart_open_file(path)
|
local function smart_open_file(path)
|
||||||
if not path or path == '' then
|
if not path or path == '' then
|
||||||
@@ -304,8 +248,7 @@ local function smart_open_file(path)
|
|||||||
-- 3. Otherwise → open in a new tab
|
-- 3. Otherwise → open in a new tab
|
||||||
vim.cmd('tabedit ' .. vim.fn.fnameescape(path))
|
vim.cmd('tabedit ' .. vim.fn.fnameescape(path))
|
||||||
end
|
end
|
||||||
|
-- Remap gf to use smart_open_file
|
||||||
-- [[ Remap gf to use smart_open_file ]]
|
|
||||||
vim.keymap.set('n', 'gf', function()
|
vim.keymap.set('n', 'gf', function()
|
||||||
local path = vim.fn.expand '<cfile>' -- get file under cursor
|
local path = vim.fn.expand '<cfile>' -- get file under cursor
|
||||||
smart_open_file(path)
|
smart_open_file(path)
|
||||||
|
|||||||
Reference in New Issue
Block a user