123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- -- 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 = {
- ['<CR>'] = actions.git_show_commit,
- ['<C-r>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 = {
- ['<C-k>'] = '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', '<C-P>', project_files)
- vim.keymap.set('n', '<C-O>', '<cmd>Telescope buffers<CR>')
- vim.keymap.set('n', '<Leader>fg', commit_files)
- vim.keymap.set('n', '<Leader>ff', '<cmd>Telescope live_grep<CR>')
- vim.keymap.set('n', '<Leader>fr', '<cmd>Telescope resume<CR>')
- vim.keymap.set('n', '<Leader>fp', '<cmd>Telescope pickers<CR>')
|