nvim/lua/custom/plugins/telescope.lua
Jeremie Fraeys 5d7b037e99
Some checks failed
Luacheck / luacheck (push) Successful in 10s
StyLua / stylua (push) Failing after 3s
fix: remove telescope-undo references since plugin was removed
2026-02-08 15:27:48 -05:00

81 lines
2.8 KiB
Lua
Executable file

return {
-- Fuzzy Finder (files, LSP, etc.)
'nvim-telescope/telescope.nvim',
cmd = 'Telescope',
version = '*',
keys = {
{ '<leader>ff', '<cmd>Telescope find_files<CR>', desc = 'Find Files' },
{ '<leader>fg', '<cmd>Telescope live_grep<CR>', desc = 'Live Grep' },
{ '<leader>fb', '<cmd>Telescope buffers<CR>', desc = 'Buffers' },
{ '<leader>fh', '<cmd>Telescope help_tags<CR>', desc = 'Help Tags' },
{ '<leader>fr', '<cmd>Telescope oldfiles<CR>', desc = 'Recent Files' },
},
dependencies = {
'nvim-lua/plenary.nvim',
{ 'nvim-telescope/telescope-fzf-native.nvim', build = 'make' },
'nvim-tree/nvim-web-devicons',
},
config = function()
local telescope = require('telescope')
-- Configure Telescope
telescope.setup({
defaults = {
-- prompt_prefix = '🔍 ',
sorting_strategy = 'descending',
layout_strategy = 'flex',
mappings = {
i = {
['<C-u>'] = false, -- Disable Ctrl+u clearing input
['<C-d>'] = false, -- Disable Ctrl+d clearing input
},
},
-- Attach the global mapping for centering the cursor on selection
attach_mappings = function(prompt_bufnr, _)
local actions = require('telescope.actions')
-- When selecting a result, center it in the middle of the screen
actions.select_default:replace(function()
local line = actions.state.get_selected_entry().lnum
vim.api.nvim_win_set_cursor(0, { line, 0 })
vim.cmd('normal! zz') -- This centers the line in the middle of the screen
actions.close(prompt_bufnr) -- Close the Telescope window
end)
return true
end,
},
extensions = {
fzf = {},
},
})
-- Load the fzf extension for Telescope
telescope.load_extension('fzf')
-- Function to find git root directory
local function find_git_root()
local current_file = vim.api.nvim_buf_get_name(0)
local current_dir = current_file ~= '' and vim.fn.fnamemodify(current_file, ':h') or vim.fn.getcwd()
local git_root =
vim.fn.systemlist('git -C ' .. vim.fn.escape(current_dir, ' ') .. ' rev-parse --show-toplevel')[1]
if vim.v.shell_error ~= 0 then
print('Not a git repository, searching in current directory.')
return vim.fn.getcwd()
end
return git_root
end
-- Function for live_grep within the Git root
local function live_grep_git_root()
local git_root = find_git_root()
if git_root then
require('telescope.builtin').live_grep({ search_dirs = { git_root } })
end
end
-- Command to trigger live_grep_git_root
vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, { desc = 'Live grep in Git root' })
end,
}