Skip to content

Commit

Permalink
Implement action selection menu for :SessionManager (#109)
Browse files Browse the repository at this point in the history
Co-authored-by: Ali Shahid <shaeinst@gmail.com>
  • Loading branch information
shaeinst and Ali Shahid authored Jan 27, 2024
1 parent b8a22f2 commit 07bb625
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ local session_manager = {}
---@param values table
function session_manager.setup(values) setmetatable(config, { __index = vim.tbl_extend('force', config.defaults, values) }) end

-- Displays action selection menu for :SessionManager
function session_manager.available_commands()
local commands = {}
for cmd, _ in pairs(session_manager) do
if cmd ~= 'setup' and cmd ~= 'available_commands' and cmd ~= 'autosave_session' then
table.insert(commands, cmd)
end
end
vim.ui.select(commands, {
prompt = 'Session Manager',
format_item = function(item) return item:sub(1, 1):upper() .. item:sub(2):gsub('_', ' ') end,
}, function(item)
if item then
session_manager[item]()
end
end)
end

--- Selects a session a loads it.
---@param discard_current boolean: If `true`, do not check for unsaved buffers.
function session_manager.load_session(discard_current)
Expand Down
4 changes: 4 additions & 0 deletions lua/session_manager/subcommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ end
--- Run specified subcommand received from completion.
---@param subcommand table
function subcommands.run(subcommand)
if subcommand.args == '' then
session_manager.available_commands()
return
end
local subcommand_func = session_manager[subcommand.fargs[1]]
if not subcommand_func then
utils.notify('No such subcommand: ' .. subcommand.fargs[1], vim.log.levels.ERROR)
Expand Down
2 changes: 1 addition & 1 deletion plugin/session_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ end
local subcommands = require('session_manager.subcommands')
local session_manager = require('session_manager')

vim.api.nvim_create_user_command('SessionManager', subcommands.run, { nargs = 1, bang = true, complete = subcommands.complete, desc = 'Run session manager command' })
vim.api.nvim_create_user_command('SessionManager', subcommands.run, { nargs = '*', bang = true, complete = subcommands.complete, desc = 'Run session manager command' })

local session_manager_group = vim.api.nvim_create_augroup('SessionManager', {})
vim.api.nvim_create_autocmd({ 'VimEnter' }, {
Expand Down

0 comments on commit 07bb625

Please sign in to comment.