Add ability to session based on cwd and git branch
This creates session files like: %Users%Oli%Code%Projects%persistance_feature%add-git-branch-to-session enabling you to segment your project directory by git branchmain
parent
2f2b0cc69d
commit
3d84264be5
|
|
@ -3,6 +3,7 @@ local M = {}
|
||||||
---@class PersistenceOptions
|
---@class PersistenceOptions
|
||||||
local defaults = {
|
local defaults = {
|
||||||
dir = vim.fn.expand(vim.fn.stdpath("config") .. "/sessions/"), -- directory where session files are saved
|
dir = vim.fn.expand(vim.fn.stdpath("config") .. "/sessions/"), -- directory where session files are saved
|
||||||
|
use_git_branch = false, -- create session files based on the branch of the git enabled repository
|
||||||
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
|
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,21 @@ local e = vim.fn.fnameescape
|
||||||
|
|
||||||
function M.get_current()
|
function M.get_current()
|
||||||
local name = vim.fn.getcwd():gsub("/", "%%")
|
local name = vim.fn.getcwd():gsub("/", "%%")
|
||||||
return Config.options.dir .. name .. ".vim"
|
return Config.options.dir .. name .. M.get_branch() ..".vim"
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.get_branch()
|
||||||
|
if Config.options.use_git_branch == true then
|
||||||
|
local branch = vim.api.nvim_exec([[!git rev-parse --abbrev-ref HEAD]], true)
|
||||||
|
-- The command returns two lines. We only need the second one
|
||||||
|
lines = {}
|
||||||
|
for s in branch:gmatch("[^\r\n]+") do
|
||||||
|
table.insert(lines, '_' .. s)
|
||||||
|
end
|
||||||
|
return lines[2]:gsub("/", "%%")
|
||||||
|
end
|
||||||
|
|
||||||
|
return ''
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.get_last()
|
function M.get_last()
|
||||||
|
|
@ -33,8 +47,8 @@ end
|
||||||
|
|
||||||
function M.stop()
|
function M.stop()
|
||||||
vim.cmd([[
|
vim.cmd([[
|
||||||
autocmd! Persistence
|
autocmd! Persistence
|
||||||
augroup! Persistence
|
augroup! Persistence
|
||||||
]])
|
]])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue