presser.nvim/presser/context_manager/init.lua
TheOnePath 543c72a2f4 Added context manager module
Added a context manager for handling and managing windows/buffers
constructed by built-ins. This was handled by the `new()` function in
presser/init.lua but is now a dedicated module.
2023-01-13 17:18:56 +00:00

46 lines
1008 B
Lua

local a = vim.api
local g = vim.g
-- setup a new global context manager for windows/buffers
g.presser_buf_ctx = g.presser_buf_ctx or {}
local M = {}
local get_ctx_head = function ( ctx )
return g.presser_buf_ctx[ctx][1]
end
M.create = function ( ctx )
local c = g.presser_buf_ctx
-- create specific context manager if it doesn't exist
if not c[ctx] then
c[ctx] = {}
g.presser_buf_ctx = c
end
end
-- :TODO: restructure function to allow for new data structure (see todo file)
M.update = function ( ctx, data )
local c = g.presser_buf_ctx
-- ensure that the context exists
if not c[ctx] then
M.create( ctx )
end
table.insert(c[ctx], data)
g.presser_buf_ctx = c
end
M.flush = function ()
for ctx, ctx_tbl in pairs( g.presser_buf_ctx ) do
for _, win_id in pairs( ctx_tbl ) do
a.nvim_win_close( win_id, true ) -- buffer contents are irrelevant in this context to save
end
end
g.presser_buf_ctx = {} -- clear the global context manager
end
return M