]> git.rmz.io Git - dotfiles.git/blob - nvim/lua/plugins/picker.lua
nvim: extract telescope config into picker.lua
[dotfiles.git] / nvim / lua / plugins / picker.lua
1 vim.g.lazyvim_picker = "telescope"
2
3 ---@type LazyPicker
4 local picker = {
5 name = "telescope",
6 commands = {
7 files = "find_files",
8 },
9 -- this will return a function that calls telescope.
10 -- cwd will default to lazyvim.util.get_root
11 -- for `files`, git_files or find_files will be chosen depending on .git
12 ---@param builtin string
13 ---@param opts? lazyvim.util.pick.Opts
14 open = function(builtin, opts)
15 opts = opts or {}
16 opts.follow = opts.follow ~= false
17 if opts.cwd and opts.cwd ~= vim.uv.cwd() then
18 local function open_cwd_dir()
19 local action_state = require("telescope.actions.state")
20 local line = action_state.get_current_line()
21 LazyVim.pick.open(
22 builtin,
23 vim.tbl_deep_extend("force", {}, opts or {}, {
24 root = false,
25 default_text = line,
26 })
27 )
28 end
29 ---@diagnostic disable-next-line: inject-field
30 opts.attach_mappings = function(_, map)
31 -- opts.desc is overridden by telescope, until it's changed there is this fix
32 map("i", "<a-c>", open_cwd_dir, { desc = "Open cwd Directory" })
33 return true
34 end
35 end
36
37 require("telescope.builtin")[builtin](opts)
38 end,
39 }
40
41 if not LazyVim.pick.register(picker) then
42 return {}
43 end
44 ---@type LazySpec
45 return {
46 { "nvim-telescope/telescope.nvim",
47 cmd = "Telescope",
48 version = false, -- telescope did only one release, so use HEAD for now
49 dependencies = {
50 { "nvim-telescope/telescope-fzf-native.nvim",
51 build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build",
52 config = function(plugin)
53 LazyVim.on_load("telescope.nvim", function()
54 local ok, err = pcall(require("telescope").load_extension, "fzf")
55 if not ok then
56 local lib = plugin.dir .. "/build/libfzf." .. (LazyVim.is_win() and "dll" or "so")
57 if not vim.uv.fs_stat(lib) then
58 LazyVim.warn("`telescope-fzf-native.nvim` not built. Rebuilding...")
59 require("lazy").build({ plugins = { plugin }, show = false }):wait(function()
60 LazyVim.info("Rebuilding `telescope-fzf-native.nvim` done.\nPlease restart Neovim.")
61 end)
62 else
63 LazyVim.error("Failed to load `telescope-fzf-native.nvim`:\n" .. err)
64 end
65 end
66 end)
67 end,
68 },
69 },
70 keys = {
71 -- from lazyvim
72 { "<leader>,", "<cmd>Telescope buffers show_all_buffers=true<cr>", desc = "Switch Buffer" },
73 { "<leader>/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" },
74 { "<leader>:", "<cmd>Telescope command_history<cr>", desc = "Command History" },
75 -- { "<leader><space>", LazyVim.pick("files"), desc = "Find Files (Root Dir)" },
76 { "<leader><space>", false },
77 -- find
78 {
79 "<leader>fb",
80 "<cmd>Telescope buffers sort_mru=true sort_lastused=true ignore_current_buffer=true<cr>",
81 desc = "Buffers",
82 },
83 { "<leader>fc", LazyVim.pick.config_files(), desc = "Find Config File" },
84 -- { "<leader>ff", LazyVim.pick("files"), desc = "Find Files (root dir)" },
85 { "<leader>ff", false }, -- fswitch
86 { "<leader>fF", LazyVim.pick("files", { root = false }), desc = "Find Files (cwd)" },
87 { "<leader>fg", "<cmd>Telescope git_files<cr>", desc = "Find Files (git-files)" },
88 { "<leader>fr", "<cmd>Telescope oldfiles<cr>", desc = "Recent" },
89 { "<leader>fR", LazyVim.pick("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" },
90 -- git
91 -- { "<leader>gc", "<cmd>Telescope git_commits<CR>", desc = "Commits" },
92 -- { "<leader>gs", "<cmd>Telescope git_status<CR>", desc = "Status" },
93 { "<leader>gc", false },
94 { "<leader>gs", false },
95 -- search
96 { '<leader>s"', "<cmd>Telescope registers<cr>", desc = "Registers" },
97 { "<leader>sa", "<cmd>Telescope autocommands<cr>", desc = "Auto Commands" },
98 { "<leader>sb", "<cmd>Telescope current_buffer_fuzzy_find<cr>", desc = "Buffer" },
99 { "<leader>sc", "<cmd>Telescope command_history<cr>", desc = "Command History" },
100 { "<leader>sC", "<cmd>Telescope commands<cr>", desc = "Commands" },
101 { "<leader>sd", "<cmd>Telescope diagnostics bufnr=0<cr>", desc = "Document Diagnostics" },
102 { "<leader>sD", "<cmd>Telescope diagnostics<cr>", desc = "Workspace Diagnostics" },
103 { "<leader>sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" },
104 { "<leader>sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" },
105 { "<leader>sh", "<cmd>Telescope help_tags<cr>", desc = "Help Pages" },
106 { "<leader>sH", "<cmd>Telescope highlights<cr>", desc = "Search Highlight Groups" },
107 { "<leader>sj", "<cmd>Telescope jumplist<cr>", desc = "Jumplist" },
108 { "<leader>sk", "<cmd>Telescope keymaps<cr>", desc = "Key Maps" },
109 { "<leader>sl", "<cmd>Telescope loclist<cr>", desc = "Location List" },
110 { "<leader>sM", "<cmd>Telescope man_pages<cr>", desc = "Man Pages" },
111 { "<leader>sm", "<cmd>Telescope marks<cr>", desc = "Jump to Mark" },
112 { "<leader>so", "<cmd>Telescope vim_options<cr>", desc = "Options" },
113 { "<leader>sR", "<cmd>Telescope resume<cr>", desc = "Resume" },
114 { "<leader>sq", "<cmd>Telescope quickfix<cr>", desc = "Quickfix List" },
115 { "<leader>sw", LazyVim.pick("grep_string", { word_match = "-w" }), desc = "Word (Root Dir)" },
116 { "<leader>sW", LazyVim.pick("grep_string", { root = false, word_match = "-w" }), desc = "Word (cwd)" },
117 { "<leader>sw", LazyVim.pick("grep_string"), mode = "v", desc = "Selection (Root Dir)" },
118 { "<leader>sW", LazyVim.pick("grep_string", { root = false }), mode = "v", desc = "Selection (cwd)" },
119 { "<leader>uC", LazyVim.pick("colorscheme", { enable_preview = true }), desc = "Colorscheme with Preview" },
120 -- {
121 -- "<leader>ss",
122 -- LazyVim.pick("lsp_document_symbols", {
123 -- symbols = {
124 -- "Class",
125 -- "Function",
126 -- "Method",
127 -- "Constructor",
128 -- "Interface",
129 -- "Module",
130 -- "Struct",
131 -- "Trait",
132 -- "Field",
133 -- "Property",
134 -- },
135 -- }),
136 -- desc = "Goto Symbol",
137 -- },
138 { "<leader>ss", false }, -- use for snippets below
139 {
140 "<leader>sS",
141 LazyVim.pick("lsp_dynamic_workspace_symbols", {
142 symbols = {
143 "Class",
144 "Function",
145 "Method",
146 "Constructor",
147 "Interface",
148 "Module",
149 "Struct",
150 "Trait",
151 "Field",
152 "Property",
153 },
154 }),
155 desc = "Goto Symbol (Workspace)",
156 },
157 },
158 opts = function()
159 local actions = require("telescope.actions")
160
161 local open_with_trouble = function(...)
162 return require("trouble.sources.telescope").open(...)
163 end
164 local find_files_no_ignore = function()
165 local action_state = require("telescope.actions.state")
166 local line = action_state.get_current_line()
167 LazyVim.pick("find_files", { no_ignore = true, default_text = line })()
168 end
169 local find_files_with_hidden = function()
170 local action_state = require("telescope.actions.state")
171 local line = action_state.get_current_line()
172 LazyVim.pick("find_files", { hidden = true, default_text = line })()
173 end
174
175 local function find_command()
176 if 1 == vim.fn.executable("rg") then
177 return { "rg", "--files", "--color", "never", "-g", "!.git" }
178 elseif 1 == vim.fn.executable("fd") then
179 return { "fd", "--type", "f", "--color", "never", "-E", ".git" }
180 elseif 1 == vim.fn.executable("fdfind") then
181 return { "fdfind", "--type", "f", "--color", "never", "-E", ".git" }
182 elseif 1 == vim.fn.executable("find") and vim.fn.has("win32") == 0 then
183 return { "find", ".", "-type", "f" }
184 elseif 1 == vim.fn.executable("where") then
185 return { "where", "/r", ".", "*" }
186 end
187 end
188
189 return {
190 defaults = {
191 prompt_prefix = " ",
192 selection_caret = " ",
193 -- open files in the first window that is an actual file.
194 -- use the current window if no other window is available.
195 get_selection_window = function()
196 local wins = vim.api.nvim_list_wins()
197 table.insert(wins, 1, vim.api.nvim_get_current_win())
198 for _, win in ipairs(wins) do
199 local buf = vim.api.nvim_win_get_buf(win)
200 if vim.bo[buf].buftype == "" then
201 return win
202 end
203 end
204 return 0
205 end,
206 mappings = {
207 i = {
208 ["<c-t>"] = open_with_trouble,
209 ["<a-t>"] = open_with_trouble,
210 ["<a-i>"] = find_files_no_ignore,
211 ["<a-h>"] = find_files_with_hidden,
212 ["<C-Down>"] = actions.cycle_history_next,
213 ["<C-Up>"] = actions.cycle_history_prev,
214 ["<C-f>"] = actions.preview_scrolling_down,
215 ["<C-b>"] = actions.preview_scrolling_up,
216 },
217 n = {
218 ["q"] = actions.close,
219 },
220 },
221 },
222 pickers = {
223 find_files = {
224 find_command = find_command,
225 hidden = true,
226 },
227 },
228 }
229 end,
230 },
231
232 -- Flash Telescope config
233 {
234 "nvim-telescope/telescope.nvim",
235 optional = true,
236 opts = function(_, opts)
237 if not LazyVim.has("flash.nvim") then
238 return
239 end
240 local function flash(prompt_bufnr)
241 require("flash").jump({
242 pattern = "^",
243 label = { after = { 0, 0 } },
244 search = {
245 mode = "search",
246 exclude = {
247 function(win)
248 return vim.bo[vim.api.nvim_win_get_buf(win)].filetype ~= "TelescopeResults"
249 end,
250 },
251 },
252 action = function(match)
253 local picker = require("telescope.actions.state").get_current_picker(prompt_bufnr)
254 picker:set_selection(match.pos[1] - 1)
255 end,
256 })
257 end
258 opts.defaults = vim.tbl_deep_extend("force", opts.defaults or {}, {
259 mappings = { n = { s = flash }, i = { ["<c-s>"] = flash } },
260 })
261 end,
262 },
263
264 -- better vim.ui with telescope
265 {
266 "stevearc/dressing.nvim",
267 lazy = true,
268 enabled = function()
269 return LazyVim.pick.want() == "telescope"
270 end,
271 init = function()
272 ---@diagnostic disable-next-line: duplicate-set-field
273 vim.ui.select = function(...)
274 require("lazy").load({ plugins = { "dressing.nvim" } })
275 return vim.ui.select(...)
276 end
277 ---@diagnostic disable-next-line: duplicate-set-field
278 vim.ui.input = function(...)
279 require("lazy").load({ plugins = { "dressing.nvim" } })
280 return vim.ui.input(...)
281 end
282 end,
283 },
284
285 {
286 "neovim/nvim-lspconfig",
287 opts = function()
288 if LazyVim.pick.want() ~= "telescope" then
289 return
290 end
291 local Keys = require("lazyvim.plugins.lsp.keymaps").get()
292 -- stylua: ignore
293 vim.list_extend(Keys, {
294 { "gd", function() require("telescope.builtin").lsp_definitions({ reuse_win = true }) end, desc = "Goto Definition", has = "definition" },
295 { "gr", "<cmd>Telescope lsp_references<cr>", desc = "References", nowait = true },
296 { "gI", function() require("telescope.builtin").lsp_implementations({ reuse_win = true }) end, desc = "Goto Implementation" },
297 { "gy", function() require("telescope.builtin").lsp_type_definitions({ reuse_win = true }) end, desc = "Goto T[y]pe Definition" },
298 })
299 end,
300 },
301
302 {
303 "benfowler/telescope-luasnip.nvim",
304 dependencies = {
305 "telescope.nvim",
306 },
307 config = function()
308 require("telescope").load_extension("luasnip")
309 end,
310 keys = { { "<leader>ss", "<cmd>Telescope luasnip<cr>", desc = "Snippets" } },
311 },
312 }