Skip to content

Overhauled vim cmds and percent encoding session file names #334

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

Merged
merged 71 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
be3a52e
feat(cmds)!: Add Session{Enable/Disable/Toggle}AutoSave
cameronr Jul 18, 2024
8faf284
wip(cmds): update vim cmds and refactor underlying code
cameronr Jul 18, 2024
bfd1d21
test: remove unneeded ':' in vim.cmd calls
cameronr Jul 18, 2024
48128c7
test: last session
cameronr Jul 18, 2024
349a5a2
refactor!: rewrote saving/loading/deleting code to be more consistent
cameronr Jul 19, 2024
c9cdcbe
chore(docs): auto-generate vimdoc
cameronr Jul 19, 2024
53944a5
chore: annotate private methods that shouldn't be in the docs
cameronr Jul 19, 2024
efd06f1
chore(docs): auto-generate vimdoc
cameronr Jul 19, 2024
0556e10
refactor: move complete function to lib so it's still testable
cameronr Jul 19, 2024
83b065d
refactor: rename to DisableAutoSave
cameronr Jul 19, 2024
d54b6fd
chore(docs): auto-generate vimdoc
cameronr Jul 19, 2024
1d6b31e
test: some win32 hacks for now
Jul 19, 2024
1886b06
test: tests for session url encoding
cameronr Jul 20, 2024
a40f446
docs: update usage of vim.loop
cameronr Jul 20, 2024
7a52338
refactor!: url encode sessions to improve support especially on windows
cameronr Jul 20, 2024
f937677
chore(docs): auto-generate vimdoc
cameronr Jul 20, 2024
0e4b7bc
fix: current_session_name to be consistent with session names
cameronr Jul 20, 2024
d34bd7b
docs: going back to loop for compat with older versions
cameronr Jul 20, 2024
e564dd4
fix: git display names and Neovim 0.7.2 compat
cameronr Jul 20, 2024
0ae5468
fix: pop a message on auto save toggle
cameronr Jul 20, 2024
29abb2f
docs: move cwd section down, add lazy = false to default config
cameronr Jul 20, 2024
8a9c6ad
fix: using logger.info instead of vim.notify for messages
cameronr Jul 20, 2024
0376ff7
chore(docs): auto-generate vimdoc
cameronr Jul 20, 2024
97b1833
fix: move last session handling to vim enter fn, add back msgs popups
cameronr Jul 20, 2024
0fe49b5
chore(docs): auto-generate vimdoc
cameronr Jul 20, 2024
d528e3b
docs: moving installation block first, troubleshooting to bottom
cameronr Jul 20, 2024
973cfd4
docs: remove debug log level
cameronr Jul 21, 2024
fecdaa0
docs: whitespace
cameronr Jul 21, 2024
6e0a60c
test: expanding session dir
cameronr Jul 21, 2024
a8c9ef6
fix: expand root and session control dirs
cameronr Jul 21, 2024
ddb5c60
fix: #272 glob entries not working on windows
Jul 21, 2024
719c095
test: unit tests for #272
Jul 21, 2024
ac53837
fix: shellslash support in legacy files
Jul 21, 2024
3393225
test: windows sessions can now have dashes
Jul 21, 2024
ae08d94
test: windows test tweaks
Jul 21, 2024
e7a3c6e
chore: remove unused bulk session conversion function
cameronr Jul 21, 2024
ba32f1d
test: legacy session delete
cameronr Jul 21, 2024
bfcc1e0
chore: re-enble test windows can now pass
Jul 21, 2024
5c53974
chore: remove logging
cameronr Jul 21, 2024
74fd2aa
feat(healthcheck): fix #204 warn if localoptions missing from session…
cameronr Jul 21, 2024
8ded967
test(lib): current_session_name() when no session
cameronr Jul 21, 2024
c5733d8
feat(checkhealth) include sesssion dir, session name, session file
cameronr Jul 22, 2024
36c5e43
refactor(lib): percent en/decode is more accurate
cameronr Jul 22, 2024
0fb8828
test(git): add restore tests
cameronr Jul 22, 2024
8336407
fix: also check for vim.g.SessionLoad in cwd handling
cameronr Jul 22, 2024
5e1c1e8
fix: wipeout buffers instead of just deleting them
cameronr Jul 22, 2024
f31d666
chore: remove unused code
cameronr Jul 22, 2024
917588b
docs: update nvim tree hook example
cameronr Jul 23, 2024
4efa10e
fix: delete sessions by file
cameronr Jul 23, 2024
fe09fec
chore(docs): auto-generate vimdoc
cameronr Jul 23, 2024
905bfe8
chore: remove unusued path.lua file
cameronr Jul 24, 2024
4e97a19
test: deleteing the current session should clear vim.v.this_session
cameronr Jul 24, 2024
08f5356
fix: #250 clear vim.v.this_session if current session is deleted
cameronr Jul 24, 2024
5454660
fix: convert extra cmds in SessionRestore, delete them SessionDelete
cameronr Jul 25, 2024
ec955f5
fix: set auto_save_enabled, auto_restore_enabled to true instead of nil
cameronr Jul 26, 2024
fb10b9f
fix: #262 remove old comment
cameronr Jul 26, 2024
9e0c8c3
refactor: hide SessionSaveToDir/SessionRestoreFrom/SessionDeleteFromDir
cameronr Jul 26, 2024
aa44caf
docs: update nvim version
cameronr Jul 26, 2024
1085688
fix: custom dir tests to use functions rather than vim cmds
cameronr Jul 26, 2024
2b236ac
fix: re-order config options based on importance
cameronr Jul 27, 2024
5c7074d
chore(docs): auto-generate vimdoc
cameronr Jul 27, 2024
a976657
Merge branch 'main' into percent-encode
cameronr Jul 27, 2024
38acc9e
chore: mark function as private
cameronr Jul 27, 2024
225d4be
chore(docs): auto-generate vimdoc
cameronr Jul 27, 2024
c68887a
fix: include % in characters encoded
cameronr Jul 27, 2024
b0f43cb
fix: escape . in session names
cameronr Jul 27, 2024
95c735b
fix: don't mark config as defaultConf so it won't create diagnostics
cameronr Jul 29, 2024
0a9d104
chore(docs): auto-generate vimdoc
cameronr Jul 29, 2024
cb4945c
fix(args): don't save a session if no session for single dir arg
cameronr Jul 29, 2024
0e46cf4
fix: delete args before saving a session
cameronr Jul 29, 2024
d44b2fb
fix(lib): add convenience param to get last part of session name
cameronr Jul 29, 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
268 changes: 136 additions & 132 deletions README.md

Large diffs are not rendered by default.

211 changes: 89 additions & 122 deletions doc/auto-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ defaultConf *defaultConf*
table default config for auto session

Fields: ~
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
{auto_session_enable_last_session?} (boolean)
{auto_session_root_dir?} (string) root directory for session files, by default is `vim.fn.stdpath('data')/sessions/`
{auto_session_enabled?} (boolean) enable auto session
{auto_session_create_enabled} (boolean|function|nil) Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
{auto_save_enabled?} (boolean) Enables/disables auto saving session
{auto_restore_enabled?} (boolean) Enables/disables auto restoring session
{auto_restore_lazy_delay_enabled?} (boolean) Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
{auto_session_suppress_dirs?} (table) Suppress auto session for directories
{auto_session_allowed_dirs?} (table) Allow auto session for directories, if empty then all directories are allowed except for suppressed ones
{auto_session_use_git_branch?} (boolean) Include git branch name in session name to differentiate between sessions for different git branches
{auto_session_enabled?} (boolean) Enables/disables auto saving and restoring
{auto_session_root_dir?} (string) root directory for session files, by default is `vim.fn.stdpath('data') .. '/sessions/'`
{auto_save_enabled?} (boolean) Enables/disables auto saving session on exit
{auto_restore_enabled?} (boolean) Enables/disables auto restoring session on start
{auto_session_suppress_dirs?} (table) Suppress auto session for directories
{auto_session_allowed_dirs?} (table) Allow auto session for directories, if empty then all directories are allowed except for suppressed ones
{auto_session_create_enabled} (boolean|function) Enables/disables auto creating new sessions. Can take a function that should return true/false if a session should be created or not
{auto_session_enable_last_session?} (boolean) On startup, loads the last saved session if session for cwd does not exist
{auto_session_use_git_branch?} (boolean) Include git branch name in session name to differentiate between sessions for different git branches
{auto_restore_lazy_delay_enabled?} (boolean) Automatically detect if Lazy.nvim is being used and wait until Lazy is done to make sure session is restored correctly. Does nothing if Lazy isn't being used. Can be disabled if a problem is suspected or for debugging
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR


luaOnlyConf *luaOnlyConf*
Expand Down Expand Up @@ -46,155 +46,144 @@ session_control *session_control*


AutoSession.setup({config}) *AutoSession.setup*
Setup function for AutoSession
Setup function for AutoSession. Config is not marked as defaultConf so it doesn't
create a diagnostic about missing fields.

Parameters: ~
{config} (defaultConf) config for auto session
{config} (table|nil) Config for auto session


AutoSession.setup_session_lens() *AutoSession.setup_session_lens*


AutoSession.get_latest_session() *AutoSession.get_latest_session*
Get latest session for the "last session" feature
AutoSession.session_exists_for_cwd() *AutoSession.session_exists_for_cwd*
Quickly checks if a session file exists for the current working directory.
This is useful for starter plugins which don't want to display 'restore session'
unless a session for the current working directory exists.

Returns: ~
(string|nil)
(boolean) if a session exists for the cwd


AutoSession.session_exists_for_cwd() *AutoSession.session_exists_for_cwd*


*AutoSession.AutoSaveSession*
AutoSession.AutoSaveSession({sessions_dir?})
AutoSession.AutoSaveSession() *AutoSession.AutoSaveSession*
AutoSaveSession
Function called by auto_session to trigger auto_saving sessions, for example on VimExit events.

Parameters: ~
{sessions_dir?} (string) the session directory to auto_save a session for. If empty this function will end up using the cwd to infer what session to save for.


*AutoSession.get_root_dir*
AutoSession.get_root_dir({with_trailing_separator?})
Gets the root directory of where to save the sessions.
By default this resolves to `vim.fn.stdpath "data" .. "/sessions/"`

Parameters: ~
{with_trailing_separator?} (boolean) whether to incude the trailing separator. A few places (telescope picker don't expect a trailing separator)

Returns: ~
(string)
(boolean) if a session was saved


AutoSession.get_cmds({typ}) *AutoSession.get_cmds*
Get the hook commands to run
This function gets cmds from both lua and vimscript configs
*AutoSession.AutoRestoreSession*
AutoSession.AutoRestoreSession({session_name?})
Function called by AutoSession when automatically restoring a session.

Parameters: ~
{typ} (string)
{session_name?} (string) An optional session to load

Returns: ~
(function[]|string[])

(boolean) returns whether restoring the session was successful or not.

PickerItem *PickerItem*

Fields: ~
{display_name} (string)
{path} (string)
AutoSession.PurgeOrphanedSessions() *AutoSession.PurgeOrphanedSessions*
Deletes sessions where the original directory no longer exists


AutoSession.format_file_name({path}) *AutoSession.format_file_name*
Formats an autosession file name to be more presentable to a user
*AutoSession.SaveSession*
AutoSession.SaveSession({session_name?}, {show_message?})
Saves a session to the dir specified in the config. If no optional
session name is passed in, it uses the cwd as the session name

Parameters: ~
{path} (string)

Returns: ~
(string)


AutoSession.get_session_files() *AutoSession.get_session_files*
{session_name?} (string|nil) Optional session name
{show_message?} (boolean) Optional, whether to show a message on save (true by default)

Returns: ~
(PickerItem[])
(boolean)


*AutoSession.SaveSession*
AutoSession.SaveSession({sessions_dir?}, {auto})
*AutoSession.SaveSessionToDir*
AutoSession.SaveSessionToDir({session_dir}, {session_name?}, {show_message?})
Saves a session to the passed in directory. If no optional
session name is passed in, it uses the cwd as the session name

Parameters: ~
{sessions_dir?} (string)
{auto} (boolean)
{session_dir} (string) Directory to write the session file to
{session_name?} (string|nil) Optional session name
{show_message?} (boolean) Optional, whether to show a message on save (true by default)

Returns: ~
(boolean)

*AutoSession.AutoRestoreSession*
AutoSession.AutoRestoreSession({session_dir})
Function called by AutoSession when automatically restoring a session.

Parameters: ~
{session_dir} (any)
*AutoSession.RestoreSession*
AutoSession.RestoreSession({session_name?}, {show_message?})
Restores a session from the passed in directory. If no optional session name
is passed in, it uses the cwd as the session name

Returns: ~
(boolean) returns whether restoring the session was successful or not.
Parameters: ~
{session_name?} (string|nil) Optional session name
{show_message?} (boolean) Optional, whether to show a message on restore (true by default)


*AutoSession.RestoreSessionFromFile*
AutoSession.RestoreSessionFromFile({session_file})
*AutoSession.RestoreSessionFromDir*
AutoSession.RestoreSessionFromDir({session_dir}, {session_name?}, {show_message?})
Restores a session from the passed in directory. If no optional session name
is passed in, it uses the cwd as the session name

Parameters: ~
{session_file} (string)
{session_dir} (string) Directory to write the session file to
{session_name?} (string|nil) Optional session name
{show_message?} (boolean) Optional, whether to show a message on restore (true by default)


*AutoSession.RestoreSession*
AutoSession.RestoreSession({sessions_dir_or_file})
Restores the session by sourcing the session file if it exists/is readable.
This function is intended to be called by the user but it is also called by `AutoRestoreSession`
*AutoSession.RestoreSessionFile*
AutoSession.RestoreSessionFile({session_path}, {show_message?})
Restores a session from a specific file

Parameters: ~
{sessions_dir_or_file} (string) a dir string or a file string
{session_path} (string) The session file to load
{show_message?} (boolean) Optional, whether to show a message on restore (true by default)

Returns: ~
(boolean) returns whether restoring the session was successful or not.

(boolean) a session restored

AutoSession.DisableAutoSave() *AutoSession.DisableAutoSave*
DisableAutoSave
Intended to be called by the user

*AutoSession.DeleteSession*
AutoSession.DeleteSession({session_name?})
Deletes a session from the config session dir. If no optional session name
is passed in, it uses the cwd as the session name

AutoSession.CompleteSessions() *AutoSession.CompleteSessions*
CompleteSessions is used by the vimscript command for session name/path completion.

Returns: ~
(string)
Parameters: ~
{session_name?} (string|nil) Optional session name


AutoSession.DeleteSessionByName({...}) *AutoSession.DeleteSessionByName*
DeleteSessionByName deletes sessions given a provided list of paths
*AutoSession.DeleteSessionFromDir*
AutoSession.DeleteSessionFromDir({session_dir}, {session_name?})
Deletes a session from the passed in directory. If no optional session
name is passed in, it uses the cwd as the session name

Parameters: ~
{...} (string[])
{session_dir} (string) Directory to delete the session from
{session_name?} (string|nil) Optional session name


AutoSession.DeleteSession({...}) *AutoSession.DeleteSession*
DeleteSession delets a single session given a provided path
*AutoSession.DeleteSessionFile*
AutoSession.DeleteSessionFile({session_path}, {session_name})
Delete a session file

Parameters: ~
{...} (string[])

{session_path} (string) The filename to delete
{session_name} (string) Session name being deleted, just use to display messages

AutoSession.PurgeOrphanedSessions() *AutoSession.PurgeOrphanedSessions*
PurgeOrphanedSessions deletes sessions with no working directory exist
Returns: ~
(boolean) the session file delted


*M.setup_autocmds*
M.setup_autocmds({config}, {AutoSession})
Setup autocmds for DirChangedPre and DirChanged
AutoSession.DisableAutoSave({enable?}) *AutoSession.DisableAutoSave*
Disables autosave. Enables autosave if enable is true

Parameters: ~
{config} (table) auto session config
{AutoSession} (table) auto session instance
{enable?} (boolean) Optional paramter to enable autosaving

Returns: ~
(boolean) autosaving is enabled or not


session_lens_config *session_lens_config*
Expand Down Expand Up @@ -222,26 +211,4 @@ SessionLens.search_session({custom_opts})
{custom_opts} (any)


M.setup() *M.setup*


M.source_session({prompt_bufnr}) *M.source_session*
Source session action
Source a selected session after doing proper current session saving and cleanup

Parameters: ~
{prompt_bufnr} (number) the telescope prompt bufnr


M.delete_session({prompt_bufnr}) *M.delete_session*
Delete session action
Delete a selected session file

Parameters: ~
{prompt_bufnr} (number) the telescope prompt bufnr


M.alternate_session() *M.alternate_session*


vim:tw=78:ts=8:noet:ft=help:norl:
21 changes: 6 additions & 15 deletions lua/auto-session/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local Lib = require "auto-session.lib"

local M = {}

---@private
---Setup autocmds for DirChangedPre and DirChanged
---@param config table auto session config
---@param AutoSession table auto session instance
Expand Down Expand Up @@ -30,8 +31,8 @@ M.setup_autocmds = function(config, AutoSession)
return
end

if AutoSession.restore_in_progress then
Lib.logger.debug "DirChangedPre: restore_in_progress is true, ignoring this event"
if AutoSession.restore_in_progress or vim.g.SessionLoad then
Lib.logger.debug "DirChangedPre: restore_in_progress/vim.g.SessionLoad is true, ignoring this event"
-- NOTE: We don't call the cwd_changed_hook here
-- I think that's probably the right choice because I assume that event is mostly
-- for preparing sessions for save/restoring but we don't want to do that when we're
Expand All @@ -41,16 +42,6 @@ M.setup_autocmds = function(config, AutoSession)

AutoSession.AutoSaveSession()

-- Clear all buffers and jumps after session save so session doesn't blead over to next session.

-- BUG: I think this is probably better done in RestoreSession. If we do it here and the
-- directory change fails (e.g. it doesn't exist), we'll have cleared the buffers and still be
-- in the same directory. If autosaving is enabled, we'll save an empty session when we exit
-- blowing away the session we were trying to save
vim.cmd "%bd!"

vim.cmd "clearjumps"

if type(conf.pre_cwd_changed_hook) == "function" then
conf.pre_cwd_changed_hook()
end
Expand All @@ -71,12 +62,12 @@ M.setup_autocmds = function(config, AutoSession)
return
end

if AutoSession.restore_in_progress then
if AutoSession.restore_in_progress or vim.g.SessionLoad then
-- NOTE: We don't call the cwd_changed_hook here (or in the other case below)
-- I think that's probably the right choice because I assume that event is mostly
-- for preparing sessions for save/restoring but we don't want to do that when we're
-- already restoring a session
Lib.logger.debug "DirChanged: restore_in_progress is true, ignoring this event"
Lib.logger.debug "DirChangedPre: restore_in_progress/vim.g.SessionLoad is true, ignoring this event"
return
end

Expand All @@ -89,7 +80,7 @@ M.setup_autocmds = function(config, AutoSession)
local success = AutoSession.AutoRestoreSession()

if not success then
Lib.logger.info("Could not load session. A session file is likely missing for this cwd." .. vim.fn.getcwd())
Lib.logger.info("Could not load session for: " .. vim.fn.getcwd())
-- Don't return, still dispatch the hook below
end

Expand Down
37 changes: 37 additions & 0 deletions lua/auto-session/health.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local AutoSession = require "auto-session"

local M = {}

---@diagnostic disable-next-line: deprecated
local start = vim.health.start or vim.health.report_start
---@diagnostic disable-next-line: deprecated
local ok = vim.health.ok or vim.health.report_ok
---@diagnostic disable-next-line: deprecated
local warn = vim.health.warn or vim.health.report_warn
---@diagnostic disable-next-line: deprecated, unused-local
local error = vim.health.error or vim.health.report_error
---@diagnostic disable-next-line: deprecated
local info = vim.health.info or vim.health.report_info

local function check_sesssion_options()
if not vim.tbl_contains(vim.split(vim.o.sessionoptions, ","), "localoptions") then
warn(
"`vim.o.sessionoptions` should contain 'localoptions' to make sure\nfiletype and highlighting work correctly after a session is restored.\n\n"
.. "Recommended setting is:\n\n"
.. 'vim.o.sessionoptions="blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions"\n'
)
else
ok "vim.o.sessionoptions"
end
end

function M.check()
start "auto-session"

info("Session directory: " .. AutoSession.get_root_dir())
info("Current session: " .. AutoSession.Lib.current_session_name())
info("Current sesssion file: " .. vim.v.this_session)
check_sesssion_options()
end

return M
Loading