diff --git a/lua/menu/init.lua b/lua/menu/init.lua index d141cdc..2a0a227 100644 --- a/lua/menu/init.lua +++ b/lua/menu/init.lua @@ -83,11 +83,12 @@ M.open = function(items, opts) local close_post = function() state.bufs = {} state.config = nil + state.nested_menu = "" if api.nvim_win_is_valid(state.old_data.win) then api.nvim_set_current_win(state.old_data.win) vim.schedule(function() - local cursor_line = math.max(1,state.old_data.cursor[1]) + local cursor_line = math.max(1, state.old_data.cursor[1]) local cursor_col = math.max(0, state.old_data.cursor[2]) api.nvim_win_set_cursor(state.old_data.win, { cursor_line, cursor_col }) diff --git a/lua/menu/mappings.lua b/lua/menu/mappings.lua index 66cbe1c..e538c7b 100644 --- a/lua/menu/mappings.lua +++ b/lua/menu/mappings.lua @@ -6,7 +6,7 @@ local M = {} M.actions = function(items, buf) local tb = vim.tbl_filter(function(v) - return not v.rtxt_type and v.rtxt and v.cmd + return v.rtxt and v.cmd end, items) for _, v in ipairs(tb) do @@ -22,6 +22,19 @@ M.actions = function(items, buf) map("n", v.rtxt, action, { buffer = buf }) end + + local nested_menus = vim.tbl_filter(function(v) + return v.items + end, items) + + for _, v in ipairs(nested_menus) do + if v.keybind then + map("n", v.keybind, function() + vim.api.nvim_win_set_cursor(0, { vim.fn.index(items, v) + 1, 0 }) + utils.toggle_nested_menu(v.name, v.items) + end, { buffer = buf }) + end + end end M.nav_win = function() diff --git a/lua/menu/state.lua b/lua/menu/state.lua index 64438c2..da353f3 100644 --- a/lua/menu/state.lua +++ b/lua/menu/state.lua @@ -1,6 +1,7 @@ local M = { bufs = {}, bufids = {}, + nested_menu = '' } return M diff --git a/lua/menu/ui.lua b/lua/menu/ui.lua index e9c6d20..7aa7df6 100644 --- a/lua/menu/ui.lua +++ b/lua/menu/ui.lua @@ -27,27 +27,6 @@ local format_title = function(buf, name, rtxt, hl, actions, title) return line end -local function toggle_nested_menu(items) - local right_bufs = utils.adjacent_bufs() - - if #right_bufs > 0 then - require("volt.utils").close { - bufs = right_bufs, - close_func = function(buf) - state.bufs[buf] = nil - - for i, val in ipairs(state.bufids) do - if val == buf then - table.remove(state.bufids, i) - end - end - end, - } - else - require("menu").open(items, { nested = true }) - end -end - return function(buf) local lines = {} local bufv = state.bufs[buf] @@ -60,14 +39,14 @@ return function(buf) local nested_menu = item.items if nested_menu then - item.rtxt = "" + item.rtxt = (item.keybind or "") .. " " end local actions = { hover = { id = hover_id, redraw = "items" }, click = function() if nested_menu then - toggle_nested_menu(nested_menu) + utils.toggle_nested_menu(item.name, nested_menu) return end diff --git a/lua/menu/utils.lua b/lua/menu/utils.lua index 991105b..2bd9229 100644 --- a/lua/menu/utils.lua +++ b/lua/menu/utils.lua @@ -64,4 +64,31 @@ M.delete_old_menus = function() end end +M.toggle_nested_menu = function(name, items) + local right_bufs = M.adjacent_bufs() + + if #right_bufs > 0 then + require("volt.utils").close { + bufs = right_bufs, + close_func = function(buf) + state.bufs[buf] = nil + + for i, val in ipairs(state.bufids) do + if val == buf then + table.remove(state.bufids, i) + end + end + end, + } + + if name ~= state.nested_menu then + require("menu").open(items, { nested = true }) + end + else + require("menu").open(items, { nested = true }) + end + + state.nested_menu = name == state.nested_menu and "" or name +end + return M