Skip to content

Commit ff0445d

Browse files
committed
update
1 parent 8921b60 commit ff0445d

File tree

2 files changed

+222
-4
lines changed

2 files changed

+222
-4
lines changed

lua/custom/plugins/init.lua

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,42 @@ local opts = { noremap = true, silent = true }
3333
map('n', ';', ':', { desc = '' })
3434
-- buffer control
3535
map('n', '<Tab>', '<cmd>bnext<CR>', { desc = 'Next buffer' })
36-
map('n', '<Leader>x', '<cmd>bdelete<CR>', { desc = 'delete buffer' })
36+
map('n', '<Leader>x', function()
37+
local cur_win = vim.api.nvim_get_current_win()
38+
local cur_buf = vim.api.nvim_get_current_buf()
39+
local cur_ft = vim.bo[cur_buf].filetype
40+
41+
local wins = vim.api.nvim_tabpage_list_wins(0)
42+
local non_float = {}
43+
for _, win in ipairs(wins) do
44+
if vim.api.nvim_win_get_config(win).relative == '' then
45+
non_float[#non_float + 1] = win
46+
end
47+
end
48+
49+
local function is_neotree_win(win)
50+
local buf = vim.api.nvim_win_get_buf(win)
51+
return vim.bo[buf].filetype == 'neo-tree'
52+
end
53+
54+
if cur_ft == 'neo-tree' then
55+
if #non_float <= 1 then
56+
vim.cmd 'enew'
57+
return
58+
end
59+
vim.api.nvim_win_close(cur_win, true)
60+
return
61+
end
62+
63+
if #non_float == 2 then
64+
local other = non_float[1] == cur_win and non_float[2] or non_float[1]
65+
if is_neotree_win(other) then
66+
vim.api.nvim_win_close(other, true)
67+
end
68+
end
69+
70+
vim.cmd 'bdelete'
71+
end, { desc = 'Delete buffer' })
3772

3873
-- telescope
3974
map('n', '<Leader>tc', '<cmd>Telescope colorscheme<CR>', { desc = '[T]elescope [C]olorscheme' })

lua/custom/plugins/snacks.lua

Lines changed: 186 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,98 @@
1+
local function titleize(action)
2+
if type(action) ~= 'string' then
3+
return nil
4+
end
5+
if action == '' then
6+
return nil
7+
end
8+
local out = action:gsub('_', ' ')
9+
return out:sub(1, 1):upper() .. out:sub(2)
10+
end
11+
12+
local function action_desc(action)
13+
if type(action) == 'string' then
14+
return titleize(action)
15+
end
16+
if type(action) == 'function' then
17+
return 'Custom action'
18+
end
19+
if type(action) ~= 'table' then
20+
return nil
21+
end
22+
23+
if action.desc then
24+
return action.desc
25+
end
26+
27+
if action.action then
28+
return action_desc(action.action)
29+
end
30+
31+
local is_list = vim.islist or vim.tbl_islist
32+
if is_list and is_list(action) then
33+
local parts = {}
34+
for _, item in ipairs(action) do
35+
local desc = action_desc(item)
36+
if desc then
37+
parts[#parts + 1] = desc
38+
end
39+
end
40+
if #parts > 0 then
41+
return table.concat(parts, ', ')
42+
end
43+
end
44+
45+
return nil
46+
end
47+
48+
local function add_desc_to_picker_keys(opts)
49+
local function apply(keys)
50+
if not keys then
51+
return
52+
end
53+
for _, spec in pairs(keys) do
54+
if type(spec) == 'table' and spec.desc == nil then
55+
local action = spec[1] or spec[2]
56+
local desc = action_desc(action)
57+
if desc then
58+
spec.desc = desc
59+
end
60+
end
61+
end
62+
end
63+
64+
for _, win in pairs(opts.win or {}) do
65+
apply(win.keys)
66+
end
67+
for _, source in pairs(opts.sources or {}) do
68+
if type(source) == 'table' and source.win then
69+
for _, win in pairs(source.win) do
70+
apply(win.keys)
71+
end
72+
end
73+
end
74+
end
75+
76+
local function ensure_dashboard_desc_keymaps()
77+
if vim.g.snacks_dashboard_desc_keymaps then
78+
return
79+
end
80+
vim.g.snacks_dashboard_desc_keymaps = true
81+
82+
local group = vim.api.nvim_create_augroup('snacks_dashboard_desc', { clear = true })
83+
vim.api.nvim_create_autocmd('FileType', {
84+
group = group,
85+
pattern = 'snacks_dashboard',
86+
callback = function(ev)
87+
vim.keymap.set('n', 'q', '<cmd>bd<cr>', { buffer = ev.buf, silent = true, desc = 'Close dashboard' })
88+
local cfg = vim.api.nvim_win_get_config(0)
89+
if cfg and cfg.relative ~= '' then
90+
vim.keymap.set('n', '<esc>', '<cmd>bd<cr>', { buffer = ev.buf, silent = true, desc = 'Close dashboard' })
91+
end
92+
end,
93+
})
94+
end
95+
196
return {
297
'folke/snacks.nvim',
398
priority = 1000,
@@ -8,16 +103,104 @@ return {
8103
-- or leave it empty to use the default settings
9104
-- refer to the configuration section below
10105
bigfile = { enabled = true },
11-
dashboard = { enabled = true },
106+
dashboard = {
107+
enabled = true,
108+
config = function()
109+
ensure_dashboard_desc_keymaps()
110+
end,
111+
},
12112
explorer = { enabled = true },
13113
indent = { enabled = true },
14114
input = { enabled = true },
15-
picker = { enabled = true },
16-
notifier = { enabled = true },
115+
picker = {
116+
enabled = true,
117+
config = function(opts)
118+
add_desc_to_picker_keys(opts)
119+
end,
120+
},
121+
notifier = {
122+
enabled = true,
123+
top_down = false,
124+
},
17125
quickfile = { enabled = true },
18126
scope = { enabled = true },
19127
scroll = { enabled = false },
20128
statuscolumn = { enabled = false },
21129
words = { enabled = true },
130+
131+
styles = {
132+
input = {
133+
keys = {
134+
n_esc = {
135+
'<esc>',
136+
{ 'cmp_close', 'cancel' },
137+
mode = 'n',
138+
expr = true,
139+
desc = 'Cancel input',
140+
},
141+
i_esc = {
142+
'<esc>',
143+
{ 'cmp_close', 'stopinsert' },
144+
mode = 'i',
145+
expr = true,
146+
desc = 'Cancel input',
147+
},
148+
i_cr = {
149+
'<cr>',
150+
{ 'cmp_accept', 'confirm' },
151+
mode = { 'i', 'n' },
152+
expr = true,
153+
desc = 'Confirm input',
154+
},
155+
i_tab = {
156+
'<tab>',
157+
{ 'cmp_select_next', 'cmp' },
158+
mode = 'i',
159+
expr = true,
160+
desc = 'Next completion',
161+
},
162+
i_ctrl_w = {
163+
'<c-w>',
164+
'<c-s-w>',
165+
mode = 'i',
166+
expr = true,
167+
desc = 'Delete word',
168+
},
169+
i_up = {
170+
'<up>',
171+
{ 'hist_up' },
172+
mode = { 'i', 'n' },
173+
desc = 'History up',
174+
},
175+
i_down = {
176+
'<down>',
177+
{ 'hist_down' },
178+
mode = { 'i', 'n' },
179+
desc = 'History down',
180+
},
181+
q = { 'q', 'cancel', desc = 'Cancel input' },
182+
},
183+
},
184+
terminal = {
185+
keys = {
186+
q = { 'q', 'hide', desc = 'Hide terminal' },
187+
gf = {
188+
'gf',
189+
function(self)
190+
local f = vim.fn.findfile(vim.fn.expand '<cfile>', '**')
191+
if f == '' then
192+
Snacks.notify.warn 'No file under cursor'
193+
else
194+
self:hide()
195+
vim.schedule(function()
196+
vim.cmd('e ' .. f)
197+
end)
198+
end
199+
end,
200+
desc = 'Open file under cursor',
201+
},
202+
},
203+
},
204+
},
22205
},
23206
}

0 commit comments

Comments
 (0)