Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: load last cwd #63

Merged
merged 35 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3720e94
feat: add list_cwd command
josh-nz Jun 11, 2024
b446b65
feat: add cwd session support to telescope
josh-nz Jun 11, 2024
2671dc5
feat: load last session for cwd
josh-nz Jun 12, 2024
af1219f
fix: reduce calls to autosave_info
josh-nz Jun 12, 2024
17afbb9
fix: correct autosave log info
josh-nz Jun 12, 2024
7e697bc
fix: changes as per review
josh-nz Jun 13, 2024
0852a37
feat: add skip autosave opt, fixes startup autosave issues
josh-nz Jun 13, 2024
c7187d1
feat: add ListCwd command function to List command
josh-nz Jun 13, 2024
2d662fe
feat: show message when no session to autoload
josh-nz Jun 13, 2024
984b34a
feat: nicer error when loading session that does not exist
josh-nz Jun 13, 2024
aca4efa
fix: correct get_last typespec
josh-nz Jun 13, 2024
a93852a
fix: get_last caller to handle nil result
josh-nz Jun 13, 2024
d5e6255
fix: improve session action messages
josh-nz Jun 13, 2024
ba6eeed
Revert "feat: add ListCwd command function to List command"
josh-nz Jun 16, 2024
945a0f4
feat: add optional dir argument to PossessionListCwd command
josh-nz Jun 16, 2024
1a67b60
docs: correct PossessionLoad optional name description.
josh-nz Jun 16, 2024
8d373b6
feat: add optional dir argument to PossessionLoadCwd command
josh-nz Jun 17, 2024
97ce76f
docs: fixes spelling, more consistent in style.
josh-nz Jun 17, 2024
d715e76
chore: refactor to use absolute path function
josh-nz Jun 17, 2024
84c5748
feat: more autoload options
josh-nz Jun 17, 2024
f139ce3
feat: backwards compatibility with older autosave config
josh-nz Jun 17, 2024
6f9e0b1
fix: strip trailing slash from dir name
josh-nz Jun 17, 2024
056b1a7
fix: expand dir first otherwise dir check fails
josh-nz Jun 17, 2024
cd92d32
fix: run config compatibility before warning of unknown keys
josh-nz Jun 17, 2024
9b12c22
feat: smarter PossessionLoadCwd completion
josh-nz Jun 17, 2024
d9ab715
chore: refactor distinct list as query function
josh-nz Jun 19, 2024
08c9b7e
feat: add autoload config deprecation warning
josh-nz Jun 19, 2024
f9492cb
fix: use vim.deprecate for old autoload config setting
josh-nz Jun 22, 2024
1406a6a
fix: use improved unique session algorithm, adjust name
josh-nz Jun 22, 2024
57bddd8
feat: skip autoload when arguments are passed to Neovim
josh-nz Jun 23, 2024
50c875f
chore: fix spelling
josh-nz Jun 23, 2024
7db1fe6
feat: skip autoload if data is piped to Neovim
josh-nz Jun 25, 2024
bc2a5d9
feat: PossessionLoadCwd now takes a cwd session name
josh-nz Jun 25, 2024
3658c8a
docs: clarify the session name for PossessionSaveCwd
josh-nz Jun 25, 2024
5cc7ba6
fix: use last cwd session if no name passed to PossessionLoadCwd
josh-nz Jun 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ require('possession').setup {
delete = 'PossessionDelete',
show = 'PossessionShow',
list = 'PossessionList',
list_cwd = 'PossessionListCwd',
migrate = 'PossessionMigrate',
},
hooks = {
Expand Down Expand Up @@ -192,6 +193,9 @@ require('telescope').load_extension('possession')
Then use `:Telescope possession list` or `require('telescope').extensions.possession.list()`
The default action will load selected session.

Alternatively, use `:Telescope possession list cwd=true` or `require('telescope').extensions.possession.list({cwd=true})`
This will limit the displayed sessions to those related to the current working directory.

![telescope](./img/telescope.png)

## Auto-save
Expand Down
10 changes: 8 additions & 2 deletions doc/possession.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ Show given session info.
List available sessions. `:PossessionList!` will not hide the `vimscript`
field that contains commands generated by |:mksession|.


:PossessionListCwd~

List available sessions for the cwd. `:PossessionListCwd!` will not hide
the `vimscript` field that contains commands generated by |:mksession|.

*:PossessionMigrate*
:PossessionMigrate {dir_or_file}~

Expand Down Expand Up @@ -148,8 +154,8 @@ autosave.on_quit~
*possession-autoload*
autoload.cwd~
`boolean | function(): boolean`
Automatically load a session for current working directory (created by
`autosave.cwd`) on VimEnter if such a session exists.
Automatically load the last session for current working directory. This
could be one created by `autosave.cwd` or a manually saved session.

*possession-hooks*
hooks.before_save~
Expand Down
5 changes: 4 additions & 1 deletion lua/possession.lua
josh-nz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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, '', { nargs = 0 }, function(o)
cmd(names.load_cwd, '', { nargs = 0 }, function(_)
commands.load_cwd()
end)
cmd(names.rename, 'old_name? new_name?', { nargs = '*', complete = complete }, function(o)
Expand All @@ -41,6 +41,9 @@ local function setup(opts)
cmd(names.list, '', { nargs = 0, bang = true }, function(o)
commands.list(o.bang)
end)
cmd(names.list_cwd, '', { nargs = 0, bang = true }, function(o)
commands.list_cwd(o.bang)
end)
cmd(names.migrate, 'dir_or_file', { nargs = 1, complete = 'file' }, function(o)
commands.migrate(o.fargs[1])
end)
Expand Down
25 changes: 22 additions & 3 deletions lua/possession/commands.lua
jedrzejboczar marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ local function get_current()
return name
end

local function get_last()
local sessions = query.as_list()
---@param only_cwd? boolean only consider sessions for the cwd
---@param silent? boolean surpress an error if there is no last session
local function get_last(only_cwd, silent)
jedrzejboczar marked this conversation as resolved.
Show resolved Hide resolved
only_cwd = only_cwd or false
silent = silent or false
local sessions = query.as_list(nil, only_cwd)
query.sort_by(sessions, 'mtime', true)
local last_session = sessions and sessions[1]
if not last_session then
utils.error('Cannot find last loaded session - specify session name as an argument')
if not silent then
utils.error('Cannot find last loaded session - specify session name as an argument')
end
return nil
end
return last_session.name
Expand Down Expand Up @@ -112,6 +118,14 @@ function M.load_cwd()
session.load(paths.cwd_session_name())
end

function M.load_last(only_cwd)
local last = get_last(only_cwd, true)
if last then
session.load(last)
return last
end
end

local function maybe_input(value, opts, callback)
if value then
callback(value)
Expand Down Expand Up @@ -174,6 +188,11 @@ function M.list(full)
display.echo_sessions { vimscript = full }
end

---@param full? boolean
function M.list_cwd(full)
display.echo_sessions { vimscript = full, only_cwd = true }
jedrzejboczar marked this conversation as resolved.
Show resolved Hide resolved
end

---@param path string
function M.migrate(path)
if vim.fn.getftype(path) == 'file' then
Expand Down
1 change: 1 addition & 0 deletions lua/possession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ local function defaults()
delete = 'PossessionDelete',
show = 'PossessionShow',
list = 'PossessionList',
list_cwd = 'PossessionListCwd',
migrate = 'PossessionMigrate',
},
hooks = {
Expand Down
4 changes: 3 additions & 1 deletion lua/possession/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ end
---@field buffers? boolean include buffers from parsed vimscript
---@field buffers_short? boolean show buffer paths normalized and relative to session cwd
---@field tab_cwd? boolean include tab cwds from parsed vimscript
---@field only_cwd? boolean only show sessions for the cwd
jedrzejboczar marked this conversation as resolved.
Show resolved Hide resolved

--- Print a list of sessions as Vim message
---@param opts? possession.EchoSessionsOpts
Expand All @@ -107,9 +108,10 @@ function M.echo_sessions(opts)
sessions = nil,
vimscript = false,
user_data = true,
only_cwd = false,
}, opts or {})

local sessions = opts.sessions or query.as_list()
local sessions = opts.sessions or query.as_list(nil, opts.only_cwd)

local info = {}
if opts.buffers or opts.tab_cwd then
Expand Down
6 changes: 4 additions & 2 deletions lua/possession/query.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ local config = require('possession.config')

--- Get a sessions as a list-like table
---@param sessions? table<string, table> like from possession.session.list()
---@param only_cwd? boolean only load sessions for the cwd
---@return table[] list of session data with additional `file` key
function M.as_list(sessions)
sessions = sessions or session.list() --[[@as table<string, table> ]]
function M.as_list(sessions, only_cwd)
jedrzejboczar marked this conversation as resolved.
Show resolved Hide resolved
only_cwd = only_cwd or false
sessions = sessions or session.list { cwd = only_cwd }
local list = {}
for file, data in pairs(sessions) do
if data.file then
Expand Down
28 changes: 18 additions & 10 deletions lua/possession/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ function M.autosave_info()
end
end

function M.autosave()
local info = M.autosave_info()
function M.autosave(autosave_info)
local info = autosave_info or M.autosave_info()
if info then
utils.debug('Auto-saving %s session "%s"', info.variant, state.session_name)
utils.debug('Auto-saving %s session "%s"', info.variant, info.name)
M.save(info.name, { no_confirm = true })
end
end
Expand Down Expand Up @@ -236,8 +236,8 @@ function M.load(name_or_data)

-- Autosave if not loading the auto-saved session itself
local autosave_info = M.autosave_info()
if config.autosave.on_load and (autosave_info and session_data.name ~= M.autosave_info().name) then
M.autosave()
if config.autosave.on_load and (autosave_info and session_data.name ~= autosave_info.name) then
M.autosave(autosave_info)
end

-- Run pre-load hook that can pre-process user data, abort if returns falsy value.
Expand Down Expand Up @@ -362,10 +362,15 @@ function M.exists(name)
end

--- Get a list of sessions as map-like table
---@param opts? {}
---@class possession.ListOpts
---@field only_cwd? boolean only load sessions for cwd

---@param opts? possession.ListOpts
---@return table<string, table> sessions {filename: session_data}
function M.list(opts)
josh-nz marked this conversation as resolved.
Show resolved Hide resolved
opts = opts or {}
opts = vim.tbl_extend('force', {
only_cwd = false,
}, opts or {})
if opts.no_read then
vim.deprecate('session.list().no_read', 'session files are now always read', '?', 'possession')
end
Expand All @@ -374,14 +379,17 @@ function M.list(opts)
local files_by_name = {}

local sessions = {}
local cwd = vim.fn.getcwd(-1, -1)
josh-nz marked this conversation as resolved.
Show resolved Hide resolved
local glob = (Path:new(config.session_dir) / '*'):absolute()
for _, file in ipairs(vim.fn.glob(glob, true, true)) do
local path = Path:new(file)
local data = vim.json.decode(path:read())
sessions[file] = data
if not opts.only_cwd or data.cwd == cwd then
sessions[file] = data

files_by_name[data.name] = files_by_name[data.name] or {}
table.insert(files_by_name[data.name], file)
files_by_name[data.name] = files_by_name[data.name] or {}
table.insert(files_by_name[data.name], file)
end
end

-- Check for name duplicates
Expand Down
4 changes: 3 additions & 1 deletion lua/possession/telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ local session_actions = {
---@field default_action? 'load'|'save'|'delete'
---@field sessions? table[] list of sessions like returned by query.as_list
---@field sort? boolean|possession.QuerySortKey sort the initial sessions list, `true` means 'mtime'
---@field only_cwd? boolean only display sessions for the cwd

---@param opts possession.TelescopeListOpts
function M.list(opts)
opts = vim.tbl_extend('force', {
default_action = 'load',
sessions = nil,
sort = 'mtime',
only_cwd = false,
}, opts or {})

assert(
Expand All @@ -79,7 +81,7 @@ function M.list(opts)
)

local get_finder = function()
local sessions = opts.sessions and vim.list_slice(opts.sessions) or query.as_list()
local sessions = opts.sessions and vim.list_slice(opts.sessions) or query.as_list(nil, opts.only_cwd)
if opts.sort then
local key = opts.sort == true and 'name' or opts.sort
local descending = key ~= 'name'
Expand Down
9 changes: 4 additions & 5 deletions plugin/possession.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ vim.api.nvim_create_autocmd('VimEnter', {

local utils = require('possession.utils')
if utils.as_function(config.autoload.cwd)() then
local paths = require('possession.paths')
local cwd = paths.cwd_session_name()
if paths.session(cwd):exists() then
utils.debug('Auto-loading CWD session: %s', cwd)
require('possession.session').load(cwd)
local cmd = require('possession.commands')
local session = cmd.load_last(true)
if session then
utils.debug('Auto-loading CWD session: %s', session)
end
end
end,
Expand Down
Loading