Skip to content

Commit

Permalink
feat: smarter PossessionLoadCwd completion
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-nz committed Jun 17, 2024
1 parent cd92d32 commit 9b12c22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lua/possession.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local function setup(opts)
local names = config.commands
local commands = require('possession.commands')
local complete = commands.complete_session
local cwd_complete = commands.cwd_complete_session

cmd(names.save, 'name?', { nargs = '?', complete = complete, bang = true }, function(o)
commands.save(o.fargs[1], o.bang)
Expand All @@ -23,7 +24,7 @@ local function setup(opts)
cmd(names.save_cwd, '', { nargs = 0, bang = true }, function(o)
commands.save_cwd(o.bang)
end)
cmd(names.load_cwd, 'dir?', { nargs = '?', complete = 'dir' }, function(o)
cmd(names.load_cwd, 'dir?', { nargs = '?', complete = cwd_complete }, function(o)
commands.load_cwd(o.fargs[1])
end)
cmd(names.rename, 'old_name? new_name?', { nargs = '*', complete = complete }, function(o)
Expand Down
28 changes: 24 additions & 4 deletions lua/possession/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ local function complete_list(candidates, opts)
end
end

-- Limits filesystem access by caching the session names per command line access
---@type table<string, string>?
-- Limits filesystem access by caching the session data per command line access
---@type table<string, { name: string, cwd: string }>?
local cached_names
vim.api.nvim_create_autocmd('CmdlineLeave', {
group = vim.api.nvim_create_augroup('possession.commands.complete', { clear = true }),
Expand All @@ -49,13 +49,33 @@ local function get_session_names()
if not cached_names then
cached_names = {}
for file, data in pairs(session.list()) do
cached_names[file] = data.name
cached_names[file] = { name = data.name, cwd = data.cwd }
end
end
return cached_names
end

M.complete_session = complete_list(get_session_names)
M.complete_session = complete_list(function()
return vim.tbl_map(function(s)
return s.name
end, get_session_names())
end)

M.cwd_complete_session = complete_list(function()
local cwds = vim.tbl_map(function(s)
return s.cwd
end, get_session_names())

local distinct_cwds = {}

for _, v in pairs(cwds) do
if not vim.tbl_contains(distinct_cwds, v) then
table.insert(distinct_cwds, v)
end
end

return distinct_cwds
end)

local function get_current()
local name = session.get_session_name()
Expand Down

0 comments on commit 9b12c22

Please sign in to comment.