1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-14 03:18:01 +08:00

feat(nvim-plug): improve lazy loading opt

make `on_ft/map/func/cmd` support string or table<string>
This commit is contained in:
Eric Wong 2025-02-11 23:31:25 +08:00
parent d7b95d3f45
commit 0532e157f3
No known key found for this signature in database
GPG Key ID: 41BB7053E835C848
2 changed files with 21 additions and 10 deletions

View File

@ -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
@ -60,7 +60,6 @@ require('plug').add({
{
'wsdjeg/scrollbar.vim',
events = { 'VimEnter' },
config = function() end,
},
{
'wsdjeg/vim-chat',
@ -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 |

View File

@ -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 = { '*' },
@ -23,9 +28,12 @@ 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)
@ -33,7 +41,7 @@ function M.on_cmds(cmds, plugSpec)
vim.cmd(opt.name .. ' ' .. opt.args)
end, {
nargs = '*',
complete = function(...)
complete = function(_)
return {}
end,
})
@ -41,6 +49,9 @@ function M.on_cmds(cmds, plugSpec)
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,
@ -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
@ -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