From ff0445d6fd6412543125d8b884f4fdf6f1f44a9d Mon Sep 17 00:00:00 2001 From: morfize <233522679+morfize@users.noreply.github.com> Date: Mon, 9 Mar 2026 16:02:33 +0900 Subject: [PATCH] update --- lua/custom/plugins/init.lua | 37 ++++++- lua/custom/plugins/snacks.lua | 189 +++++++++++++++++++++++++++++++++- 2 files changed, 222 insertions(+), 4 deletions(-) diff --git a/lua/custom/plugins/init.lua b/lua/custom/plugins/init.lua index 3e8dfe53..d4888bb8 100644 --- a/lua/custom/plugins/init.lua +++ b/lua/custom/plugins/init.lua @@ -33,7 +33,42 @@ local opts = { noremap = true, silent = true } map('n', ';', ':', { desc = '' }) -- buffer control map('n', '', 'bnext', { desc = 'Next buffer' }) -map('n', 'x', 'bdelete', { desc = 'delete buffer' }) +map('n', 'x', function() + local cur_win = vim.api.nvim_get_current_win() + local cur_buf = vim.api.nvim_get_current_buf() + local cur_ft = vim.bo[cur_buf].filetype + + local wins = vim.api.nvim_tabpage_list_wins(0) + local non_float = {} + for _, win in ipairs(wins) do + if vim.api.nvim_win_get_config(win).relative == '' then + non_float[#non_float + 1] = win + end + end + + local function is_neotree_win(win) + local buf = vim.api.nvim_win_get_buf(win) + return vim.bo[buf].filetype == 'neo-tree' + end + + if cur_ft == 'neo-tree' then + if #non_float <= 1 then + vim.cmd 'enew' + return + end + vim.api.nvim_win_close(cur_win, true) + return + end + + if #non_float == 2 then + local other = non_float[1] == cur_win and non_float[2] or non_float[1] + if is_neotree_win(other) then + vim.api.nvim_win_close(other, true) + end + end + + vim.cmd 'bdelete' +end, { desc = 'Delete buffer' }) -- telescope map('n', 'tc', 'Telescope colorscheme', { desc = '[T]elescope [C]olorscheme' }) diff --git a/lua/custom/plugins/snacks.lua b/lua/custom/plugins/snacks.lua index 43662ec8..127b3220 100644 --- a/lua/custom/plugins/snacks.lua +++ b/lua/custom/plugins/snacks.lua @@ -1,3 +1,98 @@ +local function titleize(action) + if type(action) ~= 'string' then + return nil + end + if action == '' then + return nil + end + local out = action:gsub('_', ' ') + return out:sub(1, 1):upper() .. out:sub(2) +end + +local function action_desc(action) + if type(action) == 'string' then + return titleize(action) + end + if type(action) == 'function' then + return 'Custom action' + end + if type(action) ~= 'table' then + return nil + end + + if action.desc then + return action.desc + end + + if action.action then + return action_desc(action.action) + end + + local is_list = vim.islist or vim.tbl_islist + if is_list and is_list(action) then + local parts = {} + for _, item in ipairs(action) do + local desc = action_desc(item) + if desc then + parts[#parts + 1] = desc + end + end + if #parts > 0 then + return table.concat(parts, ', ') + end + end + + return nil +end + +local function add_desc_to_picker_keys(opts) + local function apply(keys) + if not keys then + return + end + for _, spec in pairs(keys) do + if type(spec) == 'table' and spec.desc == nil then + local action = spec[1] or spec[2] + local desc = action_desc(action) + if desc then + spec.desc = desc + end + end + end + end + + for _, win in pairs(opts.win or {}) do + apply(win.keys) + end + for _, source in pairs(opts.sources or {}) do + if type(source) == 'table' and source.win then + for _, win in pairs(source.win) do + apply(win.keys) + end + end + end +end + +local function ensure_dashboard_desc_keymaps() + if vim.g.snacks_dashboard_desc_keymaps then + return + end + vim.g.snacks_dashboard_desc_keymaps = true + + local group = vim.api.nvim_create_augroup('snacks_dashboard_desc', { clear = true }) + vim.api.nvim_create_autocmd('FileType', { + group = group, + pattern = 'snacks_dashboard', + callback = function(ev) + vim.keymap.set('n', 'q', 'bd', { buffer = ev.buf, silent = true, desc = 'Close dashboard' }) + local cfg = vim.api.nvim_win_get_config(0) + if cfg and cfg.relative ~= '' then + vim.keymap.set('n', '', 'bd', { buffer = ev.buf, silent = true, desc = 'Close dashboard' }) + end + end, + }) +end + return { 'folke/snacks.nvim', priority = 1000, @@ -8,16 +103,104 @@ return { -- or leave it empty to use the default settings -- refer to the configuration section below bigfile = { enabled = true }, - dashboard = { enabled = true }, + dashboard = { + enabled = true, + config = function() + ensure_dashboard_desc_keymaps() + end, + }, explorer = { enabled = true }, indent = { enabled = true }, input = { enabled = true }, - picker = { enabled = true }, - notifier = { enabled = true }, + picker = { + enabled = true, + config = function(opts) + add_desc_to_picker_keys(opts) + end, + }, + notifier = { + enabled = true, + top_down = false, + }, quickfile = { enabled = true }, scope = { enabled = true }, scroll = { enabled = false }, statuscolumn = { enabled = false }, words = { enabled = true }, + + styles = { + input = { + keys = { + n_esc = { + '', + { 'cmp_close', 'cancel' }, + mode = 'n', + expr = true, + desc = 'Cancel input', + }, + i_esc = { + '', + { 'cmp_close', 'stopinsert' }, + mode = 'i', + expr = true, + desc = 'Cancel input', + }, + i_cr = { + '', + { 'cmp_accept', 'confirm' }, + mode = { 'i', 'n' }, + expr = true, + desc = 'Confirm input', + }, + i_tab = { + '', + { 'cmp_select_next', 'cmp' }, + mode = 'i', + expr = true, + desc = 'Next completion', + }, + i_ctrl_w = { + '', + '', + mode = 'i', + expr = true, + desc = 'Delete word', + }, + i_up = { + '', + { 'hist_up' }, + mode = { 'i', 'n' }, + desc = 'History up', + }, + i_down = { + '', + { 'hist_down' }, + mode = { 'i', 'n' }, + desc = 'History down', + }, + q = { 'q', 'cancel', desc = 'Cancel input' }, + }, + }, + terminal = { + keys = { + q = { 'q', 'hide', desc = 'Hide terminal' }, + gf = { + 'gf', + function(self) + local f = vim.fn.findfile(vim.fn.expand '', '**') + if f == '' then + Snacks.notify.warn 'No file under cursor' + else + self:hide() + vim.schedule(function() + vim.cmd('e ' .. f) + end) + end + end, + desc = 'Open file under cursor', + }, + }, + }, + }, }, }