From 10143274631467d949518a88cb28d27110918608 Mon Sep 17 00:00:00 2001 From: Iordanis Petkakis <12776461+dpetka2001@users.noreply.github.com> Date: Tue, 21 Jan 2025 21:41:09 +0200 Subject: [PATCH] feat(project): add Snacks picker --- lua/lazyvim/plugins/extras/util/project.lua | 118 ++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/lua/lazyvim/plugins/extras/util/project.lua b/lua/lazyvim/plugins/extras/util/project.lua index 01a25bc2..21f721b2 100644 --- a/lua/lazyvim/plugins/extras/util/project.lua +++ b/lua/lazyvim/plugins/extras/util/project.lua @@ -79,6 +79,121 @@ pick = function() } fzf_lua.fzf_exec(results, opts) + elseif LazyVim.pick.picker.name == "snacks" then + local project = require("project_nvim.project") + local history = require("project_nvim.utils.history") + local results = history.get_recent_projects() + ---@type snacks.picker.Config + Snacks.picker({ + layout = { + layout = { + box = "horizontal", + width = 0.8, + height = 0.8, + { + box = "vertical", + border = "rounded", + title = "Project picker", + { win = "input", height = 1, border = "bottom" }, + { win = "list", border = "none" }, + }, + }, + }, + win = { + input = { + keys = { + [""] = { "live_grep", mode = { "i", "n" } }, + [""] = { "oldfiles", mode = { "i", "n" } }, + [""] = { "tabedit", mode = { "i", "n" } }, + [""] = { "change_dir", mode = { "i", "n" } }, + [""] = { "delete_project", mode = { "i", "n" } }, + }, + }, + }, + finder = function() + local items = {} + for _, item in ipairs(results) do + items[#items + 1] = { + file = item, + text = item, + } + end + return items + end, + format = function(item, _) + local file = item.file + local ret = {} + local a = Snacks.picker.util.align + local icon, icon_hl = Snacks.util.icon(file.ft, "directory") + ret[#ret + 1] = { a(icon, 3), icon_hl } + ret[#ret + 1] = { " " } + ret[#ret + 1] = { a(file, 20) } + + return ret + end, + actions = { + confirm = function(_, item) + ---@diagnostic disable-next-line: missing-fields + Snacks.picker.pick("files", { + cwd = item.file, + on_show = function() + vim.schedule(function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", false) + end) + end, + }) + end, + tabedit = function(_, item) + vim.cmd("tabedit") + ---@diagnostic disable-next-line: missing-fields + Snacks.picker.pick("files", { + cwd = item.file, + on_show = function() + vim.schedule(function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", false) + end) + end, + }) + end, + live_grep = function(_, item) + Snacks.picker.pick("grep", { + cwd = item.file, + on_show = function() + vim.schedule(function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", false) + end) + end, + }) + end, + oldfiles = function(_, item) + ---@diagnostic disable-next-line: missing-fields + Snacks.picker.pick("recent", { + filter = { cwd = item.file }, + on_show = function() + vim.schedule(function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("i", true, false, true), "n", false) + end) + end, + }) + end, + change_dir = function(picker, item) + local path = item.file + picker:close() + local ok = project.set_pwd(path) + if ok then + LazyVim.info("Change project dir to " .. path) + end + end, + delete_project = function(_, item) + local path = item.file + local choice = vim.fn.confirm("Delete '" .. path .. "' project? ", "&Yes\n&No") + if choice == 1 then + history.delete_project({ value = path }) + end + pick() + end, + }, + }) end end @@ -180,5 +295,8 @@ return { key = "p", }) end, + keys = { + { "fp", pick, desc = "Projects" }, + }, }, }