Skip to content

Commit

Permalink
chore: types: Lz -> lz.n prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 7, 2024
1 parent 03d0805 commit 5b51bff
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 116 deletions.
8 changes: 4 additions & 4 deletions lua/lz/n/handler/cmd.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
local loader = require("lz.n.loader")

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

---@type LzCmdHandler
---@type lz.n.CmdHandler
local M = {
pending = {},
type = "cmd",
Expand Down Expand Up @@ -66,15 +66,15 @@ local function add_cmd(cmd)
})
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.del(plugin)
pcall(vim.api.nvim_del_user_command, plugin.cmd)
for _, plugins in pairs(M.pending) do
plugins[plugin.name] = nil
end
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.add(plugin)
if not plugin.cmd then
return
Expand Down
26 changes: 13 additions & 13 deletions lua/lz/n/handler/event.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
local loader = require("lz.n.loader")

---@class LzEventOpts
---@class lz.n.EventOpts
---@field event string
---@field group? string
---@field exclude? string[] augroups to exclude
---@field data? unknown
---@field buffer? number

---@class LzEventHandler: LzHandler
---@class lz.n.EventHandler: lz.n.Handler
---@field events table<string,true>
---@field group number
---@field parse fun(spec: LzEventSpec): LzEvent
---@field parse fun(spec: lz.n.EventSpec): lz.n.Event

---@type LzEventHandler
---@type lz.n.EventHandler
local M = {
pending = {},
events = {},
Expand All @@ -27,7 +27,7 @@ local M = {
elseif vim.islist(spec) then
ret = { id = table.concat(spec, "|"), event = spec }
else
ret = spec --[[@as LzEvent]]
ret = spec --[[@as lz.n.Event]]
if not ret.id then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = type(ret.event) == "string" and ret.event or table.concat(ret.event, "|")
Expand Down Expand Up @@ -67,12 +67,12 @@ local event_triggers = {
---@param event string
---@param buf integer
---@param data unknown
---@return LzEventOpts[]
---@return lz.n.EventOpts[]
local function get_state(event, buf, data)
---@type LzEventOpts[]
---@type lz.n.EventOpts[]
local state = {}
while event do
---@type LzEventOpts
---@type lz.n.EventOpts
local event_opts = {
event = event,
exclude = event ~= "FileType" and get_augroups(event) or nil,
Expand All @@ -87,7 +87,7 @@ local function get_state(event, buf, data)
end

-- Trigger an event
---@param opts LzEventOpts
---@param opts lz.n.EventOpts
local function _trigger(opts)
xpcall(
function()
Expand All @@ -106,7 +106,7 @@ end

-- Trigger an event. When a group is given, only the events in that group will be triggered.
-- When exclude is set, the events in those groups will be skipped.
---@param opts LzEventOpts
---@param opts lz.n.EventOpts
local function trigger(opts)
if opts.group or opts.exclude == nil then
return _trigger(opts)
Expand All @@ -125,7 +125,7 @@ local function trigger(opts)
end
end

---@param event LzEvent
---@param event lz.n.Event
local function add_event(event)
local done = false
vim.api.nvim_create_autocmd(event.event, {
Expand All @@ -149,7 +149,7 @@ local function add_event(event)
})
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.add(plugin)
for _, event in pairs(plugin.event or {}) do
M.pending[event.id] = M.pending[event.id] or {}
Expand All @@ -158,7 +158,7 @@ function M.add(plugin)
end
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.del(plugin)
for _, plugins in pairs(M.pending) do
plugins[plugin.name] = nil
Expand Down
12 changes: 6 additions & 6 deletions lua/lz/n/handler/ft.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
local event = require("lz.n.handler.event")

---@class LzFtHandler: LzHandler
---@field parse fun(spec: LzEventSpec): LzEvent
---@class lz.n.FtHandler: lz.n.Handler
---@field parse fun(spec: lz.n.EventSpec): lz.n.Event

---@type LzFtHandler
---@type lz.n.FtHandler
local M = {
pending = {},
type = "ft",
---@param value string
---@return LzEvent
---@return lz.n.Event
parse = function(value)
return {
id = value,
Expand All @@ -18,12 +18,12 @@ local M = {
end,
}

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.add(plugin)
event.add(plugin)
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.del(plugin)
event.del(plugin)
end
Expand Down
18 changes: 9 additions & 9 deletions lua/lz/n/handler/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---@class LzHandler
---@field type LzHandlerTypes
---@class lz.n.Handler
---@field type lz.n.HandlerTypes
---@field pending table<string, table<string, string>> -- key: plugin_name: plugin_name
---@field add fun(plugin: LzPlugin)
---@field del? fun(plugin: LzPlugin)
---@field add fun(plugin: lz.n.Plugin)
---@field del? fun(plugin: lz.n.Plugin)

local M = {}

---@enum LzHandlerTypes
---@enum lz.n.HandlerTypes
M.types = {
cmd = "cmd",
event = "event",
Expand All @@ -21,24 +21,24 @@ local handlers = {
keys = require("lz.n.handler.keys"),
}

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
local function enable(plugin)
for _, handler in pairs(handlers) do
handler.add(plugin)
end
-- TODO: Change handler add implementations to take a LzPlugin
-- TODO: Change handler add implementations to take a lz.n.Plugin
end

function M.disable(plugin)
for _, handler in pairs(handlers) do
if type(handler.del) == "function" then
-- TODO: Change handler del implementations to take a LzPlugin?
-- TODO: Change handler del implementations to take a lz.n.Plugin?
handler.del(plugin)
end
end
end

---@param plugins table<string, LzPlugin>
---@param plugins table<string, lz.n.Plugin>
function M.init(plugins)
for _, plugin in pairs(plugins) do
xpcall(
Expand Down
28 changes: 14 additions & 14 deletions lua/lz/n/handler/keys.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
local loader = require("lz.n.loader")

---@class LzKeysHandler: LzHandler
---@class lz.n.KeysHandler: lz.n.Handler

---@type LzKeysHandler
---@type lz.n.KeysHandler
local M = {
pending = {},
type = "keys",
---@param value string|LzKeysSpec
---@param value string|lz.n.KeysSpec
---@param mode? string
---@return LzKeys
---@return lz.n.Keys
parse = function(value, mode)
value = type(value) == "string" and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
value = type(value) == "string" and { value } or value --[[@as lz.n.KeysSpec]]
local ret = vim.deepcopy(value) --[[@as lz.n.Keys]]
ret.lhs = ret[1] or ""
ret.rhs = ret[2]
ret[1] = nil
Expand All @@ -31,10 +31,10 @@ local M = {

local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }

---@param keys LzKeys
---@return LzKeysBase
---@param keys lz.n.Keys
---@return lz.n.KeysBase
local function get_opts(keys)
---@type LzKeysBase
---@type lz.n.KeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= "number" and not skip[k] then
Expand All @@ -45,7 +45,7 @@ local function get_opts(keys)
end

-- Create a mapping if it is managed by lz.n
---@param keys LzKeys
---@param keys lz.n.Keys
---@param buf integer?
local function set(keys, buf)
if keys.rhs then
Expand All @@ -58,7 +58,7 @@ end

-- Delete a mapping and create the real global
-- mapping when needed
---@param keys LzKeys
---@param keys lz.n.Keys
local function del(keys)
pcall(vim.keymap.del, keys.mode, keys.lhs, {
-- NOTE: for buffer-local mappings, we only delete the mapping for the current buffer
Expand All @@ -72,7 +72,7 @@ local function del(keys)
end
end

---@param keys LzKeys
---@param keys lz.n.Keys
local function add_keys(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
Expand Down Expand Up @@ -124,7 +124,7 @@ local function add_keys(keys)
end
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.add(plugin)
for _, key in pairs(plugin.keys or {}) do
M.pending[key.id] = M.pending[key.id] or {}
Expand All @@ -133,7 +133,7 @@ function M.add(plugin)
end
end

---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M.del(plugin)
for _, plugins in pairs(M.pending) do
plugins[plugin.name] = nil
Expand Down
4 changes: 2 additions & 2 deletions lua/lz/n/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if not vim.loader or vim.fn.has("nvim-0.9.1") ~= 1 then
error("lz.n requires Neovim >= 0.9.1")
end

---@param spec string | LzSpec
---@param spec string | lz.n.Spec
function M.load(spec)
if vim.g.lzn_did_load then
return vim.notify("lz.n has already loaded your plugins.", vim.log.levels.WARN, { title = "lz.n" })
Expand All @@ -17,7 +17,7 @@ function M.load(spec)
if type(spec) == "string" then
spec = { import = spec }
end
---@cast spec LzSpec
---@cast spec lz.n.Spec
local plugins = require("lz.n.spec").parse(spec)
require("lz.n.loader").load_startup_plugins(plugins)
require("lz.n.state").plugins = plugins
Expand Down
20 changes: 10 additions & 10 deletions lua/lz/n/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local M = {}
local DEFAULT_PRIORITY = 50

---@package
---@param plugin LzPlugin
---@param plugin lz.n.Plugin
function M._load(plugin)
if plugin.enable == false or (type(plugin.enable) == "function" and not plugin.enable()) then
return
Expand All @@ -16,7 +16,7 @@ function M._load(plugin)
-- TODO: Load plugin
end

---@param plugins table<string, LzPlugin>
---@param plugins table<string, lz.n.Plugin>
local function run_before_all(plugins)
for _, plugin in pairs(plugins) do
if plugin.beforeAll then
Expand All @@ -34,8 +34,8 @@ local function run_before_all(plugins)
end
end

---@param plugins table<string, LzPlugin>
---@return LzPlugin[]
---@param plugins table<string, lz.n.Plugin>
---@return lz.n.Plugin[]
local function get_eager_plugins(plugins)
local result = {}
for _, plugin in pairs(plugins) do
Expand All @@ -44,15 +44,15 @@ local function get_eager_plugins(plugins)
end
end
table.sort(result, function(a, b)
---@cast a LzPlugin
---@cast b LzPlugin
---@cast a lz.n.Plugin
---@cast b lz.n.Plugin
return (a.priority or DEFAULT_PRIORITY) > (b.priority or DEFAULT_PRIORITY)
end)
return result
end

--- Loads startup plugins, removing loaded plugins from the table
---@param plugins table<string, LzPlugin>
---@param plugins table<string, lz.n.Plugin>
function M.load_startup_plugins(plugins)
run_before_all(plugins)
for _, plugin in pairs(get_eager_plugins(plugins)) do
Expand All @@ -61,10 +61,10 @@ function M.load_startup_plugins(plugins)
end
end

---@param plugins string | LzPlugin | string[] | LzPlugin[]
---@param plugins string | lz.n.Plugin | string[] | lz.n.Plugin[]
function M.load(plugins)
plugins = (type(plugins) == "string" or plugins.name) and { plugins } or plugins
---@cast plugins (string|LzPlugin)[]
---@cast plugins (string|lz.n.Plugin)[]
for _, plugin in pairs(plugins) do
local loadable = true
if type(plugin) == "string" then
Expand All @@ -74,7 +74,7 @@ function M.load(plugins)
vim.notify("Plugin " .. plugin .. " not found", vim.log.levels.ERROR, { title = "lz.n" })
loadable = false
end
---@cast plugin LzPlugin
---@cast plugin lz.n.Plugin
end
if loadable then
M._load(plugin)
Expand Down
Loading

0 comments on commit 5b51bff

Please sign in to comment.