From 4eda6b5272f7fe9b5fabb8e5c13ef4567004b865 Mon Sep 17 00:00:00 2001 From: Samir Benmendil Date: Sat, 15 Feb 2025 14:45:39 +0000 Subject: [PATCH] nvim: replace telescope with snacks.picker --- nvim/lua/plugins/editor.lua | 4 +- nvim/lua/plugins/picker.lua | 402 +++++++++++------------------------- nvim/lua/plugins/snacks.lua | 3 - 3 files changed, 126 insertions(+), 283 deletions(-) diff --git a/nvim/lua/plugins/editor.lua b/nvim/lua/plugins/editor.lua index deab249..5dcdccf 100644 --- a/nvim/lua/plugins/editor.lua +++ b/nvim/lua/plugins/editor.lua @@ -72,7 +72,7 @@ return { { "folke/todo-comments.nvim", -- Finds and lists all of the TODO, HACK, BUG, etc comment -- in your project and loads them into a browsable list. - cmd = { "TodoTrouble", "TodoTelescope" }, + cmd = { "TodoTrouble"}, event = "LazyFile", -- stylua: ignore keys = { @@ -80,8 +80,6 @@ return { { "[t", function() require("todo-comments").jump_prev() end, desc = "Previous Todo Comment" }, { "xt", "Trouble todo toggle", desc = "Todo (Trouble)" }, { "xT", "Trouble todo toggle filter = {tag = {TODO,FIX,FIXME}}", desc = "Todo/Fix/Fixme (Trouble)" }, - { "st", "TodoTelescope", desc = "Todo" }, - { "sT", "TodoTelescope keywords=TODO,FIX,FIXME", desc = "Todo/Fix/Fixme" }, }, opts = {} }, diff --git a/nvim/lua/plugins/picker.lua b/nvim/lua/plugins/picker.lua index 7b98619..36b5430 100644 --- a/nvim/lua/plugins/picker.lua +++ b/nvim/lua/plugins/picker.lua @@ -1,40 +1,17 @@ -vim.g.lazyvim_picker = "telescope" +vim.g.lazyvim_picker = "snacks" ---@type LazyPicker local picker = { - name = "telescope", + name = "snacks", commands = { - files = "find_files", + files = "files", + live_grep = "grep", + oldfiles = "recent", }, - -- 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) + ---@param source string + ---@param opts? snacks.picker.Config + open = function(source, opts) + return Snacks.picker.pick(source, opts) end, } @@ -43,270 +20,141 @@ if not LazyVim.pick.register(picker) then 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, + desc = "Fast and modern file picker", + { "folke/snacks.nvim", + opts = { + picker = { + win = { + input = { + keys = { + [""] = { "toggle_cwd", mode = { "n", "i" }, }, + [""] = { "trouble_open", mode = { "n", "i" }, }, + }, + }, + }, + actions = { + ---@param p snacks.Picker + toggle_cwd = function(p) + local root = LazyVim.root({ buf = p.input.filter.current_buf, normalize = true }) + local cwd = vim.fs.normalize((vim.uv or vim.loop).cwd() or ".") + local current = p:cwd() + p:set_cwd(current == root and cwd or root) + p:find() + end, + actions = require("trouble.sources.snacks").actions, + }, }, }, + -- stylua: ignore 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" }, + { ",", function() Snacks.picker.buffers() end, desc = "Buffers" }, + { "/", LazyVim.pick("grep"), desc = "Grep (Root Dir)" }, + { ":", function() Snacks.picker.command_history() end, desc = "Command History" }, -- { "", LazyVim.pick("files"), desc = "Find Files (Root Dir)" }, - { "", false }, + { "n", function() Snacks.picker.notifications() end, desc = "Notification History" }, -- find - { - "fb", - "Telescope buffers sort_mru=true sort_lastused=true ignore_current_buffer=true", - desc = "Buffers", - }, + { "fb", function() Snacks.picker.buffers() end, desc = "Buffers" }, + { "fB", function() Snacks.picker.buffers({ hidden = true, nofile = true }) end, desc = "Buffers (all)" }, { "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"), desc = "Find Files (Root Dir)" }, { "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)" }, + { "fg", function() Snacks.picker.git_files() end, desc = "Find Files (git-files)" }, + { "fr", LazyVim.pick("oldfiles"), desc = "Recent" }, + { "fR", function() Snacks.picker.recent({ filter = { cwd = true }}) end, desc = "Recent (cwd)" }, + { "fp", function() Snacks.picker.projects() end, desc = "Projects" }, -- 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" }, + -- { "gc", function() Snacks.picker.git_log() end, desc = "Git Log" }, + -- { "gd", function() Snacks.picker.git_diff() end, desc = "Git Diff (hunks)" }, + -- { "gs", function() Snacks.picker.git_status() end, desc = "Git Status" }, + -- Grep + { "sb", function() Snacks.picker.lines() end, desc = "Buffer Lines" }, + { "sB", function() Snacks.picker.grep_buffers() end, desc = "Grep Open Buffers" }, { "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)", - }, + { "sp", function() Snacks.picker.lazy() end, desc = "Search for Plugin Spec" }, + { "sw", LazyVim.pick("grep_word"), desc = "Visual selection or word (Root Dir)", mode = { "n", "x" } }, + { "sW", LazyVim.pick("grep_word", { root = false }), desc = "Visual selection or word (cwd)", mode = { "n", "x" } }, + -- search + { 's"', function() Snacks.picker.registers() end, desc = "Registers" }, + { "sa", function() Snacks.picker.autocmds() end, desc = "Autocmds" }, + { "sc", function() Snacks.picker.command_history() end, desc = "Command History" }, + { "sC", function() Snacks.picker.commands() end, desc = "Commands" }, + { "sd", function() Snacks.picker.diagnostics() end, desc = "Diagnostics" }, + { "sh", function() Snacks.picker.help() end, desc = "Help Pages" }, + { "sH", function() Snacks.picker.highlights() end, desc = "Highlights" }, + { "si", function() Snacks.picker.icons() end, desc = "Icons" }, + { "sj", function() Snacks.picker.jumps() end, desc = "Jumps" }, + { "sk", function() Snacks.picker.keymaps() end, desc = "Keymaps" }, + { "sl", function() Snacks.picker.loclist() end, desc = "Location List" }, + { "sM", function() Snacks.picker.man() end, desc = "Man Pages" }, + { "sm", function() Snacks.picker.marks() end, desc = "Marks" }, + { "sR", function() Snacks.picker.resume() end, desc = "Resume" }, + { "sq", function() Snacks.picker.qflist() end, desc = "Quickfix List" }, + { "su", function() Snacks.picker.undo() end, desc = "Undotree" }, + -- ui + { "uC", function() Snacks.picker.colorschemes() end, desc = "Colorschemes" }, }, - 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", + { "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" }, + { "gd", function() Snacks.picker.lsp_definitions() end, desc = "Goto Definition", has = "definition" }, + -- { "gr", function() Snacks.picker.lsp_references() end, nowait = true, desc = "References" }, + { "gI", function() Snacks.picker.lsp_implementations() end, desc = "Goto Implementation" }, + { "gy", function() Snacks.picker.lsp_type_definitions() end, desc = "Goto T[y]pe Definition" }, + { "ss", function() Snacks.picker.lsp_symbols({ filter = LazyVim.config.kind_filter }) end, desc = "LSP Symbols", has = "documentSymbol" }, + { "sS", function() Snacks.picker.lsp_workspace_symbols({ filter = LazyVim.config.kind_filter }) end, desc = "LSP Workspace Symbols", has = "workspace/symbols" }, }) end, }, - - { - "benfowler/telescope-luasnip.nvim", - dependencies = { - "telescope.nvim", + { "folke/todo-comments.nvim", + optional = true, + -- stylua: ignore + keys = { + { "st", function() Snacks.picker.todo_comments() end, desc = "Todo" }, + { "sT", function () Snacks.picker.todo_comments({ keywords = { "TODO", "FIX", "FIXME" } }) end, desc = "Todo/Fix/Fixme" }, + }, + }, + { "folke/flash.nvim", + -- TODO: verify if I want to keep this + specs = { + { + "folke/snacks.nvim", + opts = { + picker = { + win = { + input = { + keys = { + [""] = { "flash", mode = { "n", "i" } }, + ["s"] = { "flash" }, + }, + }, + }, + actions = { + flash = function(picker) + 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 ~= "snacks_picker_list" + end, + }, + }, + action = function(match) + local idx = picker.list:row2idx(match.pos[1]) + picker.list:_move(idx, true, true) + end, + }) + end, + }, + }, + }, + }, }, - config = function() - require("telescope").load_extension("luasnip") - end, - keys = { { "ss", "Telescope luasnip", desc = "Snippets" } }, }, } diff --git a/nvim/lua/plugins/snacks.lua b/nvim/lua/plugins/snacks.lua index 2c7f6a8..63e12ab 100644 --- a/nvim/lua/plugins/snacks.lua +++ b/nvim/lua/plugins/snacks.lua @@ -37,9 +37,6 @@ return { }, dashboard = { preset = { - pick = function(cmd, opts) - return LazyVim.pick(cmd, opts)() - end, -- TODO: replace header header = [[ ██╗ █████╗ ██╗ ██╗███████╗██╗ ██╗██╗ ██╗██╗███╗ ███╗ Z -- 2.48.1