Skip to content

Commit

Permalink
refactor(handlers): don't expose state
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Aug 20, 2024
1 parent 4290974 commit 06a6c3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
17 changes: 9 additions & 8 deletions lua/lz/n/handler/cmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ local loader = require("lz.n.loader")

---@class lz.n.CmdHandler: lz.n.Handler

---@type table<string, table<string, lz.n.Plugin[]>>
local pending = {}

---@type lz.n.CmdHandler
local M = {
---@type table<string, table<string, lz.n.Plugin[]>>
pending = {},
spec_field = "cmd",
}

---@param name string
---@return lz.n.Plugin?
function M.lookup(name)
return require("lz.n.handler.extra").lookup(M.pending, name)
return require("lz.n.handler.extra").lookup(pending, name)
end

---@param cmd string
local function load(cmd)
vim.api.nvim_del_user_command(cmd)
loader.load(vim.tbl_values(M.pending[cmd]))
loader.load(vim.tbl_values(pending[cmd]))
end

---@param cmd string
Expand Down Expand Up @@ -49,7 +50,7 @@ local function add_cmd(cmd)
if not info then
vim.schedule(function()
---@type string
local plugins = "`" .. table.concat(vim.tbl_values(M.pending[cmd]), ", ") .. "`"
local plugins = "`" .. table.concat(vim.tbl_values(pending[cmd]), ", ") .. "`"
vim.notify("Command `" .. cmd .. "` not found after loading " .. plugins, vim.log.levels.ERROR)
end)
return
Expand All @@ -75,7 +76,7 @@ end

---@param name string
function M.del(name)
vim.iter(M.pending)
vim.iter(pending)
:filter(function(_, plugins)
return plugins[name] ~= nil
end)
Expand All @@ -92,8 +93,8 @@ function M.add(plugin)
end
---@param cmd string
vim.iter(plugin.cmd):each(function(cmd)
M.pending[cmd] = M.pending[cmd] or {}
M.pending[cmd][plugin.name] = plugin
pending[cmd] = pending[cmd] or {}
pending[cmd][plugin.name] = plugin
add_cmd(cmd)
end)
end
Expand Down
17 changes: 9 additions & 8 deletions lua/lz/n/handler/colorscheme.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@ local loader = require("lz.n.loader")
---@class lz.n.ColorschemeHandler: lz.n.Handler
---@field augroup? integer

local pending = {}

---@type lz.n.ColorschemeHandler
local M = {
---@type table<string, table<string, lz.n.Plugin[]>>
pending = {},
augroup = nil,
spec_field = "colorscheme",
}

---@param name string
---@return lz.n.Plugin?
function M.lookup(name)
return require("lz.n.handler.extra").lookup(M.pending, name)
return require("lz.n.handler.extra").lookup(pending, name)
end

---@param name string
function M.del(name)
vim.iter(M.pending):each(function(_, plugins)
vim.iter(pending):each(function(_, plugins)
plugins[name] = nil
end)
end

---@param name string
local function on_colorscheme(name)
local pending = M.pending[name] or {}
if vim.tbl_isempty(pending) then
local plugins = pending[name] or {}
if vim.tbl_isempty(plugins) then
-- already loaded
return
end
loader.load(vim.tbl_values(pending))
loader.load(vim.tbl_values(plugins))
end

local function init()
Expand All @@ -55,8 +56,8 @@ function M.add(plugin)
init()
---@param colorscheme string
vim.iter(plugin.colorscheme):each(function(colorscheme)
M.pending[colorscheme] = M.pending[colorscheme] or {}
M.pending[colorscheme][plugin.name] = plugin
pending[colorscheme] = pending[colorscheme] or {}
pending[colorscheme][plugin.name] = plugin
end)
end

Expand Down
17 changes: 9 additions & 8 deletions lua/lz/n/handler/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ local lz_n_events = {

lz_n_events["User DeferredUIEnter"] = lz_n_events.DeferredUIEnter

---@type table<string, table<string, lz.n.Plugin[]>>
local pending = {}

---@type lz.n.EventHandler
local M = {
---@type table<string, table<string, lz.n.Plugin[]>>
pending = {},
events = {},
group = vim.api.nvim_create_augroup("lz_n_handler_event", { clear = true }),
spec_field = "event",
Expand Down Expand Up @@ -60,7 +61,7 @@ local M = {
---@param name string
---@return lz.n.Plugin?
function M.lookup(name)
return require("lz.n.handler.extra").lookup(M.pending, name)
return require("lz.n.handler.extra").lookup(pending, name)
end

-- Get all augroups for an event
Expand Down Expand Up @@ -151,14 +152,14 @@ local function add_event(event)
once = true,
pattern = event.pattern,
callback = function(ev)
if done or not M.pending[event.id] then
if done or not pending[event.id] then
return
end
-- HACK: work-around for https://github.com/neovim/neovim/issues/25526
done = true
local state = get_state(ev.event, ev.buf, ev.data)
-- load the plugins
loader.load(M.pending[event.id])
loader.load(pending[event.id])
-- check if any plugin created an event handler for this event and fire the group
---@param s lz.n.EventOpts
vim.iter(state):each(function(s)
Expand All @@ -172,15 +173,15 @@ end
function M.add(plugin)
---@param event lz.n.Event
vim.iter(plugin.event or {}):each(function(event)
M.pending[event.id] = M.pending[event.id] or {}
M.pending[event.id][plugin.name] = plugin
pending[event.id] = pending[event.id] or {}
pending[event.id][plugin.name] = plugin
add_event(event)
end)
end

---@param name string
function M.del(name)
vim.iter(M.pending):each(function(_, plugins)
vim.iter(pending):each(function(_, plugins)
plugins[name] = nil
end)
end
Expand Down
19 changes: 10 additions & 9 deletions lua/lz/n/handler/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ local function parse(value, mode)
return ret
end

---@type table<string, table<string, lz.n.Plugin[]>>
local pending = {}

---@type lz.n.KeysHandler
local M = {
---@type table<string, table<string, lz.n.Plugin[]>>
pending = {},
spec_field = "keys",
---@param value string|lz.n.KeysSpec
---@return lz.n.Keys[]
Expand All @@ -47,7 +48,7 @@ local M = {
---@param name string
---@return lz.n.Plugin?
function M.lookup(name)
return require("lz.n.handler.extra").lookup(M.pending, name)
return require("lz.n.handler.extra").lookup(pending, name)
end

local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
Expand Down Expand Up @@ -100,10 +101,10 @@ local function add_keys(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.pending[keys.id]
local plugins = pending[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.pending[keys.id] = nil
pending[keys.id] = nil
if plugins then
loader.load(plugins)
end
Expand All @@ -130,7 +131,7 @@ local function add_keys(keys)
vim.api.nvim_create_autocmd("FileType", {
pattern = keys.ft,
callback = function(event)
if M.pending[keys.id] then
if pending[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
Expand All @@ -148,15 +149,15 @@ end
function M.add(plugin)
---@param key lz.n.Keys
vim.iter(plugin.keys or {}):each(function(key)
M.pending[key.id] = M.pending[key.id] or {}
M.pending[key.id][plugin.name] = plugin
pending[key.id] = pending[key.id] or {}
pending[key.id][plugin.name] = plugin
add_keys(key)
end)
end

---@param name string
function M.del(name)
vim.iter(M.pending):each(function(_, plugins)
vim.iter(pending):each(function(_, plugins)
plugins[name] = nil
end)
end
Expand Down

0 comments on commit 06a6c3e

Please sign in to comment.