telescope.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. local M = {}
  2. local utils = require "utils"
  3. function M.config()
  4. -- Define this minimal config so that it's available if telescope is not yet available.
  5. lvim.builtin.telescope = {
  6. ---@usage disable telescope completely [not recommeded]
  7. active = true,
  8. on_config_done = nil,
  9. }
  10. local status_ok, actions = pcall(require, "telescope.actions")
  11. if not status_ok then
  12. return
  13. end
  14. lvim.builtin.telescope = vim.tbl_extend("force", lvim.builtin.telescope, {
  15. defaults = {
  16. prompt_prefix = " ",
  17. selection_caret = " ",
  18. entry_prefix = " ",
  19. initial_mode = "insert",
  20. selection_strategy = "reset",
  21. sorting_strategy = "descending",
  22. layout_strategy = "horizontal",
  23. layout_config = {
  24. width = 0.75,
  25. prompt_position = "bottom",
  26. preview_cutoff = 120,
  27. horizontal = { mirror = false },
  28. vertical = { mirror = false },
  29. },
  30. file_sorter = require("telescope.sorters").get_fzy_sorter,
  31. file_ignore_patterns = {},
  32. generic_sorter = require("telescope.sorters").get_generic_fuzzy_sorter,
  33. path_display = { shorten = 5 },
  34. winblend = 0,
  35. border = {},
  36. borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
  37. color_devicons = true,
  38. use_less = true,
  39. set_env = { ["COLORTERM"] = "truecolor" }, -- default = nil,
  40. file_previewer = require("telescope.previewers").vim_buffer_cat.new,
  41. grep_previewer = require("telescope.previewers").vim_buffer_vimgrep.new,
  42. qflist_previewer = require("telescope.previewers").vim_buffer_qflist.new,
  43. -- Developer configurations: Not meant for general override
  44. -- buffer_previewer_maker = require("telescope.previewers").buffer_previewer_maker,
  45. mappings = {
  46. i = {
  47. ["<C-n>"] = actions.move_selection_next,
  48. ["<C-p>"] = actions.move_selection_previous,
  49. ["<C-c>"] = actions.close,
  50. ["<C-j>"] = actions.cycle_history_next,
  51. ["<C-k>"] = actions.cycle_history_prev,
  52. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  53. ["<CR>"] = actions.select_default + actions.center,
  54. -- To disable a keymap, put [map] = false
  55. -- So, to not map "<C-n>", just put
  56. -- ["<c-t>"] = trouble.open_with_trouble,
  57. -- ["<c-x>"] = false,
  58. -- ["<esc>"] = actions.close,
  59. -- Otherwise, just set the mapping to the function that you want it to be.
  60. -- ["<C-i>"] = actions.select_horizontal,
  61. -- Add up multiple actions
  62. -- You can perform as many actions in a row as you like
  63. -- ["<CR>"] = actions.select_default + actions.center + my_cool_custom_action,
  64. },
  65. n = {
  66. ["<C-n>"] = actions.move_selection_next,
  67. ["<C-p>"] = actions.move_selection_previous,
  68. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  69. -- ["<c-t>"] = trouble.open_with_trouble,
  70. -- ["<C-i>"] = my_cool_custom_action,
  71. },
  72. },
  73. },
  74. extensions = {
  75. fzy_native = {
  76. override_generic_sorter = false,
  77. override_file_sorter = true,
  78. },
  79. },
  80. })
  81. end
  82. function M.find_lunarvim_files(opts)
  83. opts = opts or {}
  84. local themes = require "telescope.themes"
  85. local theme_opts = themes.get_ivy {
  86. sorting_strategy = "ascending",
  87. layout_strategy = "bottom_pane",
  88. prompt = ">> ",
  89. prompt_title = "~ LunarVim files ~",
  90. cwd = utils.join_paths(get_runtime_dir(), "lvim"),
  91. find_command = { "git", "ls-files" },
  92. }
  93. opts = vim.tbl_deep_extend("force", theme_opts, opts)
  94. require("telescope.builtin").find_files(opts)
  95. end
  96. function M.grep_lunarvim_files(opts)
  97. opts = opts or {}
  98. local themes = require "telescope.themes"
  99. local theme_opts = themes.get_ivy {
  100. sorting_strategy = "ascending",
  101. layout_strategy = "bottom_pane",
  102. prompt = ">> ",
  103. prompt_title = "~ search LunarVim ~",
  104. cwd = utils.join_paths(get_runtime_dir(), "lvim"),
  105. }
  106. opts = vim.tbl_deep_extend("force", theme_opts, opts)
  107. require("telescope.builtin").live_grep(opts)
  108. end
  109. function M.view_lunarvim_changelog()
  110. local finders = require "telescope.finders"
  111. local make_entry = require "telescope.make_entry"
  112. local pickers = require "telescope.pickers"
  113. local previewers = require "telescope.previewers"
  114. local actions = require "telescope.actions"
  115. local opts = {}
  116. local conf = require("telescope.config").values
  117. opts.entry_maker = make_entry.gen_from_git_commits(opts)
  118. pickers.new(opts, {
  119. prompt_title = "LunarVim changelog",
  120. finder = finders.new_oneshot_job(
  121. vim.tbl_flatten {
  122. "git",
  123. "log",
  124. "--pretty=oneline",
  125. "--abbrev-commit",
  126. "--",
  127. ".",
  128. },
  129. opts
  130. ),
  131. previewer = {
  132. previewers.git_commit_diff_to_parent.new(opts),
  133. previewers.git_commit_diff_to_head.new(opts),
  134. previewers.git_commit_diff_as_was.new(opts),
  135. previewers.git_commit_message.new(opts),
  136. },
  137. --TODO: consider opening a diff view when pressing enter
  138. attach_mappings = function(_, map)
  139. map("i", "<enter>", actions._close)
  140. map("n", "<enter>", actions._close)
  141. map("i", "<esc>", actions._close)
  142. map("n", "<esc>", actions._close)
  143. map("n", "q", actions._close)
  144. return true
  145. end,
  146. sorter = conf.file_sorter(opts),
  147. }):find()
  148. end
  149. function M.code_actions()
  150. local opts = {
  151. winblend = 15,
  152. layout_config = {
  153. prompt_position = "top",
  154. width = 80,
  155. height = 12,
  156. },
  157. borderchars = {
  158. prompt = { "─", "│", " ", "│", "╭", "╮", "│", "│" },
  159. results = { "─", "│", "─", "│", "├", "┤", "╯", "╰" },
  160. preview = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
  161. },
  162. border = {},
  163. previewer = false,
  164. shorten_path = false,
  165. }
  166. require("telescope.builtin").lsp_code_actions(require("telescope.themes").get_dropdown(opts))
  167. end
  168. function M.setup()
  169. local telescope = require "telescope"
  170. telescope.setup(lvim.builtin.telescope)
  171. if lvim.builtin.project.active then
  172. telescope.load_extension "projects"
  173. end
  174. if lvim.builtin.telescope.on_config_done then
  175. lvim.builtin.telescope.on_config_done(telescope)
  176. end
  177. end
  178. return M