From dc281eb2fbfb115fb9844dd9719722efdf4ae3d7 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Sun, 9 Feb 2025 20:34:56 +0000 Subject: [PATCH] nvim: extract telescope config into picker.lua --- nvim/lua/config/options.lua | 2 - nvim/lua/plugins/editor.lua | 92 +---------- nvim/lua/plugins/picker.lua | 312 ++++++++++++++++++++++++++++++++++++ 3 files changed, 313 insertions(+), 93 deletions(-) create mode 100644 nvim/lua/plugins/picker.lua diff --git a/nvim/lua/config/options.lua b/nvim/lua/config/options.lua index d0ef34b..5fce922 100644 --- a/nvim/lua/config/options.lua +++ b/nvim/lua/config/options.lua @@ -22,5 +22,3 @@ if vim.fn.has("nvim-0.9.0") == 1 then end -- do not let markdown plugin change indent vim.g.markdown_recommended_style = 0 - -vim.g.lazyvim_picker = "telescope" diff --git a/nvim/lua/plugins/editor.lua b/nvim/lua/plugins/editor.lua index c3281fe..0b331bf 100644 --- a/nvim/lua/plugins/editor.lua +++ b/nvim/lua/plugins/editor.lua @@ -1,95 +1,5 @@ ----@type LazyPluginSpec +---@type LazySpec return { - { - "nvim-telescope/telescope.nvim", - keys = { - { "fF", LazyVim.pick("files", { cwd = false }), desc = "Find Files (cwd)" }, - -- from lazyvim - { ",", "Telescope buffers show_all_buffers=true", desc = "Switch Buffer" }, - { "/", LazyVim.pick("live_grep"), desc = "Grep (root dir)" }, - { ":", "Telescope command_history", desc = "Command History" }, - { "", LazyVim.pick("files"), desc = "Find Files (root dir)" }, - { "", false }, - -- find - { "fb", "Telescope buffers sort_lastused=true", desc = "Buffers" }, - -- { "ff", LazyVim.pick("files"), desc = "Find Files (root dir)" }, - { "ff", false }, -- fswitch - { "fF", LazyVim.pick("files", { cwd = false }), desc = "Find Files (cwd)" }, - { "fr", "Telescope oldfiles", desc = "Recent" }, - { "fR", LazyVim.pick("oldfiles", { cwd = vim.loop.cwd() }), desc = "Recent (cwd)" }, - -- git - -- { "gc", "Telescope git_commits", desc = "commits" }, - { "gc", false }, - -- { "gs", "Telescope git_status", desc = "status" }, - { "gs", false }, - -- search - { "sa", "Telescope autocommands", desc = "Auto Commands" }, - { "sb", "Telescope current_buffer_fuzzy_find", desc = "Buffer" }, - { "sc", "Telescope command_history", desc = "Command History" }, - { "sC", "Telescope commands", desc = "Commands" }, - { "sd", "Telescope diagnostics bufnr=0", desc = "Document diagnostics" }, - { "sD", "Telescope diagnostics", desc = "Workspace diagnostics" }, - { "sg", LazyVim.pick("live_grep"), desc = "Grep (root dir)" }, - { "sG", LazyVim.pick("live_grep", { cwd = false }), desc = "Grep (cwd)" }, - { "sh", "Telescope help_tags", desc = "Help Pages" }, - { "sH", "Telescope highlights", desc = "Search Highlight Groups" }, - { "sk", "Telescope keymaps", desc = "Key Maps" }, - { "sM", "Telescope man_pages", desc = "Man Pages" }, - { "sm", "Telescope marks", desc = "Jump to Mark" }, - { "so", "Telescope vim_options", desc = "Options" }, - { "sR", "Telescope resume", desc = "Resume" }, - { "sw", LazyVim.pick("grep_string"), desc = "Word (root dir)" }, - { "sW", LazyVim.pick("grep_string", { cwd = false }), desc = "Word (cwd)" }, - { "uC", LazyVim.pick("colorscheme", { enable_preview = true }), desc = "Colorscheme with preview" }, - -- { - -- "ss", - -- LazyVim.pick("lsp_document_symbols", { - -- symbols = { - -- "Class", - -- "Function", - -- "Method", - -- "Constructor", - -- "Interface", - -- "Module", - -- "Struct", - -- "Trait", - -- "Field", - -- "Property", - -- }, - -- }), - -- desc = "Goto Symbol", - -- }, - { "ss", false }, -- use for snippets below - { - "sS", - LazyVim.pick("lsp_dynamic_workspace_symbols", { - symbols = { - "Class", - "Function", - "Method", - "Constructor", - "Interface", - "Module", - "Struct", - "Trait", - "Field", - "Property", - }, - }), - desc = "Goto Symbol (Workspace)", - }, - }, - }, - { - "benfowler/telescope-luasnip.nvim", - dependencies = { - "telescope.nvim", - }, - config = function() - require("telescope").load_extension("luasnip") - end, - keys = { { "ss", "Telescope luasnip", desc = "Snippets" } }, - }, { "stevearc/oil.nvim", opts = { diff --git a/nvim/lua/plugins/picker.lua b/nvim/lua/plugins/picker.lua new file mode 100644 index 0000000..7b98619 --- /dev/null +++ b/nvim/lua/plugins/picker.lua @@ -0,0 +1,312 @@ +vim.g.lazyvim_picker = "telescope" + +---@type LazyPicker +local picker = { + name = "telescope", + commands = { + files = "find_files", + }, + -- this will return a function that calls telescope. + -- cwd will default to lazyvim.util.get_root + -- for `files`, git_files or find_files will be chosen depending on .git + ---@param builtin string + ---@param opts? lazyvim.util.pick.Opts + open = function(builtin, opts) + opts = opts or {} + opts.follow = opts.follow ~= false + if opts.cwd and opts.cwd ~= vim.uv.cwd() then + local function open_cwd_dir() + local action_state = require("telescope.actions.state") + local line = action_state.get_current_line() + LazyVim.pick.open( + builtin, + vim.tbl_deep_extend("force", {}, opts or {}, { + root = false, + default_text = line, + }) + ) + end + ---@diagnostic disable-next-line: inject-field + opts.attach_mappings = function(_, map) + -- opts.desc is overridden by telescope, until it's changed there is this fix + map("i", "", open_cwd_dir, { desc = "Open cwd Directory" }) + return true + end + end + + require("telescope.builtin")[builtin](opts) + end, +} + +if not LazyVim.pick.register(picker) then + return {} +end +---@type LazySpec +return { + { "nvim-telescope/telescope.nvim", + cmd = "Telescope", + version = false, -- telescope did only one release, so use HEAD for now + dependencies = { + { "nvim-telescope/telescope-fzf-native.nvim", + build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build", + config = function(plugin) + LazyVim.on_load("telescope.nvim", function() + local ok, err = pcall(require("telescope").load_extension, "fzf") + if not ok then + local lib = plugin.dir .. "/build/libfzf." .. (LazyVim.is_win() and "dll" or "so") + if not vim.uv.fs_stat(lib) then + LazyVim.warn("`telescope-fzf-native.nvim` not built. Rebuilding...") + require("lazy").build({ plugins = { plugin }, show = false }):wait(function() + LazyVim.info("Rebuilding `telescope-fzf-native.nvim` done.\nPlease restart Neovim.") + end) + else + LazyVim.error("Failed to load `telescope-fzf-native.nvim`:\n" .. err) + end + end + end) + end, + }, + }, + keys = { + -- from lazyvim + { ",", "Telescope buffers show_all_buffers=true", desc = "Switch Buffer" }, + { "/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { ":", "Telescope command_history", desc = "Command History" }, + -- { "", LazyVim.pick("files"), desc = "Find Files (Root Dir)" }, + { "", false }, + -- find + { + "fb", + "Telescope buffers sort_mru=true sort_lastused=true ignore_current_buffer=true", + desc = "Buffers", + }, + { "fc", LazyVim.pick.config_files(), desc = "Find Config File" }, + -- { "ff", LazyVim.pick("files"), desc = "Find Files (root dir)" }, + { "ff", false }, -- fswitch + { "fF", LazyVim.pick("files", { root = false }), desc = "Find Files (cwd)" }, + { "fg", "Telescope git_files", desc = "Find Files (git-files)" }, + { "fr", "Telescope oldfiles", desc = "Recent" }, + { "fR", LazyVim.pick("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" }, + -- git + -- { "gc", "Telescope git_commits", desc = "Commits" }, + -- { "gs", "Telescope git_status", desc = "Status" }, + { "gc", false }, + { "gs", false }, + -- search + { 's"', "Telescope registers", desc = "Registers" }, + { "sa", "Telescope autocommands", desc = "Auto Commands" }, + { "sb", "Telescope current_buffer_fuzzy_find", desc = "Buffer" }, + { "sc", "Telescope command_history", desc = "Command History" }, + { "sC", "Telescope commands", desc = "Commands" }, + { "sd", "Telescope diagnostics bufnr=0", desc = "Document Diagnostics" }, + { "sD", "Telescope diagnostics", desc = "Workspace Diagnostics" }, + { "sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" }, + { "sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" }, + { "sh", "Telescope help_tags", desc = "Help Pages" }, + { "sH", "Telescope highlights", desc = "Search Highlight Groups" }, + { "sj", "Telescope jumplist", desc = "Jumplist" }, + { "sk", "Telescope keymaps", desc = "Key Maps" }, + { "sl", "Telescope loclist", desc = "Location List" }, + { "sM", "Telescope man_pages", desc = "Man Pages" }, + { "sm", "Telescope marks", desc = "Jump to Mark" }, + { "so", "Telescope vim_options", desc = "Options" }, + { "sR", "Telescope resume", desc = "Resume" }, + { "sq", "Telescope quickfix", desc = "Quickfix List" }, + { "sw", LazyVim.pick("grep_string", { word_match = "-w" }), desc = "Word (Root Dir)" }, + { "sW", LazyVim.pick("grep_string", { root = false, word_match = "-w" }), desc = "Word (cwd)" }, + { "sw", LazyVim.pick("grep_string"), mode = "v", desc = "Selection (Root Dir)" }, + { "sW", LazyVim.pick("grep_string", { root = false }), mode = "v", desc = "Selection (cwd)" }, + { "uC", LazyVim.pick("colorscheme", { enable_preview = true }), desc = "Colorscheme with Preview" }, + -- { + -- "ss", + -- LazyVim.pick("lsp_document_symbols", { + -- symbols = { + -- "Class", + -- "Function", + -- "Method", + -- "Constructor", + -- "Interface", + -- "Module", + -- "Struct", + -- "Trait", + -- "Field", + -- "Property", + -- }, + -- }), + -- desc = "Goto Symbol", + -- }, + { "ss", false }, -- use for snippets below + { + "sS", + LazyVim.pick("lsp_dynamic_workspace_symbols", { + symbols = { + "Class", + "Function", + "Method", + "Constructor", + "Interface", + "Module", + "Struct", + "Trait", + "Field", + "Property", + }, + }), + desc = "Goto Symbol (Workspace)", + }, + }, + opts = function() + local actions = require("telescope.actions") + + local open_with_trouble = function(...) + return require("trouble.sources.telescope").open(...) + end + local find_files_no_ignore = function() + local action_state = require("telescope.actions.state") + local line = action_state.get_current_line() + LazyVim.pick("find_files", { no_ignore = true, default_text = line })() + end + local find_files_with_hidden = function() + local action_state = require("telescope.actions.state") + local line = action_state.get_current_line() + LazyVim.pick("find_files", { hidden = true, default_text = line })() + end + + local function find_command() + if 1 == vim.fn.executable("rg") then + return { "rg", "--files", "--color", "never", "-g", "!.git" } + elseif 1 == vim.fn.executable("fd") then + return { "fd", "--type", "f", "--color", "never", "-E", ".git" } + elseif 1 == vim.fn.executable("fdfind") then + return { "fdfind", "--type", "f", "--color", "never", "-E", ".git" } + elseif 1 == vim.fn.executable("find") and vim.fn.has("win32") == 0 then + return { "find", ".", "-type", "f" } + elseif 1 == vim.fn.executable("where") then + return { "where", "/r", ".", "*" } + end + end + + return { + defaults = { + prompt_prefix = " ", + selection_caret = " ", + -- open files in the first window that is an actual file. + -- use the current window if no other window is available. + get_selection_window = function() + local wins = vim.api.nvim_list_wins() + table.insert(wins, 1, vim.api.nvim_get_current_win()) + for _, win in ipairs(wins) do + local buf = vim.api.nvim_win_get_buf(win) + if vim.bo[buf].buftype == "" then + return win + end + end + return 0 + end, + mappings = { + i = { + [""] = open_with_trouble, + [""] = open_with_trouble, + [""] = find_files_no_ignore, + [""] = find_files_with_hidden, + [""] = actions.cycle_history_next, + [""] = actions.cycle_history_prev, + [""] = actions.preview_scrolling_down, + [""] = actions.preview_scrolling_up, + }, + n = { + ["q"] = actions.close, + }, + }, + }, + pickers = { + find_files = { + find_command = find_command, + hidden = true, + }, + }, + } + end, + }, + + -- Flash Telescope config + { + "nvim-telescope/telescope.nvim", + optional = true, + opts = function(_, opts) + if not LazyVim.has("flash.nvim") then + return + end + local function flash(prompt_bufnr) + require("flash").jump({ + pattern = "^", + label = { after = { 0, 0 } }, + search = { + mode = "search", + exclude = { + function(win) + return vim.bo[vim.api.nvim_win_get_buf(win)].filetype ~= "TelescopeResults" + end, + }, + }, + action = function(match) + local picker = require("telescope.actions.state").get_current_picker(prompt_bufnr) + picker:set_selection(match.pos[1] - 1) + end, + }) + end + opts.defaults = vim.tbl_deep_extend("force", opts.defaults or {}, { + mappings = { n = { s = flash }, i = { [""] = flash } }, + }) + end, + }, + + -- better vim.ui with telescope + { + "stevearc/dressing.nvim", + lazy = true, + enabled = function() + return LazyVim.pick.want() == "telescope" + end, + init = function() + ---@diagnostic disable-next-line: duplicate-set-field + vim.ui.select = function(...) + require("lazy").load({ plugins = { "dressing.nvim" } }) + return vim.ui.select(...) + end + ---@diagnostic disable-next-line: duplicate-set-field + vim.ui.input = function(...) + require("lazy").load({ plugins = { "dressing.nvim" } }) + return vim.ui.input(...) + end + end, + }, + + { + "neovim/nvim-lspconfig", + opts = function() + if LazyVim.pick.want() ~= "telescope" then + return + end + local Keys = require("lazyvim.plugins.lsp.keymaps").get() + -- stylua: ignore + vim.list_extend(Keys, { + { "gd", function() require("telescope.builtin").lsp_definitions({ reuse_win = true }) end, desc = "Goto Definition", has = "definition" }, + { "gr", "Telescope lsp_references", desc = "References", nowait = true }, + { "gI", function() require("telescope.builtin").lsp_implementations({ reuse_win = true }) end, desc = "Goto Implementation" }, + { "gy", function() require("telescope.builtin").lsp_type_definitions({ reuse_win = true }) end, desc = "Goto T[y]pe Definition" }, + }) + end, + }, + + { + "benfowler/telescope-luasnip.nvim", + dependencies = { + "telescope.nvim", + }, + config = function() + require("telescope").load_extension("luasnip") + end, + keys = { { "ss", "Telescope luasnip", desc = "Snippets" } }, + }, +} -- 2.48.1