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.

175 lines
6.2 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 = vim.tbl_extend(
  36. "force",
  37. require("telescope.themes").get_ivy(),
  38. {
  39. file_sorter = sorters.get_fzy_sorter,
  40. color_devicons = true,
  41. buffer_previewer_maker = truncate_large_files,
  42. initial_mode = "insert",
  43. selection_strategy = "reset",
  44. sorting_strategy = "ascending",
  45. path_display = { "truncate" },
  46. mappings = {
  47. i = {
  48. ["<C-n>"] = actions.move_selection_next,
  49. ["<C-p>"] = actions.move_selection_previous,
  50. ["<C-j>"] = actions.cycle_history_next,
  51. ["<C-k>"] = actions.cycle_history_prev,
  52. ["<C-q>"] = actions.smart_send_to_qflist + actions.open_qflist,
  53. },
  54. },
  55. }
  56. ),
  57. pickers = {
  58. diagnostics = {
  59. initial_mode = "normal",
  60. layout_config = {
  61. preview_cutoff = 9999,
  62. },
  63. },
  64. },
  65. themes = {
  66. ivy = {
  67. layout_strategy = 'bottom_pane',
  68. layout_config = {
  69. height = 50,
  70. },
  71. },
  72. },
  73. extensions = {
  74. undo = {
  75. side_by_side = true,
  76. mappings = {
  77. i = {
  78. ["<CR>"] = require("telescope-undo.actions").restore,
  79. },
  80. n = {
  81. ["<CR>"] = require("telescope-undo.actions").restore,
  82. ["y"] = require("telescope-undo.actions").yank_additions,
  83. ["Y"] = require("telescope-undo.actions").yank_deletions,
  84. ["u"] = require("telescope-undo.actions").restore,
  85. },
  86. },
  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", "<leader>ff", function()
  111. require("plugins.telescope.multigrep").live_multgrep()
  112. end, {})
  113. keymap.set("n", "<leader>p", project_files, {})
  114. keymap.set("n", "<leader>fr", builtin.resume, {})
  115. keymap.set("n", "<leader>fb", builtin.buffers, {})
  116. keymap.set("n", "<leader>fo", builtin.oldfiles, {})
  117. keymap.set("n", "<leader>m", builtin.marks, {})
  118. keymap.set("n", "<leader>cc", builtin.commands, {})
  119. keymap.set("n", "<leader>ch", builtin.command_history, {})
  120. keymap.set("n", "<leader>gb", function()
  121. builtin.git_branches({
  122. attach_mappings = function(_, map)
  123. map("i", "<c-d>", actions.git_delete_branch)
  124. map("n", "<c-d>", actions.git_delete_branch)
  125. return true
  126. end,
  127. })
  128. end, {})
  129. keymap.set("n", "<leader>gc", builtin.git_commits, {})
  130. keymap.set("n", "<leader>vh", builtin.help_tags, {})
  131. keymap.set("n", "<leader>ds", builtin.lsp_document_symbols, {})
  132. keymap.set("n", "<leader>ws", builtin.lsp_workspace_symbols, {})
  133. keymap.set("n", "<leader>dws", builtin.lsp_dynamic_workspace_symbols, {})
  134. keymap.set("n", "<leader>gf", function()
  135. builtin.grep_string({
  136. search = vim.fn.expand("<cword>"),
  137. })
  138. end)
  139. keymap.set("n", "<leader>gF", function()
  140. builtin.grep_string({
  141. search = vim.fn.expand("<cWORD>"),
  142. })
  143. end)
  144. keymap.set("n", "<leader>gD", function()
  145. builtin.find_files({
  146. search_file = vim.fn.expand("<cword>"),
  147. })
  148. end)
  149. vim.keymap.set("n", "<leader>u", "<cmd>Telescope undo<cr>")
  150. end,
  151. }