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

129 lines
3.2 KiB
Lua
Raw Normal View History

2025-02-12 11:49:02 +01:00
-- 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',
2025-02-12 11:49:02 +01:00
function()
require('fzf-lua-p').projects()
end,
'n',
2025-02-12 11:49:02 +01:00
},
},
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()
2025-02-12 11:49:02 +01:00
local cwd = vim.loop.cwd()
if not cwd then
return
end
2025-02-12 11:49:02 +01:00
-- 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
2025-02-12 11:49:02 +01:00
-- 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,
2025-02-12 11:49:02 +01:00
})
end,
opts = {
-- Default Workspaces
--- Any folder with a '.git' within a workspace is a 'project'.
workspaces = {
-- General
{ '~/src', { '.git' } },
2025-02-12 11:49:02 +01:00
-- Specific
--- Blender
{ '~/src/blender', { '.git' } },
2025-02-12 11:49:02 +01:00
--- College
{ '~/src/college/bsc', { '.active_course' } },
{ '~/src/college/msc', { '.active_course' } },
2025-02-12 11:49:02 +01:00
--- Infrastructures
{ '~/src/infras/clusters', { '.git' } },
2025-02-12 11:49:02 +01:00
--- Photonics
{ '~/src/photonics/books', { '.git' } },
2025-02-12 11:49:02 +01:00
{ '~/src/photonics/packages', { '.git' } },
--- Neovim
{ '~/comps/neovim', { '.git' } },
{ '~/.config', { 'nvim.version' } },
2025-02-12 11:49:02 +01:00
},
},
}