olimorris / persisted.nvim

💾 Simple session management for Neovim with git branching, autoloading and Telescope support

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Provide git branch and session directory to callbacks

HumblePresent opened this issue · comments

Currently, it is not possible to obtain the session directory and git branch from the session table passed to the telescope before_source() and after_source() callbacks. I think it may be useful to provide these fields as part of the session table passed to the callbacks. For example I am trying to obtain the git branch of a session selected in the telescope picker in order to automatically check out the branch before sourcing the session file. This would be especially useful in the event that buffers for files that only exist on that branch are restored by the session file.

Also, I am not sure why a default branch name is appended to session files whose directories are not git repositories. I find that this makes it difficult to determine if a session's directory is actually a git repo or not because they all have a branch name even if it's just a default. Wouldn't it make more sense to just not append anything for session directories that are not git repos?

commented

Couple of things regarding this.

Firstly, the plugin is only ever intended to be simple. I kinda broke my own rules by adding Telescope support but enough people were keen and I saw the benefits so included it. I did provisionally add branch and session details which could be picked up by the callbacks but dropped them as I couldn't implement them "simply".

Secondly, when Git Branching was implemented it was done in a pretty basic way. Append the branch name to the filename. That saved me storing state elsewhere. However it also means that it's pretty nasty when it comes to breaking changes or I'll need to come up with something that is no longer "simple" to solve it.

In theory, you have the session name so could determine the branch from that (although it's going to be super unreliable owing to how I implemented git branching).

So in summary, I built enough to make this simple and hoped that for users who want complexity, they can use the callbacks to achieve what they want.

I understand the desire for simplicity given this wouldn't be super simple to implement. Alternatively, would you consider using some known separator between the session directory name and the associated branch in the session file name? I am fine with implementing the branch switching in a callback but the issue is that I cannot reliably separate the branch name from the directory name based on the session file name. Here's an example session file

/home/name/.local/share/nvim/sessions/%home%dir_name_branch_name.vim

There is no way to know at what point within dir_name_branch_name the directory name ends and the branch name begins because the directory name and branch are just separated by an underscore. If they were separated by a unique separator like ++ or ## instead of the underscore then it could be easily parsed within a callback. This would technically be a breaking change but would only require a small code change.

commented

So breaking changes are an absolute no-no for myself unless it's truly critical.

I wouldn't mind a config option that allows people to provide a separator option between session and branch. The default
could be _ but we could allow people to change it to ++ for instance. That way it doesn't break existing config yet would allow your ask above.

Yeah adding a config option is a good compromise. I can work on PR.