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.

181 lines
6.3 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. extensions = {
  71. undo = {
  72. side_by_side = true,
  73. mappings = {
  74. i = {
  75. ["<CR>"] = require("telescope-undo.actions").restore,
  76. },
  77. n = {
  78. ["<CR>"] = require("telescope-undo.actions").restore,
  79. ["y"] = require("telescope-undo.actions").yank_additions,
  80. ["Y"] = require("telescope-undo.actions").yank_deletions,
  81. ["u"] = require("telescope-undo.actions").restore,
  82. },
  83. },
  84. },
  85. ["ui-select"] = {
  86. require("telescope.themes").get_dropdown({}),
  87. },
  88. },
  89. })
  90. telescope.load_extension("fzf")
  91. telescope.load_extension("undo")
  92. telescope.load_extension("ui-select")
  93. -- We cache the results of "git rev-parse"
  94. -- Process creation is expensive in Windows, so this reduces latency
  95. local is_inside_work_tree = {}
  96. local project_files = function()
  97. local cwd = vim.fn.getcwd()
  98. if is_inside_work_tree[cwd] == nil then
  99. vim.fn.system("git rev-parse --is-inside-work-tree")
  100. is_inside_work_tree[cwd] = vim.v.shell_error == 0
  101. end
  102. if is_inside_work_tree[cwd] then
  103. builtin.git_files({ show_untracked = true, hidden = true })
  104. else
  105. builtin.find_files({})
  106. end
  107. end
  108. -- set keymaps
  109. local keymap = vim.keymap
  110. keymap.set("n", "<C-g>", function()
  111. builtin.live_grep({ hidden = true })
  112. end, {})
  113. keymap.set("n", "<leader>ff", function()
  114. builtin.live_grep({ hidden = true })
  115. end, {})
  116. keymap.set("n", "<C-p>", project_files, {})
  117. keymap.set("n", "<leader>p", project_files, {})
  118. keymap.set("n", "<leader>fr", builtin.resume, {})
  119. keymap.set("n", "<leader>fb", builtin.buffers, {})
  120. keymap.set("n", "<leader>fo", builtin.oldfiles, {})
  121. keymap.set("n", "<leader>m", builtin.marks, {})
  122. keymap.set("n", "<leader>cc", builtin.commands, {})
  123. keymap.set("n", "<leader>ch", builtin.command_history, {})
  124. keymap.set("n", "<leader>gb", function()
  125. builtin.git_branches({
  126. attach_mappings = function(_, map)
  127. map("i", "<c-d>", actions.git_delete_branch)
  128. map("n", "<c-d>", actions.git_delete_branch)
  129. return true
  130. end,
  131. })
  132. end, {})
  133. keymap.set("n", "<leader>gc", builtin.git_commits, {})
  134. keymap.set("n", "<leader>vh", builtin.help_tags, {})
  135. keymap.set("n", "<leader>ds", builtin.lsp_document_symbols, {})
  136. keymap.set("n", "<leader>ws", builtin.lsp_workspace_symbols, {})
  137. keymap.set("n", "<leader>dws", builtin.lsp_dynamic_workspace_symbols, {})
  138. keymap.set("n", "<leader>gf", function()
  139. builtin.grep_string({
  140. search = vim.fn.expand("<cword>"),
  141. })
  142. end)
  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>gD", function()
  149. builtin.find_files({
  150. search_file = vim.fn.expand("<cword>"),
  151. })
  152. end)
  153. vim.keymap.set("n", "<leader>u", "<cmd>Telescope undo<cr>")
  154. end,
  155. }