Skip to content

Commit

Permalink
feat(nvim-plug): improve lazy loading opt
Browse files Browse the repository at this point in the history
make `on_ft/map/func/cmd` support string or table<string>
  • Loading branch information
wsdjeg committed Feb 11, 2025
1 parent d7b95d3 commit 0532e15
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 5 additions & 6 deletions bundle/nvim-plug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require('plug').setup({
max_processes = 5,
base_url = 'https://github.com',
-- default ui is `notify`, use `default` for split window UI
ui = 'notify',
ui = 'default',
-- default is nil
http_proxy = 'http://127.0.0.1:7890',
-- default is nil
Expand All @@ -60,7 +60,6 @@ require('plug').add({
{
'wsdjeg/scrollbar.vim',
events = { 'VimEnter' },
config = function() end,
},
{
'wsdjeg/vim-chat',
Expand Down Expand Up @@ -98,13 +97,13 @@ The plugin spec is inspired by [dein.nvim](https://github.com/Shougo/dein.vim).
| name | description |
| --------------- | ------------------------------------------------------------------------------------------------------------- |
| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` |
| `cmds` | `table<string>`, commands lazy loading |
| `events` | `table<string>`, events lazy loading |
| `cmds` | `string` or `table<string>`, commands lazy loading |
| `events` | `string` or `table<string>`, events lazy loading |
| `config` | `function`, function called after adding plugin path to nvim rtp, before loading files in `plugin/` directory |
| `config_after` | `function`, function called after loading files in `plugin/` directory |
| `config_before` | `function`, function called when `plug.add()` function is called |
| `on_ft` | `table<string>`, filetypes lazy loading |
| `on_map` | `table<string>`, key bindings lazy loading |
| `on_ft` | `string` or `table<string>`, filetypes lazy loading |
| `on_map` | `string` or `table<string>`, key bindings lazy loading |
| `on_func` | `string` or `table<string>`, vim function lazy loading |
| `script_type` | `string`, plugin type including `color`, `plugin`, etc.. |
| `build` | `string` or `table<string>`, executed by [job](https://spacevim.org/api/job/) api |
Expand Down
20 changes: 16 additions & 4 deletions bundle/nvim-plug/lua/plug/hooks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ local cmd_plugins = {}
local on_ft_plugins = {}
local on_fn_plugins = {}

--- @param events string | table<string>
--- @param plugSpec PluginSpec
function M.on_events(events, plugSpec)
if type(events) == 'string' then
events = { events }
end
event_plugins[plugSpec.name] = vim.api.nvim_create_autocmd(events, {
group = group,
pattern = { '*' },
Expand All @@ -23,24 +28,30 @@ function M.on_events(events, plugSpec)
})
end

--- @param cmds table<string>
--- @param cmds string | table<string>
--- @param plugSpec PluginSpec
function M.on_cmds(cmds, plugSpec)
if type(cmds) == 'string' then
cmds = { cmds }
end
for _, cmd in ipairs(cmds) do
cmd_plugins[cmd] = plugSpec
vim.api.nvim_create_user_command(cmd, function(opt)
plugin_loader.load(cmd_plugins[opt.name])
vim.cmd(opt.name .. ' ' .. opt.args)
end, {
nargs = '*',
complete = function(...)
complete = function(_)
return {}
end,
})
end
end

function M.on_ft(fts, plugSpec)
if type(fts) == 'string' then
fts = { fts }
end
on_ft_plugins[plugSpec.name] = vim.api.nvim_create_autocmd({ 'FileType' }, {
group = group,
pattern = fts,
Expand All @@ -55,6 +66,9 @@ function M.on_ft(fts, plugSpec)
end

function M.on_map(maps, plugSpec)
if type(maps) == 'string' then
maps = { maps }
end
for _, lhs in ipairs(maps) do
vim.keymap.set('n', lhs, function()
for _, v in ipairs(plugSpec.on_map) do
Expand Down Expand Up @@ -100,12 +114,10 @@ function M.on_func(fn, plugSpec)
group = group,
pattern = fns,
callback = function(_)

if on_fn_plugins[plugSpec.name] then
vim.api.nvim_del_autocmd(on_fn_plugins[plugSpec.name])
end
plugin_loader.load(plugSpec)

end,
})
end
Expand Down

0 comments on commit 0532e15

Please sign in to comment.