telescope.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. preview_cutoff = 120,
  26. horizontal = { mirror = false },
  27. vertical = { mirror = false },
  28. },
  29. file_sorter = require("telescope.sorters").get_fzy_sorter,
  30. file_ignore_patterns = {},
  31. generic_sorter = require("telescope.sorters").get_generic_fuzzy_sorter,
  32. path_display = { shorten = 5 },
  33. winblend = 0,
  34. border = {},
  35. borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
  36. color_devicons = true,
  37. use_less = true,
  38. set_env = { ["COLORTERM"] = "truecolor" }, -- default = nil,
  39. file_previewer = require("telescope.previewers").vim_buffer_cat.new,
  40. grep_previewer = require("telescope.previewers").vim_buffer_vimgrep.new,
  41. qflist_previewer = require("telescope.previewers").vim_buffer_qflist.new,
  42. -- Developer configurations: Not meant for general override
  43. -- buffer_previewer_maker = require("telescope.previewers").buffer_previewer_maker,
  44. mappings = {
  45. i = {
  46. ["<C-n>"] = actions.move_selection_next,
  47. ["<C-p>"] = actions.move_selection_previous,
  48. ["<C-c>"] = actions.close,
  49. ["<C-j>"] = actions.cycle_history_next,
  50. ["<C-k>"] = actions.cycle_history_prev,
  51. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  52. ["<CR>"] = actions.select_default + actions.center,
  53. -- To disable a keymap, put [map] = false
  54. -- So, to not map "<C-n>", just put
  55. -- ["<c-t>"] = trouble.open_with_trouble,
  56. -- ["<c-x>"] = false,
  57. -- ["<esc>"] = actions.close,
  58. -- Otherwise, just set the mapping to the function that you want it to be.
  59. -- ["<C-i>"] = actions.select_horizontal,
  60. -- Add up multiple actions
  61. -- You can perform as many actions in a row as you like
  62. -- ["<CR>"] = actions.select_default + actions.center + my_cool_custom_action,
  63. },
  64. n = {
  65. ["<C-n>"] = actions.move_selection_next,
  66. ["<C-p>"] = actions.move_selection_previous,
  67. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  68. -- ["<c-t>"] = trouble.open_with_trouble,
  69. -- ["<C-i>"] = my_cool_custom_action,
  70. },
  71. },
  72. },
  73. extensions = {
  74. fzy_native = {
  75. override_generic_sorter = false,
  76. override_file_sorter = true,
  77. },
  78. },
  79. })
  80. end
  81. function M.find_lunarvim_files(opts)
  82. opts = opts or {}
  83. local themes = require "telescope.themes"
  84. local theme_opts = themes.get_ivy {
  85. sorting_strategy = "ascending",
  86. layout_strategy = "bottom_pane",
  87. prompt_prefix = ">> ",
  88. prompt_title = "~ LunarVim files ~",
  89. cwd = utils.join_paths(get_runtime_dir(), "lvim"),
  90. find_command = { "git", "ls-files" },
  91. }
  92. opts = vim.tbl_deep_extend("force", theme_opts, opts)
  93. require("telescope.builtin").find_files(opts)
  94. end
  95. function M.grep_lunarvim_files(opts)
  96. opts = opts or {}
  97. local themes = require "telescope.themes"
  98. local theme_opts = themes.get_ivy {
  99. sorting_strategy = "ascending",
  100. layout_strategy = "bottom_pane",
  101. prompt_prefix = ">> ",
  102. prompt_title = "~ search LunarVim ~",
  103. cwd = utils.join_paths(get_runtime_dir(), "lvim"),
  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