You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.8 KiB

  1. return {
  2. "nvim-telescope/telescope.nvim",
  3. dependencies = {
  4. "nvim-lua/plenary.nvim",
  5. { "nvim-telescope/telescope-fzf-native.nvim", build = "make" },
  6. "nvim-tree/nvim-web-devicons",
  7. "debugloop/telescope-undo.nvim",
  8. "nvim-telescope/telescope-ui-select.nvim",
  9. },
  10. config = function()
  11. local telescope = require("telescope")
  12. local actions = require("telescope.actions")
  13. local previewers = require("telescope.previewers")
  14. local previewers_utils = require("telescope.previewers.utils")
  15. local sorters = require("telescope.sorters")
  16. local builtin = require("telescope.builtin")
  17. local max_size = 100000
  18. local truncate_large_files = function(filepath, bufnr, opts)
  19. opts = opts or {}
  20. filepath = vim.fn.expand(filepath)
  21. ---@diagnostic disable-next-line: undefined-field
  22. vim.loop.fs_stat(filepath, function(_, stat)
  23. if not stat then
  24. return
  25. end
  26. if stat.size > max_size then
  27. local cmd = { "head", "-c", max_size, filepath }
  28. previewers_utils.job_maker(cmd, bufnr, opts)
  29. else
  30. previewers.buffer_previewer_maker(filepath, bufnr, opts)
  31. end
  32. end)
  33. end
  34. telescope.setup({
  35. defaults = {
  36. file_sorter = sorters.get_fzy_sorter,
  37. color_devicons = true,
  38. buffer_previewer_maker = truncate_large_files,
  39. initial_mode = "insert",
  40. selection_strategy = "reset",
  41. sorting_strategy = "ascending",
  42. layout_strategy = "horizontal",
  43. layout_config = {
  44. horizontal = {
  45. prompt_position = "top",
  46. preview_width = 0.55,
  47. results_width = 0.8,
  48. },
  49. vertical = {
  50. mirror = false,
  51. },
  52. width = 0.87,
  53. height = 0.80,
  54. preview_cutoff = 120,
  55. },
  56. path_display = { "truncate" },
  57. winblend = 0,
  58. border = {},
  59. borderchars = nil,
  60. mappings = {
  61. i = {
  62. ["<C-n>"] = actions.move_selection_next,
  63. ["<C-p>"] = actions.move_selection_previous,
  64. ["<C-j>"] = actions.cycle_history_next,
  65. ["<C-k>"] = actions.cycle_history_prev,
  66. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  67. },
  68. },
  69. },
  70. pickers = {
  71. diagnostics = {
  72. theme = "ivy",
  73. initial_mode = "normal",
  74. layout_config = {
  75. preview_cutoff = 9999,
  76. },
  77. },
  78. },
  79. extensions = {
  80. undo = {
  81. side_by_side = true,
  82. mappings = {
  83. i = {
  84. ["<CR>"] = require("telescope-undo.actions").restore,
  85. },
  86. n = {
  87. ["<CR>"] = require("telescope-undo.actions").restore,
  88. ["y"] = require("telescope-undo.actions").yank_additions,
  89. ["Y"] = require("telescope-undo.actions").yank_deletions,
  90. ["u"] = require("telescope-undo.actions").restore,
  91. },
  92. },
  93. },
  94. ["ui-select"] = {
  95. require("telescope.themes").get_dropdown({}),
  96. },
  97. },
  98. })
  99. telescope.load_extension("fzf")
  100. telescope.load_extension("undo")
  101. telescope.load_extension("ui-select")
  102. -- We cache the results of "git rev-parse"
  103. -- Process creation is expensive in Windows, so this reduces latency
  104. local is_inside_work_tree = {}
  105. local project_files = function()
  106. local cwd = vim.fn.getcwd()
  107. if is_inside_work_tree[cwd] == nil then
  108. vim.fn.system("git rev-parse --is-inside-work-tree")
  109. is_inside_work_tree[cwd] = vim.v.shell_error == 0
  110. end
  111. if is_inside_work_tree[cwd] then
  112. builtin.git_files({ show_untracked = true, hidden = true })
  113. else
  114. builtin.find_files({})
  115. end
  116. end
  117. -- set keymaps
  118. local keymap = vim.keymap
  119. keymap.set("n", "<leader>ff", function()
  120. builtin.live_grep({ hidden = true })
  121. end, {})
  122. keymap.set("n", "<leader>p", project_files, {})
  123. keymap.set("n", "<leader>fr", builtin.resume, {})
  124. keymap.set("n", "<leader>fb", builtin.buffers, {})
  125. keymap.set("n", "<leader>fo", builtin.oldfiles, {})
  126. keymap.set("n", "<leader>m", builtin.marks, {})
  127. keymap.set("n", "<leader>cc", builtin.commands, {})
  128. keymap.set("n", "<leader>ch", builtin.command_history, {})
  129. keymap.set("n", "<leader>gb", function()
  130. builtin.git_branches({
  131. attach_mappings = function(_, map)
  132. map("i", "<c-d>", actions.git_delete_branch)
  133. map("n", "<c-d>", actions.git_delete_branch)
  134. return true
  135. end,
  136. })
  137. end, {})
  138. keymap.set("n", "<leader>gc", builtin.git_commits, {})
  139. keymap.set("n", "<leader>vh", builtin.help_tags, {})
  140. keymap.set("n", "<leader>ds", builtin.lsp_document_symbols, {})
  141. keymap.set("n", "<leader>ws", builtin.lsp_workspace_symbols, {})
  142. keymap.set("n", "<leader>dws", builtin.lsp_dynamic_workspace_symbols, {})
  143. keymap.set("n", "<leader>gf", function()
  144. builtin.grep_string({
  145. search = vim.fn.expand("<cword>"),
  146. })
  147. end)
  148. keymap.set("n", "<leader>gF", function()
  149. builtin.grep_string({
  150. search = vim.fn.expand("<cWORD>"),
  151. })
  152. end)
  153. keymap.set("n", "<leader>gD", function()
  154. builtin.find_files({
  155. search_file = vim.fn.expand("<cword>"),
  156. })
  157. end)
  158. vim.keymap.set("n", "<leader>u", "<cmd>Telescope undo<cr>")
  159. end,
  160. }