-- file/buffer/etc picker local telescope = require('telescope') local telescope_actions = require('telescope.actions') local telescope_builtin = require('telescope.builtin') local telescope_pickers = require('telescope.pickers') local telescope_finders = require('telescope.finders') local telescope_previewers = require('telescope.previewers') local telescope_putils = require('telescope.previewers.utils') local telescope_conf = require('telescope.config').values local action_state = require('telescope.actions.state') local util = require('user.util') local actions = {} actions.git_show_commit = function(prompt_bufnr) local selection = action_state.get_selected_entry() local path = vim.fn.FugitiveFind(selection.value) telescope_actions.close(prompt_bufnr) vim.cmd.edit(path) end local commit_mappings = { i = { [''] = actions.git_show_commit, ['c'] = telescope_actions.git_checkout, } } telescope.setup({ defaults = { mappings = { i = { ['jj'] = 'close', }, }, layout_config = { prompt_position = 'top', }, sorting_strategy = 'ascending', -- use filename as preview window title dynamic_preview_title = true, path_display = { -- shorten directory names of everything but the last 3 parts -- foo/bar/baz/file.txt -> f/boo/bar/file.txt shorten = { len = 1, exclude = { -3, -2, -1 } }, -- truncate the beginning of the file name if wider than the window truncate = true, }, preview = { -- don't preview files larger than 1MB filesize_limit = 1, timeout = 500, }, vimgrep_arguments = { -- defaults 'rg', '--color=never', '--no-heading', '--with-filename', '--line-number', '--column', '--smart-case', -- search "hidden" files except git folder '--hidden', '--iglob=!.git' }, -- ignore things we're likely not to edit file_ignore_patterns = { '%.zip$', '%.yarn/releases/', '%.yarn/plugins/' }, -- picker history cache_picker = { num_pickers = 10, }, }, pickers = { buffers = { sort_lastused = true, sort_mru = true, mappings = { i = { [''] = 'delete_buffer' }, }, }, find_files = { find_command = { 'fd', '--type', 'f', '--strip-cwd-prefix' } }, git_commits = { mappings = commit_mappings, }, git_bcommits = { mappings = commit_mappings, }, }, }) -- use native sorter for better performance telescope.load_extension('fzf') -- custom picker to fallback to files if no git local function project_files() local ok = pcall(telescope_builtin.git_files, { show_untracked = true }) if not ok then telescope_builtin.find_files({}) end end -- custom picker for files within a commit local function commit_files(opts) local resolved = util.resolve_git_path(0) if not resolved then vim.print('current file is not a fugitive path') return end opts = opts or {} telescope_pickers.new(opts, { prompt_title = resolved.commit, finder = telescope_finders.new_oneshot_job({ 'git', 'ls-tree', '--name-only', '-r', resolved.commit }, { entry_maker = function(entry) local path = string.format('fugitive://%s//%s/%s', resolved.repo, resolved.commit, entry) return { path = path, value = entry, display = entry, ordinal = entry, } end, }), sorter = telescope_conf.file_sorter(opts), -- the builtin previewer has fancy async loading which doesn't work for -- fugitive paths so we have to define our own previewer = telescope_previewers.new_buffer_previewer({ title = function(self) return 'Commit Files' end, dyn_title = function(self, entry) return entry.value end, define_preview = function(self, entry, status) -- the builtin previewer does more things like using mime type -- fallbacks as well as binary file detection which ours doesn't do local ft = telescope_putils.filetype_detect(entry.value) vim.api.nvim_buf_call(self.state.bufnr, function() vim.cmd('Gread ' .. entry.path) telescope_putils.highlighter(self.state.bufnr, ft, opts) end) end, }), }):find() end vim.keymap.set('n', '', project_files) vim.keymap.set('n', '', 'Telescope buffers') vim.keymap.set('n', 'fg', commit_files) vim.keymap.set('n', 'ff', 'Telescope live_grep') vim.keymap.set('n', 'fr', 'Telescope resume') vim.keymap.set('n', 'fp', 'Telescope pickers')