folke / persistence.nvim

💾 Simple session management for Neovim
Apache License 2.0
638 stars 26 forks source link

Enable sessions per git branch and set global variable if running #6

Closed olimorris closed 2 years ago

olimorris commented 3 years ago

Firstly, thank you for writing such a beautifully simple Lua based session management plugin. I had previously used Vim-Obsession coupled with Vim-Prosession. Your plugin did nearly everything I needed it to do.

My workflow

Before I explain this pull request I'd like to explain how I work and hence why I made the changes.

I often work across two projects at any one time. However, within those projects are many git branches that I switch between. We implement git-flow rather religiously so I may be viewing the develop branch and then switching to a hotfix or a brand new feature. As such, it's really handy to quickly switch branch on the command line and recover exactly where I was last time from within Neovim.

Lastly, I like to know if Persistence is running or not. So I display a small glyph in my statusline to indicate its status. That way I know whether I need to initiate or stop it.

The proposed changes

Git branching

Enabling of an option called use_git_branch to enable session files to be created for the cwd and the current git branch. The main change has been the addition of a function called get_branch() which determines the current branch. I opted to run a vim command for this as it is cleaner than some of the pure Lua alternatives I saw.

If enabled, then session files will be saved like:

%Users%Oli%Code%Projects%persistence_feature%add-git-branch-to-session.vim

where _feature%add-git-branch-to-session is the branch feature\add-git-branch-to-session.

If use_git_branch is enabled and the cwd is not a git enabled repo, then _HEAD.vim is appended to the session filename. I detect this by looking for a .git folder within the cwd.

Determine if Persistence is running

I added a global variable using_persistence to the start, save, load, and stop commands. If active then true is returned. If Persistence has been stopped then a false is returned. This makes it easy to check for Persistence's status using a vim.g.using_persistence command from within my statusline config.

It allows me to have a really useful helper function in my dotfiles for loading/starting/stopping sessions:

function manage_sessions()
  if vim.g.using_persistence == nil then
    return vim.cmd('lua require("persistence").load()')
  end
  if vim.g.using_persistence then
    return vim.cmd('lua require("persistence").stop()')
  end
  return vim.cmd('lua require("persistence").start()')
end
map('n', '<Leader>s', '<cmd>call v:lua.manage_sessions()<CR>', opts)

Summary

Hopefully some simple changes that don't change the beautiful simplicity of this plugin. I also hope they're useful features for the rest of the community. I feel other session plugins are far too bloated, complicated and still lacking the features I need.

bruhtus commented 2 years ago

Git branching

Enabling of an option called use_git_branch to enable session files to be created for the cwd and the current git branch. The main change has been the addition of a function called get_branch() which determines the current one. I opted to run a vim command for this as it is cleaner than some of the pure lua alternatives I saw.

If enabled, then session files will be saved like:

%Users%Oli%Code%Projects%persistence_feature%add-git-branch-to-session.vim

where _feature%add-git-branch-to-session is the branch feature\add-git-branch-to-session.

If use_git_branch is enabled and the cwd is not a git enabled repo, then _HEAD.vim is appended to the session filename. I detect this by looking for a .git folder within the cwd.

I want to ask a question. Is this change enable us to have different vim session for different git branch?

olimorris commented 2 years ago

I want to ask a question. Is this change enable us to have different vim session for different git branch?

Yes. Exactly that.

olimorris commented 2 years ago

@folke - what are you thoughts on merging this into main?

olimorris commented 2 years ago

When merging the prior updates from the base branch, the git branching functionality got chopped. Horrific miss on my part.

chevsunpower commented 1 year ago

Did this get merged elsewhere or did this never land? Branch-based sessions would be a welcome addition.

olimorris commented 1 year ago

I don't believe so. I ended up forking this repo and making my own session plugin instead.