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

Overhauled vim cmds and percent encoding session file names #334

Merged
merged 71 commits into from
Jul 30, 2024

Conversation

cameronr
Copy link
Collaborator

@cameronr cameronr commented Jul 27, 2024

While this PR is largely compatible with existing setups, it does include some breaking changes (as discussed in #257) with how vim commands work with arguments. It also includes a new session filename format (percent encoding some characters) but it transparently handles converting between the old format (e.g. %some%dir on *nix/MacOS and C++-some-dir on Windows) and the new percent encoding (%2Fsome%2Fdir format on both on all platforms)

Command Overhaul

How the commands work with arguments has been updated. The argument to any function (Save/Restore/Delete) is strictly a session name, which means either a named session (e.g mysession) or a path (e.g. /my/proj). It is no longer possible to specify a directory to save in and a session name all in one argument. That was necessary because it was impossible to make that consistent in all cases. The benefit is that arguments to the commands and what you see in the session picker are all consistent. See "/my/proj" as the session name in the session picker? Great, you can restore it with :SessionRestore /my/proj

:SessionSave " saves a session based on the `cwd` in `auto_session_root_dir`
:SessionSave my_session " saves a session called `my_session` in `auto_session_root_dir`

:SessionRestore " restores a session based on the `cwd` from `auto_session_root_dir`
:SessionRestore my_session " restores `my_session` from `auto_session_root_dir`

:SessionDelete " deletes a session based on the `cwd` from `auto_session_root_dir`
:SessionDelete my_session " deletes `my_sesion` from `auto_session_root_dir`

I also added a :SessionSearch command that will pop the Telescope picker if it's installed and vim.ui.select otherwise. DisableAutoSave was renamed to SessionDisableAutoSave to make it consistent with the rest of the commands and I added a toggle cmd useful for a keymap:

:SessionSearch " open a session picker, uses Telescope if installed, vim.ui.select otherwise

:SesssionDisableAutoSave " disables autosave
:SesssionDisableAutoSave! " enables autosave (still does all checks in the config)
:SesssionToggleAutoSave " toggles autosave

In addition, RestoreSessionFromFile has been removed as a vim command because it's no longer necessary.

I decided not to expose SessionSaveToDir/SessionRestoreFromDir/SessionDeleteFromDir versions as vim cmds as they seem like more niche uses cases and they clutter the autocomplete list. That functionality is available in Lua, tho, as SaveSessionToDir, RestoreSessionFromDir, DeleteSessionFromDir.

As a little bonus, :SessionRestore/:SessionDelete also now get session name completion when you're typing.

New session filename format

Session file names are now percent encoded rather than the custom and different escaping methods used between *nix/MacOS and Windows. The new format is the same across all platforms and will percent encode all /\:*?"'<>+ |.% characters. The immediate benefit is supporting session names with dashes on windows and some slightly better git branch naming. This should mean fewer encoding related issues going forward.

When restoring a session, it will transparently move an old format session file to the new format and all places the search/delete sessions are able to read both formats.

current_session_name

The return from require('auto-session.lib').current_session_name is now the full session name, consistent with what is displayed in session search and used for save/restore. That means if it's a session that was automatically named after the cwd, it will be longer than was previously returned. To get just the last part of the session name (like the previous behavior), just pass true as an argument. For example, for the session named /some/dir/myproj, then require('auto-session.lib').current_session_name(true) will return just myproj

Fixes

Fixes: #55, #73, #148, #195, #204, #242, #250, #257, #262

#116: Addresses 1-4 but not 5 or 6.

#273: Doesn't fully address, especially the way we're handling git branches, but the simplicity of just having a session file without requiring a mapping table is desirable enough that I think it's worth seeing if this is good enough. If not, we can add a mapping layer on top.

Closing Thoughts

Even with the unit test coverage, it could make sense to integrate this into a another branch and get a few other people testing. Or we can just integrate it into main and I'll make sure to respond quickly to any reported issues.

Lastly, assuming that this PR is approved, I'll update the breaking changes issue with the text above.

cameronr and others added 30 commits July 17, 2024 23:22
BREAKING CHANGE: Removed DisableAutoSave as it didn't follow the naming
convention
All commands and functions now expect a session name, which is either
just a string (e.g. `:SessionSave mysession`) or a path (usually the cwd,
`:SessionSave` with no argument is the same as `:SessionSave ..
vim.fn.getcwd()`). This should not impact most people but you can now no
longer do something like `SessionSave /some/dir/session.vim` and expect
it to save to `/some/dir`. What will happen now is that it will save a
session with the name '/some/dir/session'.

Also introducing some new cmds:

- `:SessionSearch` (pops a picker, eitherTelescope or vim.ui.select)
- `:SesssionEnableAutoSave` enables autosave on exit, subject to all of the normal filters in the config
- `:SesssionEnableAutoSave!` disable autosave on exit
- `:SesssionToggleAutoSave` toggles autosave on exit

Also fixed autocomplete for session names for Restore/Delete
BREAKING CHANGE: This is listed as a break change but there is code to
handle working with the old session file names. In theory, it should be
seamless but it's worth highlighting what's changed in case you run into
any trouble.
Only check for last session at `auto_restore_session_at_vim_enter` to
make sure it's only ever run once as cwd handling calls AutoRestore
cameronr and others added 21 commits July 22, 2024 09:18
That way a you can delete legacy files directly vs it deleting the
non-legacy session first. We're still restoring sessions by name rather
than file, mostly to handle dynamic conversion
Shouldn't change the code at all, just cleaner to not have to deal with
nil
I think it's cleaner not to expose them as vim cmds as they clutter
the cmd space for what is a pretty niche use case. That functionality
is available in Lua, tho, as SaveSessionToDir, RestoreSessionFromDir,
DeleteSessionFromDir for anyone that might want to use it.
This enables handling session names that end in .vim (as you might find
in a vim plugin name)
@cameronr cameronr changed the title Overhauled vim cmds and percent encoding sessions Overhauled vim cmds and percent encoding session file names Jul 28, 2024
cameronr and others added 5 commits July 28, 2024 20:27
If it's marked as defaultConf, then it will create a diagnostic for
missing fields and we don't want that.
If launched with a single directory argument but there's no session for
that directory, don't save a session in the cwd for that directory
(unless it the single directory argument matches the cwd)
If we don't delete the args when saving the session, the session will
keep adding those files back in when the session is reloaded, even if
those buffers had been closed
@rmagatti rmagatti merged commit 5dd9600 into rmagatti:main Jul 30, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write tests
2 participants