telescope.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. -- file/buffer/etc picker
  2. local telescope = require('telescope')
  3. local telescope_actions = require('telescope.actions')
  4. local telescope_builtin = require('telescope.builtin')
  5. local telescope_pickers = require('telescope.pickers')
  6. local telescope_finders = require('telescope.finders')
  7. local telescope_themes = require('telescope.themes')
  8. local telescope_previewers = require('telescope.previewers')
  9. local telescope_putils = require('telescope.previewers.utils')
  10. local telescope_conf = require('telescope.config').values
  11. local action_state = require('telescope.actions.state')
  12. local util = require('user.util')
  13. local actions = {}
  14. actions.git_show_commit = function(prompt_bufnr)
  15. local selection = action_state.get_selected_entry()
  16. local path = vim.fn.FugitiveFind(selection.value)
  17. telescope_actions.close(prompt_bufnr)
  18. vim.cmd.edit(path)
  19. end
  20. local commit_mappings = {
  21. i = {
  22. ['<CR>'] = actions.git_show_commit,
  23. ['<C-r>c'] = telescope_actions.git_checkout,
  24. }
  25. }
  26. telescope.setup({
  27. defaults = {
  28. mappings = {
  29. i = {
  30. ['<Esc>'] = 'close',
  31. ['<C-e>'] = 'close',
  32. },
  33. },
  34. layout_config = {
  35. prompt_position = 'top',
  36. },
  37. sorting_strategy = 'ascending',
  38. -- use filename as preview window title
  39. dynamic_preview_title = true,
  40. path_display = {
  41. -- shorten directory names of everything but the last 3 parts
  42. -- foo/bar/baz/file.txt -> f/boo/bar/file.txt
  43. shorten = { len = 1, exclude = { -3, -2, -1 } },
  44. -- truncate the beginning of the file name if wider than the window
  45. truncate = true,
  46. },
  47. preview = {
  48. -- don't preview files larger than 1MB
  49. filesize_limit = 1,
  50. timeout = 500,
  51. },
  52. vimgrep_arguments = {
  53. -- defaults
  54. 'rg',
  55. '--color=never',
  56. '--no-heading',
  57. '--with-filename',
  58. '--line-number',
  59. '--column',
  60. '--smart-case',
  61. -- search "hidden" files except git folder
  62. '--hidden',
  63. '--iglob=!.git'
  64. },
  65. -- ignore things we're likely not to edit
  66. file_ignore_patterns = {
  67. '%.zip$',
  68. '%.yarn/releases/',
  69. '%.yarn/plugins/'
  70. },
  71. -- picker history
  72. cache_picker = {
  73. num_pickers = 10,
  74. },
  75. },
  76. pickers = {
  77. buffers = {
  78. sort_lastused = true,
  79. sort_mru = true,
  80. mappings = {
  81. i = {
  82. ['<C-k>'] = 'delete_buffer'
  83. },
  84. },
  85. },
  86. find_files = {
  87. find_command = { 'fd', '--type', 'f', '--strip-cwd-prefix' }
  88. },
  89. git_commits = {
  90. mappings = commit_mappings,
  91. },
  92. git_bcommits = {
  93. mappings = commit_mappings,
  94. },
  95. },
  96. extensions = {
  97. ['ui-select'] = {
  98. telescope_themes.get_dropdown({})
  99. }
  100. }
  101. })
  102. -- use native sorter for better performance
  103. telescope.load_extension('fzf')
  104. -- use telescope for vim.ui.select
  105. telescope.load_extension('ui-select')
  106. -- custom picker to fallback to files if no git
  107. local function project_files()
  108. local ok = pcall(telescope_builtin.git_files, { show_untracked = true })
  109. if not ok then telescope_builtin.find_files({}) end
  110. end
  111. -- custom picker for files within a commit
  112. local function commit_files(opts)
  113. local resolved = util.resolve_git_path(0)
  114. if not resolved then
  115. vim.print('current file is not a fugitive path')
  116. return
  117. end
  118. opts = opts or {}
  119. telescope_pickers.new(opts, {
  120. prompt_title = resolved.commit,
  121. finder = telescope_finders.new_oneshot_job({ 'git', 'ls-tree', '--name-only', '-r', resolved.commit }, {
  122. entry_maker = function(entry)
  123. local path = string.format('fugitive://%s//%s/%s', resolved.repo, resolved.commit, entry)
  124. return {
  125. path = path,
  126. value = entry,
  127. display = entry,
  128. ordinal = entry,
  129. }
  130. end,
  131. }),
  132. sorter = telescope_conf.file_sorter(opts),
  133. -- the builtin previewer has fancy async loading which doesn't work for
  134. -- fugitive paths so we have to define our own
  135. previewer = telescope_previewers.new_buffer_previewer({
  136. title = function(self)
  137. return 'Commit Files'
  138. end,
  139. dyn_title = function(self, entry)
  140. return entry.value
  141. end,
  142. define_preview = function(self, entry, status)
  143. -- the builtin previewer does more things like using mime type
  144. -- fallbacks as well as binary file detection which ours doesn't do
  145. local ft = telescope_putils.filetype_detect(entry.value)
  146. vim.api.nvim_buf_call(self.state.bufnr, function()
  147. vim.cmd('Gread ' .. entry.path)
  148. telescope_putils.highlighter(self.state.bufnr, ft, opts)
  149. end)
  150. end,
  151. }),
  152. }):find()
  153. end
  154. vim.keymap.set('n', '<C-P>', project_files)
  155. vim.keymap.set('n', '<C-O>', '<cmd>Telescope buffers<CR>')
  156. vim.keymap.set('n', '<Leader>fg', commit_files)
  157. vim.keymap.set('n', '<Leader>ff', '<cmd>Telescope live_grep<CR>')
  158. vim.keymap.set('n', '<Leader>fr', '<cmd>Telescope resume<CR>')
  159. vim.keymap.set('n', '<Leader>fp', '<cmd>Telescope pickers<CR>')