From 06a6c3e3e2b30464a223453add22a92e3e2b563d Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Tue, 20 Aug 2024 21:35:44 +0200 Subject: [PATCH] refactor(handlers): don't expose state --- lua/lz/n/handler/cmd.lua | 17 +++++++++-------- lua/lz/n/handler/colorscheme.lua | 17 +++++++++-------- lua/lz/n/handler/event.lua | 17 +++++++++-------- lua/lz/n/handler/keys.lua | 19 ++++++++++--------- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/lua/lz/n/handler/cmd.lua b/lua/lz/n/handler/cmd.lua index 292613d..038c7c6 100644 --- a/lua/lz/n/handler/cmd.lua +++ b/lua/lz/n/handler/cmd.lua @@ -2,23 +2,24 @@ local loader = require("lz.n.loader") ---@class lz.n.CmdHandler: lz.n.Handler +---@type table> +local pending = {} + ---@type lz.n.CmdHandler local M = { - ---@type table> - 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 @@ -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 @@ -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) @@ -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 diff --git a/lua/lz/n/handler/colorscheme.lua b/lua/lz/n/handler/colorscheme.lua index 5169822..182295a 100644 --- a/lua/lz/n/handler/colorscheme.lua +++ b/lua/lz/n/handler/colorscheme.lua @@ -3,10 +3,11 @@ 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> - pending = {}, augroup = nil, spec_field = "colorscheme", } @@ -14,24 +15,24 @@ 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 ---@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() @@ -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 diff --git a/lua/lz/n/handler/event.lua b/lua/lz/n/handler/event.lua index adb6f03..541aa9f 100644 --- a/lua/lz/n/handler/event.lua +++ b/lua/lz/n/handler/event.lua @@ -18,10 +18,11 @@ local lz_n_events = { lz_n_events["User DeferredUIEnter"] = lz_n_events.DeferredUIEnter +---@type table> +local pending = {} + ---@type lz.n.EventHandler local M = { - ---@type table> - pending = {}, events = {}, group = vim.api.nvim_create_augroup("lz_n_handler_event", { clear = true }), spec_field = "event", @@ -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 @@ -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) @@ -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 diff --git a/lua/lz/n/handler/keys.lua b/lua/lz/n/handler/keys.lua index b99d525..34525f7 100644 --- a/lua/lz/n/handler/keys.lua +++ b/lua/lz/n/handler/keys.lua @@ -23,10 +23,11 @@ local function parse(value, mode) return ret end +---@type table> +local pending = {} + ---@type lz.n.KeysHandler local M = { - ---@type table> - pending = {}, spec_field = "keys", ---@param value string|lz.n.KeysSpec ---@return lz.n.Keys[] @@ -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 } @@ -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 @@ -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 @@ -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