nvim-snappy/lua/plugins/workflow/projections.lua

125 lines
3.3 KiB
Lua

-- projections.nvim
--- Lightweight workspace, project and session manager.
local startswith = function(text, prefix)
return text:find(prefix, 1, true) == 1
end
local function store_session()
local cwd = vim.loop.cwd()
if cwd and require('projections.session').info(cwd) then
require('projections.session').store(cwd)
end
end
-- Plugin
return {
'gnikdroy/projections.nvim',
commit = '889914169fa1f5c00fb8659653b5a8330fb223e6',
lazy = true,
dependencies = {
{
'nyngwang/fzf-lua-projections.nvim',
commit = '411672ab6f7c38d3a4a51916fda1a01c1618ae04',
},
},
keys = {
{
"<leader>sp",
function()
require('fzf-lua-p').projects()
end,
"n",
},
},
init = function()
vim.opt.sessionoptions = 'blank,buffers,folds,globals,help,localoptions,resize,tabpages,terminal'
--####################
--# - Store Session on Close Vim
--####################
vim.api.nvim_create_autocmd({ 'VimLeavePre' }, {
callback = store_session,
})
--####################
--# - Store Session on Timer
--####################
vim.api.nvim_create_autocmd({ 'BufWritePre' }, {
callback = store_session,
})
--local timer = vim.uv.new_timer()
--timer:start(1000, 1000, vim.schedule_wrap(store_session))
--####################
--# - Restore Session on Open Vim
--####################
vim.api.nvim_create_autocmd({ 'VimEnter' }, {
callback = function()
local workspaces = require("projections.workspace").get_workspaces()
local cwd = vim.loop.cwd()
if not cwd then return end
-- If Currently Within a Project, CD to that Project
for _, ws in ipairs(workspaces) do
for _, project in ipairs(ws:projects()) do
local path_project = project:path().path
if startswith(cwd, path_project) then
vim.cmd('cd ' .. path_project)
end
end
end
if not cwd then return end
-- If Path was Opened: Retrieve That Path
local path_opened_file = nil
if next(vim.fn.argv()) then
path_opened_file = vim.fn.argv(0)
end
-- If CWD is a Project: Restore Project Session
cwd = vim.loop.cwd()
local session_info = require('projections.session').info(cwd)
if session_info ~= nil then
require('projections.session').restore(cwd)
end
-- If Originally Opened w/File, Open it Now
--- This makes sure that, although we restored the session...
--- ...the file requested to edit will still be opened for editing.
if path_opened_file ~= nil then
vim.cmd('e ' .. path_opened_file)
end
-- TODO: Perhaps we need to question the user a little more about what they want.
end
})
end,
opts = {
-- Default Workspaces
--- Any folder with a '.git' within a workspace is a 'project'.
workspaces = {
-- General
{ '~/src', { '.git' } },
-- Specific
--- Blender
{ '~/src/blender', { '.git' } },
--- College
{ '~/src/college/bsc', { ".active_course" } },
{ '~/src/college/msc', { ".active_course" } },
--- Infrastructures
{ '~/src/infras/clusters', { '.git' } },
--- Photonics
{ '~/src/photonics/books', { '.git' } },
{ '~/src/photonics/packages', { '.git' } },
--- Neovim
{ "~/comps/neovim", { '.git' } },
{ "~/.config", { 'nvim.version' } },
},
},
}