dotfiles/config/nvim/lua/fschauen/telescope.lua

69 lines
1.8 KiB
Lua

M = {}
local builtin = function(picker, opts)
return function(title)
return function()
local args = vim.tbl_extend('keep', { prompt_title = title }, opts or {})
return require('telescope.builtin')[picker](args)
end
end
end
---Preserve register contents over function call.
---@param reg string: register to save, must be a valid register name.
---@param func function: function that may freely clobber the register.
---@return any: return value of calling `func`.
local with_saved_register = function(reg, func)
local saved = vim.fn.getreg(reg)
local result = func()
vim.fn.setreg(reg, saved)
return result
end
---Get selected text.
---@return string: selected text, or work under cursor if not in visual mode.
local get_selected_text = function()
if vim.fn.mode() ~= 'v' then return vim.fn.expand '<cword>' end
return with_saved_register('v', function()
vim.cmd [[noautocmd sil norm "vy]]
return vim.fn.getreg 'v'
end)
end
M.pickers = setmetatable({
all_files = builtin('find_files', {
hidden = true,
no_ignore = true,
no_ignore_parent = true,
}),
colorscheme = builtin('colorscheme', {
enable_preview = true,
}),
diagnostics = builtin('diagnostics', {
bufnr = 0
}),
dotfiles = builtin('find_files', {
cwd = '~/.dotfiles',
hidden = true,
}),
selection = function(title)
return function()
local text = get_selected_text()
return require('telescope.builtin').grep_string {
prompt_title = string.format(title .. ': %s ', text),
search = text,
}
end
end,
here = builtin('current_buffer_fuzzy_find'),
}, {
-- Fall back to telescope's built-in pickers if a custom one is not defined
-- above, but make sure to keep the title we defined.
__index = function(_, picker)
return builtin(picker)
end
})
return M