Files
nvim/lua/conf/plugins/telescope.lua
2025-08-10 13:10:05 -04:00

158 lines
4.0 KiB
Lua

local map = core.misc.map
--- get the root directory for telescope to search
---@return string the root directory
local function root_dir()
local clients = vim.lsp.get_clients({ bufnr = 0 })
if #clients > 0 then
return clients[1].config.root_dir
end
return "."
end
--- wrap a telebuilt picker to make it work for the current project root
---@param fn function telebuilt function
---@return function the new function
local function telebuilt_picker(fn)
return function()
fn { cwd = root_dir() }
end
end
return { "nvim-telescope/telescope.nvim",
reqs = {
"nvim-lua/plenary.nvim",
{ "nvim-telescope/telescope-fzf-native.nvim",
config = function()
vim.cmd("make")
end
},
"nvim-telescope/telescope-ui-select.nvim"
},
lazy = function(load)
load:cmd("Telescope")
load:keymap("n", "<leader>f")
load:keymap("n", "<leader>s")
load:keymap("n", "<leader>i")
load:keymap("n", "<leader>tc")
load:keymap("n", "<leader>tp")
end,
load = function()
local telescope = require("telescope")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local function send_limited_to_qflist_and_open(prompt_bufnr)
local picker = action_state.get_current_picker(prompt_bufnr)
local entries = {}
local max_items = 100
local count = 0
for entry in picker.manager:iter() do
if count >= max_items then
break
end
local filename = entry.path or entry.filename or entry.value
local text = entry.text or entry.value
if not text then
if type(entry.value) == "table" then
text = entry.value.text
else
text = entry.value
end
end
local pattern
if not entry.lnum then
if type(entry.display) == "string" then
pattern = entry.display
elseif type(entry.ordinal) == "string" then
pattern = entry.ordinal
elseif type(text) == "string" then
pattern = text
else
entry.lnum = 1
end
end
if filename then
table.insert(entries, {
filename = filename,
text = text,
lnum = entry.lnum,
col = entry.col,
-- we try and put a pattern in based on the info we receive from
-- telescope so that the qf list takes us to the correct place
pattern = pattern
})
count = count + 1
end
end
if #entries > 0 then
-- make sure errors get suppressed. I don't care
pcall(vim.fn.setqflist, {}, " ", {
title = "Telescope Limited Results",
items = entries
})
end
actions.select_default(prompt_bufnr)
end
telescope.setup {
defaults = {
layout_strategy = "bottom_pane",
borderchars = { " ", " ", " ", " ", " ", " ", " ", " " },
mappings = {
i = {
["<esc>"] = actions.close,
["<C-j>"] = actions.move_selection_next,
["<C-k>"] = actions.move_selection_previous,
["<CR>"] = send_limited_to_qflist_and_open,
}
}
},
pickers = {
colorscheme = {
enable_preview = true
}
},
extensions = {
fzf = {}
}
}
-- load in the fzf extension
telescope.load_extension("fzf")
telescope.load_extension("ui-select")
-- keymaps
local telebuilt = require("telescope.builtin")
map("n", "<leader>f", telebuilt_picker(telebuilt.find_files), {
desc = "Find files."
})
map("n", "<leader>s", telebuilt_picker(telebuilt.live_grep), {
desc = "Find strings."
})
map("n", "<leader>i", telebuilt.help_tags, { desc = "find help tags." })
-- find over specific directories
map("n", "<leader>tc", function()
telebuilt.find_files { cwd = vim.fn.stdpath("config") }
end, { desc = "find config files" })
map("n", "<leader>tp", function()
telebuilt.find_files {
cwd = vim.fs.joinpath(vim.fn.stdpath("data"), "site/pack/deps/opt")
}
end, { desc = "find files in plugin directory" })
map("n", "<leader>tt", "<cmd>Telescope<CR>")
end
}