Files
nvim/lua/plugins/picker.lua
T

170 lines
6.3 KiB
Lua

-- snacks.nvim: only picker, lazygit, terminal and image are used.
-- fff handles files and grep in the full profile; if its native library was
-- not downloaded or built (or in slim), those maps fall back to snacks.picker.
local full = vim.g.profile == "full"
local fff_built
local function use_fff()
if fff_built == nil then
fff_built = false
local plugin = full and require("lazy.core.config").plugins["fff"]
if plugin then
local ext = jit.os == "OSX" and "dylib" or "so"
fff_built = vim.uv.fs_stat(plugin.dir .. "/target/release/libfff_nvim." .. ext) ~= nil
end
end
return fff_built
end
local function files()
if use_fff() then
require("fff").find_files()
else
Snacks.picker.files()
end
end
local function grep()
if use_fff() then
require("fff").live_grep()
else
Snacks.picker.grep()
end
end
local function fuzzy_grep()
if use_fff() then
require("fff").live_grep({ grep = { modes = { "fuzzy", "plain" } } })
else
Snacks.picker.grep()
end
end
-- The dashboard centres every line on its own, so pad them all to one width to keep the art aligned.
local header = (function()
local lines = {
[[ __ __]],
[[ |__| ____ | | _________ ____ ______ ____ ___________]],
[[ | |/ _ \| |/ /\_ __ \_/ __ \ / ___// \_/ __ \_ __ \]],
[[ | ( <_> ) < | | \/\ ___/ \___ \| | \ ___/| | \/]],
[[/\__| |\____/|__|_ \ |__| \___ >____ >___| /\___ >__|]],
[[\______| \/ \/ \/ \/ \/]],
}
local width = 0
for _, line in ipairs(lines) do
width = math.max(width, #line)
end
for i, line in ipairs(lines) do
lines[i] = line .. string.rep(" ", width - #line)
end
return table.concat(lines, "\n")
end)()
-- The finder entries press the same leader maps as below, so fff and the snacks fallback stay in one place.
local dashboard = {
enabled = true,
width = 66, -- the header is 64 columns wide
preset = {
header = header,
keys = {
{ icon = " ", key = "f", desc = "Find file", action = "<leader>ff" },
{ icon = " ", key = "r", desc = "Recent files", action = "<leader>fr" },
{ icon = " ", key = "w", desc = "Find word", action = "<leader>fw" },
{ icon = " ", key = "g", desc = "Grep", action = "<leader>fg" },
{ icon = " ", key = "p", desc = "Projects", action = "<leader>fp" },
{
icon = " ",
key = "s",
desc = "Restore session",
action = function()
require("persistence").load()
end,
},
{ icon = "󰒲 ", key = "l", desc = "Lazy", action = ":Lazy" },
{ icon = " ", key = "q", desc = "Quit", action = ":qa" },
},
},
}
local keys = {
{ "<leader>ff", files, desc = "Find files" },
{ "<leader><leader>", files, desc = "Find files" },
{ "<leader>,", function() Snacks.picker.buffers() end, desc = "Buffers" },
{ "<leader>fr", function() Snacks.picker.recent() end, desc = "Recent files" },
{ "<leader>fp", function() Snacks.picker.projects() end, desc = "Projects" },
{ "<leader>fR", function() Snacks.picker.resume() end, desc = "Resume last picker" },
{ "<leader>fu", function() Snacks.picker.undo() end, desc = "Undo history" },
{ "<leader>fG", fuzzy_grep, desc = "Fuzzy grep" },
{ "<leader>gB", function() Snacks.picker.git_branches() end, desc = "Git branches" },
{ "<leader>gc", function() Snacks.picker.git_log() end, desc = "Git log" },
{ "<leader>gC", function() Snacks.picker.git_log_file() end, desc = "Git log (file)" },
{ "<leader>gx", function() Snacks.picker.git_status() end, desc = "Git status" },
{ "<leader>bd", function() Snacks.bufdelete() end, desc = "Delete buffer" },
{ "<leader>bD", function() Snacks.bufdelete({ force = true }) end, desc = "Delete buffer (force)" },
{ "<leader>bo", function() Snacks.bufdelete.other() end, desc = "Delete other buffers" },
{ "<leader>bb", "<cmd>e #<cr>", desc = "Alternate buffer" },
{ "<leader>fg", grep, desc = "Live grep" },
{ "<leader>fw", function() Snacks.picker.grep_word() end, desc = "Grep word under cursor", mode = { "n", "x" } },
{ "<leader>fb", function() Snacks.picker.buffers() end, desc = "Buffers" },
{ "<leader>fh", function() Snacks.picker.help() end, desc = "Help" },
{ "<leader>fk", function() Snacks.picker.keymaps() end, desc = "Keymaps" },
{ "<leader>fd", function() Snacks.picker.diagnostics() end, desc = "Diagnostics" },
{ "<leader>cn", function() Snacks.words.jump(vim.v.count1) end, desc = "Next reference", mode = { "n", "t" } },
{ "<leader>cp", function() Snacks.words.jump(-vim.v.count1) end, desc = "Previous reference", mode = { "n", "t" } },
{ "<C-/>", function() Snacks.terminal.toggle(nil, { win = { position = "float" } }) end, desc = "Toggle terminal", mode = { "n", "t" } },
{ "<C-_>", function() Snacks.terminal.toggle(nil, { win = { position = "float" } }) end, desc = "which_key_ignore", mode = { "n", "t" } },
}
if full then
table.insert(keys, { "<leader>fs", function() Snacks.picker.lsp_workspace_symbols() end, desc = "Workspace symbols" })
table.insert(keys, { "<leader>fo", function() Snacks.picker.lsp_symbols() end, desc = "Document symbols" })
end
if vim.fn.executable("lazygit") == 1 then
table.insert(keys, { "<leader>gg", function() Snacks.lazygit() end, desc = "Lazygit" })
end
local picker = {}
if full then
-- <a-a> sends selected files or grep hits to the running agent.
picker = {
actions = {
sidekick_send = function(...)
return require("sidekick.cli.picker.snacks").send(...)
end,
},
win = { input = { keys = { ["<a-a>"] = { "sidekick_send", mode = { "n", "i" } } } } },
}
end
return {
{
"folke/snacks.nvim",
lazy = false,
priority = 1000,
keys = keys,
opts = {
dashboard = dashboard,
picker = picker,
lazygit = {},
terminal = {},
image = { enabled = full },
words = { enabled = true },
statuscolumn = { enabled = true },
quickfile = { enabled = true },
},
config = function(_, opts)
require("snacks").setup(opts)
require("core.toggles").setup()
end,
},
{
"dmtrKovalenko/fff",
cond = full,
lazy = true,
-- Downloads the prebuilt library into target/release, falls back to cargo build.
build = function()
require("fff.download").download_or_build_binary()
end,
opts = {},
},
}