draft: initial implementation

This commit is contained in:
Marc Jakobi
2024-04-11 20:46:06 +02:00
parent ac72bb5c76
commit e61adde6ce
15 changed files with 904 additions and 102 deletions
+88
View File
@@ -0,0 +1,88 @@
local loader = require('lz.n.loader')
---@class LzCmdHandler: LzHandler
---@type LzCmdHandler
local M = {
active = {},
managed = {},
type = 'cmd',
}
---@param cmd string
local function load(cmd)
vim.api.nvim_del_user_command(cmd)
loader.load(M.active[cmd])
end
---@param cmd string
local function add(cmd)
vim.api.nvim_create_user_command(cmd, function(event)
---@cast event vim.api.keyset.user_command
local command = {
cmd = cmd,
bang = event.bang or nil,
---@diagnostic disable-next-line: undefined-field
mods = event.smods,
---@diagnostic disable-next-line: undefined-field
args = event.fargs,
count = event.count >= 0 and event.range == 0 and event.count or nil,
}
if event.range == 1 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1 }
elseif event.range == 2 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1, event.line2 }
end
---@type string
local plugins = '`' .. table.concat(vim.tbl_values(M.active[cmd]), ', ') .. '`'
load(cmd)
local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
if not info then
vim.schedule(function()
vim.notify('Command `' .. cmd .. '` not found after loading ' .. plugins, vim.log.levels.ERROR)
end)
return
end
command.nargs = info.nargs
---@diagnostic disable-next-line: undefined-field
if event.args and event.args ~= '' and info.nargs and info.nargs:find('[1?]') then
---@diagnostic disable-next-line: undefined-field
command.args = { event.args }
end
vim.cmd(command)
end, {
bang = true,
range = true,
nargs = '*',
complete = function(_, line)
load(cmd)
return vim.fn.getcompletion(line, 'cmdline')
end,
})
end
---@param cmd string
function M.del(cmd)
pcall(vim.api.nvim_del_user_command, cmd)
end
---@param plugin LzPlugin
function M.add(plugin)
if not plugin.cmd then
return
end
for _, cmd in pairs(plugin.cmd) do
M.active[cmd] = M.active[cmd] or {}
M.active[cmd][plugin.name] = plugin.name
add(cmd)
end
end
return M
+147
View File
@@ -0,0 +1,147 @@
local loader = require('lz.n.loader')
---@class LzEventOpts
---@field event string
---@field group? string
---@field exclude? string[] augroups to exclude
---@field data? unknown
---@field buffer? number
---@class LzEventHandler: LzHandler
---@field events table<string,true>
---@field group number
---@type LzEventHandler
local M = {
active = {},
managed = {},
type = 'event',
}
---@param spec LzEventSpec
---@return LzEvent
function M.parse(spec)
local ret
if type(spec) == 'string' then
local event, pattern = spec:match('^(%w+)%s+(.*)$')
event = event or spec
return { id = spec, event = event, pattern = pattern }
elseif vim.tbl_islist(spec) then
ret = { id = table.concat(spec, '|'), event = spec }
else
ret = spec --[[@as LzEvent]]
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, '|')
if ret.pattern then
---@diagnostic disable-next-line: assign-type-mismatch, param-type-mismatch
ret.id = ret.id .. ' ' .. (type(ret.pattern) == 'string' and ret.pattern or table.concat(ret.pattern, ', '))
end
end
end
return ret
end
-- Get all augroups for an event
---@param event string
local function get_augroups(event)
---@type string[]
local groups = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds { event = event }) do
if autocmd.group_name then
table.insert(groups, autocmd.group_name)
end
end
return groups
end
local event_triggers = {
FileType = 'BufReadPost',
BufReadPost = 'BufReadPre',
}
-- Get the current state of the event and all the events that will be fired
---@param event string
---@param buf integer
---@param data unknown
---@return LzEventOpts[]
local function get_state(event, buf, data)
---@type LzEventOpts[]
local state = {}
while event do
---@type LzEventOpts
local event_opts = {
event = event,
exclude = event ~= 'FileType' and get_augroups(event) or nil,
buffer = buf,
data = data,
}
table.insert(state, 1, event_opts)
data = nil -- only pass the data to the first event
event = event_triggers[event]
end
return state
end
-- Trigger an event
---@param opts LzEventOpts
local function _trigger(opts)
xpcall(
function()
vim.api.nvim_exec_autocmds(opts.event, {
buffer = opts.buffer,
group = opts.group,
modeline = false,
data = opts.data,
})
end,
vim.schedule_wrap(function(err)
vim.notify(err, vim.log.levels.ERROR)
end)
)
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
local function trigger(opts)
if opts.group or opts.exclude == nil then
return _trigger(opts)
end
---@type table<string,true>
local done = {}
for _, autocmd in ipairs(vim.api.nvim_get_autocmds { event = opts.event }) do
local id = autocmd.event .. ':' .. (autocmd.group or '') ---@type string
local skip = done[id] or (opts.exclude and vim.tbl_contains(opts.exclude, autocmd.group_name))
done[id] = true
if autocmd.group and not skip then
opts.group = autocmd.group_name
_trigger(opts)
end
end
end
---@param event LzEvent
function M.add(event)
local done = false
vim.api.nvim_create_autocmd(event.event, {
group = M.group,
once = true,
pattern = event.pattern,
callback = function(ev)
if done or not M.active[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.active[event.id])
-- check if any plugin created an event handler for this event and fire the group
for _, s in ipairs(state) do
trigger(s)
end
end,
})
end
return M
+27
View File
@@ -0,0 +1,27 @@
local event = require('lz.n.handler.event')
---@class LzFtHandler: LzHandler
---@type LzFtHandler
local M = {
active = {},
managed = {},
type = 'ft',
}
---@param value string
---@return LzEvent
function M.parse(value)
return {
id = value,
event = 'FileType',
pattern = value,
}
end
---@param lz_event LzEvent
function M.add(lz_event)
event.add(lz_event)
end
return M
+55
View File
@@ -0,0 +1,55 @@
---@class LzHandler
---@field type LzHandlerTypes
---@field active table<string,table<string,string>>
---@field managed table<string,string>
---@field add fun(plugin: LzPlugin)
---@field del? fun(plugin: LzPlugin)
local M = {}
---@enum LzHandlerTypes
M.types = {
cmd = 'cmd',
event = 'event',
ft = 'ft',
keys = 'keys',
}
local handlers = {
cmd = require('lz.n.handler.cmd'),
event = require('lz.n.handler.event'),
ft = require('lz.n.handler.ft'),
keys = require('lz.n.handler.keys'),
}
---@param plugin LzPlugin
local function enable(plugin)
for _, handler in pairs(handlers) do
handler.add(plugin)
end
-- TODO: Change handler add implementations to take a LzPlugin
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?
handler.del(plugin)
end
end
end
---@param plugins table<string, LzPlugin>
function M.init(plugins)
for _, plugin in pairs(plugins) do
xpcall(
enable,
vim.schedule_wrap(function(err)
vim.notify(('Failed to enable handlers for %s: %s'):format(plugin.name, err), vim.log.levels.ERROR)
end),
plugin
)
end
end
return M
+128
View File
@@ -0,0 +1,128 @@
local loader = require('lz.n.loader')
---@class LzKeysHandler: LzHandler
---@type LzKeysHandler
local M = {
active = {},
managed = {},
type = 'keys',
}
---@param value string|LzKeysSpec
---@param mode? string
---@return LzKeys
function M.parse(value, mode)
value = type(value) == 'string' and { value } or value --[[@as LzKeysSpec]]
local ret = vim.deepcopy(value) --[[@as LzKeys]]
ret.lhs = ret[1] or ''
ret.rhs = ret[2]
ret[1] = nil
ret[2] = nil
ret.mode = mode or 'n'
ret.id = vim.api.nvim_replace_termcodes(ret.lhs, true, true, true)
if ret.ft then
local ft = type(ret.ft) == 'string' and { ret.ft } or ret.ft --[[@as string[] ]]
ret.id = ret.id .. ' (' .. table.concat(ft, ', ') .. ')'
end
if ret.mode ~= 'n' then
ret.id = ret.id .. ' (' .. ret.mode .. ')'
end
return ret
end
local skip = { mode = true, id = true, ft = true, rhs = true, lhs = true }
---@param keys LzKeys
---@return LzKeysBase
local function get_opts(keys)
---@type LzKeysBase
local opts = {}
for k, v in pairs(keys) do
if type(k) ~= 'number' and not skip[k] then
opts[k] = v
end
end
return opts
end
-- Create a mapping if it is managed by lz.n
---@param keys LzKeys
---@param buf integer?
local function set(keys, buf)
if keys.rhs then
local opts = get_opts(keys)
opts.buffer = buf
vim.keymap.set(keys.mode, keys.lhs, keys.rhs, opts)
end
end
-- Delete a mapping and create the real global
-- mapping when needed
---@param keys LzKeys
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
-- So the mapping could still exist in other buffers
buffer = keys.ft and true or nil,
})
-- make sure to create global mappings when needed
-- buffer-local mappings are managed by lazy
if not keys.ft then
set(keys)
end
end
---@param keys LzKeys
M.add = function(keys)
local lhs = keys.lhs
local opts = get_opts(keys)
---@param buf? number
local function add(buf)
vim.keymap.set(keys.mode, lhs, function()
local plugins = M.active[keys.id]
-- always delete the mapping immediately to prevent recursive mappings
del(keys)
M.active[keys.id] = nil
if plugins then
loader.load(plugins)
end
-- Create the real buffer-local mapping
if keys.ft then
set(keys, buf)
end
if keys.mode:sub(-1) == 'a' then
lhs = lhs .. '<C-]>'
end
local feed = vim.api.nvim_replace_termcodes('<Ignore>' .. lhs, true, true, true)
-- insert instead of append the lhs
vim.api.nvim_feedkeys(feed, 'i', false)
end, {
desc = opts.desc,
nowait = opts.nowait,
-- we do not return anything, but this is still needed to make operator pending mappings work
expr = true,
buffer = buf,
})
end
-- buffer-local mappings
if keys.ft then
vim.api.nvim_create_autocmd('FileType', {
pattern = keys.ft,
callback = function(event)
if M.active[keys.id] then
add(event.buf)
else
-- Only create the mapping if its managed by lz.n
-- otherwise the plugin is supposed to manage it
set(keys, event.buf)
end
end,
})
else
add()
end
end
return M