telescope.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 recommended]
  6. active = true,
  7. on_config_done = nil,
  8. }
  9. local ok, actions = pcall(require, "telescope.actions")
  10. if not 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 = {
  26. preview_width = function(_, cols, _)
  27. if cols < 120 then
  28. return math.floor(cols * 0.5)
  29. end
  30. return math.floor(cols * 0.6)
  31. end,
  32. mirror = false,
  33. },
  34. vertical = { mirror = false },
  35. },
  36. vimgrep_arguments = {
  37. "rg",
  38. "--color=never",
  39. "--no-heading",
  40. "--with-filename",
  41. "--line-number",
  42. "--column",
  43. "--smart-case",
  44. "--hidden",
  45. "--glob=!.git/",
  46. },
  47. mappings = {
  48. i = {
  49. ["<C-n>"] = actions.move_selection_next,
  50. ["<C-p>"] = actions.move_selection_previous,
  51. ["<C-c>"] = actions.close,
  52. ["<C-j>"] = actions.cycle_history_next,
  53. ["<C-k>"] = actions.cycle_history_prev,
  54. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  55. ["<CR>"] = actions.select_default + actions.center,
  56. },
  57. n = {
  58. ["<C-n>"] = actions.move_selection_next,
  59. ["<C-p>"] = actions.move_selection_previous,
  60. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  61. },
  62. },
  63. file_ignore_patterns = {},
  64. path_display = { shorten = 5 },
  65. winblend = 0,
  66. border = {},
  67. borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
  68. color_devicons = true,
  69. set_env = { ["COLORTERM"] = "truecolor" }, -- default = nil,
  70. pickers = {
  71. find_files = {
  72. find_command = { "fd", "--type=file", "--hidden", "--smart-case" },
  73. },
  74. live_grep = {
  75. --@usage don't include the filename in the search results
  76. only_sort_text = true,
  77. },
  78. },
  79. },
  80. extensions = {
  81. fzf = {
  82. fuzzy = true, -- false will only do exact matching
  83. override_generic_sorter = true, -- override the generic sorter
  84. override_file_sorter = true, -- override the file sorter
  85. case_mode = "smart_case", -- or "ignore_case" or "respect_case"
  86. },
  87. },
  88. })
  89. end
  90. function M.code_actions()
  91. local opts = {
  92. winblend = 15,
  93. layout_config = {
  94. prompt_position = "top",
  95. width = 80,
  96. height = 12,
  97. },
  98. borderchars = lvim.builtin.telescope.defaults.borderchars,
  99. border = {},
  100. previewer = false,
  101. shorten_path = false,
  102. }
  103. local builtin = require "telescope.builtin"
  104. local themes = require "telescope.themes"
  105. builtin.lsp_code_actions(themes.get_dropdown(opts))
  106. end
  107. function M.setup()
  108. local previewers = require "telescope.previewers"
  109. local sorters = require "telescope.sorters"
  110. local actions = require "telescope.actions"
  111. lvim.builtin.telescope = vim.tbl_extend("keep", {
  112. file_previewer = previewers.vim_buffer_cat.new,
  113. grep_previewer = previewers.vim_buffer_vimgrep.new,
  114. qflist_previewer = previewers.vim_buffer_qflist.new,
  115. file_sorter = sorters.get_fuzzy_file,
  116. generic_sorter = sorters.get_generic_fuzzy_sorter,
  117. ---@usage Mappings are fully customizable. Many familiar mapping patterns are setup as defaults.
  118. mappings = {
  119. i = {
  120. ["<C-n>"] = actions.move_selection_next,
  121. ["<C-p>"] = actions.move_selection_previous,
  122. ["<C-c>"] = actions.close,
  123. ["<C-j>"] = actions.cycle_history_next,
  124. ["<C-k>"] = actions.cycle_history_prev,
  125. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  126. ["<CR>"] = actions.select_default + actions.center,
  127. },
  128. n = {
  129. ["<C-n>"] = actions.move_selection_next,
  130. ["<C-p>"] = actions.move_selection_previous,
  131. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  132. },
  133. },
  134. }, lvim.builtin.telescope)
  135. local telescope = require "telescope"
  136. telescope.setup(lvim.builtin.telescope)
  137. if lvim.builtin.project.active then
  138. pcall(function()
  139. require("telescope").load_extension "projects"
  140. end)
  141. end
  142. if lvim.builtin.telescope.on_config_done then
  143. lvim.builtin.telescope.on_config_done(telescope)
  144. end
  145. if lvim.builtin.telescope.extensions and lvim.builtin.telescope.extensions.fzf then
  146. pcall(function()
  147. require("telescope").load_extension "fzf"
  148. end)
  149. end
  150. end
  151. return M