]> git.rmz.io Git - dotfiles.git/commitdiff
nvim: replace telescope with snacks.picker
authorSamir Benmendil <me@rmz.io>
Sat, 15 Feb 2025 14:45:39 +0000 (14:45 +0000)
committerSamir Benmendil <me@rmz.io>
Sun, 2 Mar 2025 16:05:27 +0000 (16:05 +0000)
nvim/lua/plugins/editor.lua
nvim/lua/plugins/picker.lua
nvim/lua/plugins/snacks.lua

index deab249f45c3716114f7788d7e66a9679bd7f535..5dcdccf8deb853e82422a3bacda784b25a1734bf 100644 (file)
@@ -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.
   { "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 = {
     event = "LazyFile",
     -- stylua: ignore
     keys = {
@@ -80,8 +80,6 @@ return {
       { "[t", function() require("todo-comments").jump_prev() end, desc = "Previous Todo Comment" },
       { "<leader>xt", "<cmd>Trouble todo toggle<cr>", desc = "Todo (Trouble)" },
       { "<leader>xT", "<cmd>Trouble todo toggle filter = {tag = {TODO,FIX,FIXME}}<cr>", desc = "Todo/Fix/Fixme (Trouble)" },
       { "[t", function() require("todo-comments").jump_prev() end, desc = "Previous Todo Comment" },
       { "<leader>xt", "<cmd>Trouble todo toggle<cr>", desc = "Todo (Trouble)" },
       { "<leader>xT", "<cmd>Trouble todo toggle filter = {tag = {TODO,FIX,FIXME}}<cr>", desc = "Todo/Fix/Fixme (Trouble)" },
-      { "<leader>st", "<cmd>TodoTelescope<cr>", desc = "Todo" },
-      { "<leader>sT", "<cmd>TodoTelescope keywords=TODO,FIX,FIXME<cr>", desc = "Todo/Fix/Fixme" },
     },
     opts = {}
   },
     },
     opts = {}
   },
index 7b986199f81deb4a358fa7c2b2307e3daec130de..36b543026ebf1c5f26f0fc29a129f051b765ed39 100644 (file)
@@ -1,40 +1,17 @@
-vim.g.lazyvim_picker = "telescope"
+vim.g.lazyvim_picker = "snacks"
 
 ---@type LazyPicker
 local picker = {
 
 ---@type LazyPicker
 local picker = {
-  name = "telescope",
+  name = "snacks",
   commands = {
   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", "<a-c>", 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,
 }
 
   end,
 }
 
@@ -43,270 +20,141 @@ if not LazyVim.pick.register(picker) then
 end
 ---@type LazySpec
 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,
+  desc = "Fast and modern file picker",
+  { "folke/snacks.nvim",
+    opts = {
+      picker = {
+        win = {
+          input = {
+            keys = {
+              ["<a-c>"] = { "toggle_cwd", mode = { "n", "i" }, },
+              ["<c-t>"] = { "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 = {
     keys = {
-      -- from lazyvim
-      { "<leader>,", "<cmd>Telescope buffers show_all_buffers=true<cr>", desc = "Switch Buffer" },
-      { "<leader>/", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" },
-      { "<leader>:", "<cmd>Telescope command_history<cr>", desc = "Command History" },
+      { "<leader>,", function() Snacks.picker.buffers() end, desc = "Buffers" },
+      { "<leader>/", LazyVim.pick("grep"), desc = "Grep (Root Dir)" },
+      { "<leader>:", function() Snacks.picker.command_history() end, desc = "Command History" },
       -- { "<leader><space>", LazyVim.pick("files"), desc = "Find Files (Root Dir)" },
       -- { "<leader><space>", LazyVim.pick("files"), desc = "Find Files (Root Dir)" },
-      { "<leader><space>", false },
+      { "<leader>n", function() Snacks.picker.notifications() end, desc = "Notification History" },
       -- find
       -- find
-      {
-        "<leader>fb",
-        "<cmd>Telescope buffers sort_mru=true sort_lastused=true ignore_current_buffer=true<cr>",
-        desc = "Buffers",
-      },
+      { "<leader>fb", function() Snacks.picker.buffers() end, desc = "Buffers" },
+      { "<leader>fB", function() Snacks.picker.buffers({ hidden = true, nofile = true }) end, desc = "Buffers (all)" },
       { "<leader>fc", LazyVim.pick.config_files(), desc = "Find Config File" },
       { "<leader>fc", LazyVim.pick.config_files(), desc = "Find Config File" },
-      -- { "<leader>ff", LazyVim.pick("files"), desc = "Find Files (root dir)" },
-      { "<leader>ff", false }, -- fswitch
+      -- { "<leader>ff", LazyVim.pick("files"), desc = "Find Files (Root Dir)" },
       { "<leader>fF", LazyVim.pick("files", { root = false }), desc = "Find Files (cwd)" },
       { "<leader>fF", LazyVim.pick("files", { root = false }), desc = "Find Files (cwd)" },
-      { "<leader>fg", "<cmd>Telescope git_files<cr>", desc = "Find Files (git-files)" },
-      { "<leader>fr", "<cmd>Telescope oldfiles<cr>", desc = "Recent" },
-      { "<leader>fR", LazyVim.pick("oldfiles", { cwd = vim.uv.cwd() }), desc = "Recent (cwd)" },
+      { "<leader>fg", function() Snacks.picker.git_files() end, desc = "Find Files (git-files)" },
+      { "<leader>fr", LazyVim.pick("oldfiles"), desc = "Recent" },
+      { "<leader>fR", function() Snacks.picker.recent({ filter = { cwd = true }}) end, desc = "Recent (cwd)" },
+      { "<leader>fp", function() Snacks.picker.projects() end, desc = "Projects" },
       -- git
       -- git
-      -- { "<leader>gc", "<cmd>Telescope git_commits<CR>", desc = "Commits" },
-      -- { "<leader>gs", "<cmd>Telescope git_status<CR>", desc = "Status" },
-      { "<leader>gc", false },
-      { "<leader>gs", false },
-      -- search
-      { '<leader>s"', "<cmd>Telescope registers<cr>", desc = "Registers" },
-      { "<leader>sa", "<cmd>Telescope autocommands<cr>", desc = "Auto Commands" },
-      { "<leader>sb", "<cmd>Telescope current_buffer_fuzzy_find<cr>", desc = "Buffer" },
-      { "<leader>sc", "<cmd>Telescope command_history<cr>", desc = "Command History" },
-      { "<leader>sC", "<cmd>Telescope commands<cr>", desc = "Commands" },
-      { "<leader>sd", "<cmd>Telescope diagnostics bufnr=0<cr>", desc = "Document Diagnostics" },
-      { "<leader>sD", "<cmd>Telescope diagnostics<cr>", desc = "Workspace Diagnostics" },
+      -- { "<leader>gc", function() Snacks.picker.git_log() end, desc = "Git Log" },
+      -- { "<leader>gd", function() Snacks.picker.git_diff() end, desc = "Git Diff (hunks)" },
+      -- { "<leader>gs", function() Snacks.picker.git_status() end, desc = "Git Status" },
+      -- Grep
+      { "<leader>sb", function() Snacks.picker.lines() end, desc = "Buffer Lines" },
+      { "<leader>sB", function() Snacks.picker.grep_buffers() end, desc = "Grep Open Buffers" },
       { "<leader>sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" },
       { "<leader>sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" },
       { "<leader>sg", LazyVim.pick("live_grep"), desc = "Grep (Root Dir)" },
       { "<leader>sG", LazyVim.pick("live_grep", { root = false }), desc = "Grep (cwd)" },
-      { "<leader>sh", "<cmd>Telescope help_tags<cr>", desc = "Help Pages" },
-      { "<leader>sH", "<cmd>Telescope highlights<cr>", desc = "Search Highlight Groups" },
-      { "<leader>sj", "<cmd>Telescope jumplist<cr>", desc = "Jumplist" },
-      { "<leader>sk", "<cmd>Telescope keymaps<cr>", desc = "Key Maps" },
-      { "<leader>sl", "<cmd>Telescope loclist<cr>", desc = "Location List" },
-      { "<leader>sM", "<cmd>Telescope man_pages<cr>", desc = "Man Pages" },
-      { "<leader>sm", "<cmd>Telescope marks<cr>", desc = "Jump to Mark" },
-      { "<leader>so", "<cmd>Telescope vim_options<cr>", desc = "Options" },
-      { "<leader>sR", "<cmd>Telescope resume<cr>", desc = "Resume" },
-      { "<leader>sq", "<cmd>Telescope quickfix<cr>", desc = "Quickfix List" },
-      { "<leader>sw", LazyVim.pick("grep_string", { word_match = "-w" }), desc = "Word (Root Dir)" },
-      { "<leader>sW", LazyVim.pick("grep_string", { root = false, word_match = "-w" }), desc = "Word (cwd)" },
-      { "<leader>sw", LazyVim.pick("grep_string"), mode = "v", desc = "Selection (Root Dir)" },
-      { "<leader>sW", LazyVim.pick("grep_string", { root = false }), mode = "v", desc = "Selection (cwd)" },
-      { "<leader>uC", LazyVim.pick("colorscheme", { enable_preview = true }), desc = "Colorscheme with Preview" },
-      -- {
-      --   "<leader>ss",
-      --   LazyVim.pick("lsp_document_symbols", {
-      --     symbols = {
-      --       "Class",
-      --       "Function",
-      --       "Method",
-      --       "Constructor",
-      --       "Interface",
-      --       "Module",
-      --       "Struct",
-      --       "Trait",
-      --       "Field",
-      --       "Property",
-      --     },
-      --   }),
-      --   desc = "Goto Symbol",
-      -- },
-      { "<leader>ss", false }, -- use for snippets below
-      {
-        "<leader>sS",
-        LazyVim.pick("lsp_dynamic_workspace_symbols", {
-          symbols = {
-            "Class",
-            "Function",
-            "Method",
-            "Constructor",
-            "Interface",
-            "Module",
-            "Struct",
-            "Trait",
-            "Field",
-            "Property",
-          },
-        }),
-        desc = "Goto Symbol (Workspace)",
-      },
+      { "<leader>sp", function() Snacks.picker.lazy() end, desc = "Search for Plugin Spec" },
+      { "<leader>sw", LazyVim.pick("grep_word"), desc = "Visual selection or word (Root Dir)", mode = { "n", "x" } },
+      { "<leader>sW", LazyVim.pick("grep_word", { root = false }), desc = "Visual selection or word (cwd)", mode = { "n", "x" } },
+      -- search
+      { '<leader>s"', function() Snacks.picker.registers() end, desc = "Registers" },
+      { "<leader>sa", function() Snacks.picker.autocmds() end, desc = "Autocmds" },
+      { "<leader>sc", function() Snacks.picker.command_history() end, desc = "Command History" },
+      { "<leader>sC", function() Snacks.picker.commands() end, desc = "Commands" },
+      { "<leader>sd", function() Snacks.picker.diagnostics() end, desc = "Diagnostics" },
+      { "<leader>sh", function() Snacks.picker.help() end, desc = "Help Pages" },
+      { "<leader>sH", function() Snacks.picker.highlights() end, desc = "Highlights" },
+      { "<leader>si", function() Snacks.picker.icons() end, desc = "Icons" },
+      { "<leader>sj", function() Snacks.picker.jumps() end, desc = "Jumps" },
+      { "<leader>sk", function() Snacks.picker.keymaps() end, desc = "Keymaps" },
+      { "<leader>sl", function() Snacks.picker.loclist() end, desc = "Location List" },
+      { "<leader>sM", function() Snacks.picker.man() end, desc = "Man Pages" },
+      { "<leader>sm", function() Snacks.picker.marks() end, desc = "Marks" },
+      { "<leader>sR", function() Snacks.picker.resume() end, desc = "Resume" },
+      { "<leader>sq", function() Snacks.picker.qflist() end, desc = "Quickfix List" },
+      { "<leader>su", function() Snacks.picker.undo() end, desc = "Undotree" },
+      -- ui
+      { "<leader>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 = {
-              ["<c-t>"] = open_with_trouble,
-              ["<a-t>"] = open_with_trouble,
-              ["<a-i>"] = find_files_no_ignore,
-              ["<a-h>"] = find_files_with_hidden,
-              ["<C-Down>"] = actions.cycle_history_next,
-              ["<C-Up>"] = actions.cycle_history_prev,
-              ["<C-f>"] = actions.preview_scrolling_down,
-              ["<C-b>"] = 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 = { ["<c-s>"] = 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()
     opts = function()
-      if LazyVim.pick.want() ~= "telescope" then
-        return
-      end
       local Keys = require("lazyvim.plugins.lsp.keymaps").get()
       -- stylua: ignore
       vim.list_extend(Keys, {
       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", "<cmd>Telescope lsp_references<cr>", 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" },
+        { "<leader>ss", function() Snacks.picker.lsp_symbols({ filter = LazyVim.config.kind_filter }) end, desc = "LSP Symbols", has = "documentSymbol" },
+        { "<leader>sS", function() Snacks.picker.lsp_workspace_symbols({ filter = LazyVim.config.kind_filter }) end, desc = "LSP Workspace Symbols", has = "workspace/symbols" },
       })
     end,
   },
       })
     end,
   },
-
-  {
-    "benfowler/telescope-luasnip.nvim",
-    dependencies = {
-      "telescope.nvim",
+  { "folke/todo-comments.nvim",
+    optional = true,
+    -- stylua: ignore
+    keys = {
+      { "<leader>st", function() Snacks.picker.todo_comments() end, desc = "Todo" },
+      { "<leader>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 = {
+                  ["<a-s>"] = { "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 = { { "<leader>ss", "<cmd>Telescope luasnip<cr>", desc = "Snippets" } },
   },
 }
   },
 }
index 2c7f6a87d16507a370ce4d5d1726f4746a6de19a..63e12ab4a3f73339d1c382a8be6ab9f809115cdc 100644 (file)
@@ -37,9 +37,6 @@ return {
       },
       dashboard = {
         preset = {
       },
       dashboard = {
         preset = {
-          pick = function(cmd, opts)
-            return LazyVim.pick(cmd, opts)()
-          end,
           -- TODO: replace header
           header = [[
           ██╗      █████╗ ██╗   ██╗███████╗██╗   ██╗██╗   ██╗██╗███╗   ███╗          Z
           -- TODO: replace header
           header = [[
           ██╗      █████╗ ██╗   ██╗███████╗██╗   ██╗██╗   ██╗██╗███╗   ███╗          Z