telescope.lua 6.2 KB

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