1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-14 05:27:59 +08:00

feat(nvim-plug): add on_ft support

This commit is contained in:
Eric Wong 2025-02-04 23:50:08 +08:00
parent bbf79d8b4c
commit e80046552b
No known key found for this signature in database
GPG Key ID: 41BB7053E835C848
3 changed files with 49 additions and 23 deletions

View File

@ -5,6 +5,8 @@
[![](https://spacevim.org/img/build-with-SpaceVim.svg)](https://spacevim.org) [![](https://spacevim.org/img/build-with-SpaceVim.svg)](https://spacevim.org)
[![GPLv3 License](https://img.spacevim.org/license-GPLv3-blue.svg)](LICENSE) [![GPLv3 License](https://img.spacevim.org/license-GPLv3-blue.svg)](LICENSE)
**Alpha version. Any changes, including backward incompatible changes, are applied without announcements.**
## Usage ## Usage
```lua ```lua
@ -32,9 +34,11 @@ require("plug").add({
## Plugin Spec ## Plugin Spec
| name | description | | name | description |
| ------ | ------------------------------------------------------- | | -------- | ------------------------------------------------------- |
| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | | `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` |
| `cmds` | `table<string>`, commands lazy loading | | `cmds` | `table<string>`, commands lazy loading |
| `events` | `table<string>`, events lazy loading |
| `on_ft` | `table<string>`, filetypes lazy loading |
## Commands ## Commands

View File

@ -1,18 +1,22 @@
local M = {} local M = {}
local group = vim.api.nvim_create_augroup("plugin_hooks", { clear = true }) local group = vim.api.nvim_create_augroup('plugin_hooks', { clear = true })
local plugin_loader = require("plug.loader") local plugin_loader = require('plug.loader')
local event_plugins = {} local event_plugins = {}
local cmd_plugins = {} local cmd_plugins = {}
local on_ft_plugins = {}
function M.on_events(events, plugSpec) function M.on_events(events, plugSpec)
event_plugins[plugSpec.name] = vim.api.nvim_create_autocmd(events, { event_plugins[plugSpec.name] = vim.api.nvim_create_autocmd(events, {
group = group, group = group,
pattern = { "*" }, pattern = { '*' },
callback = function(_) callback = function(_)
vim.api.nvim_del_autocmd(event_plugins[plugSpec.name]) vim.api.nvim_del_autocmd(event_plugins[plugSpec.name])
if on_ft_plugins[plugSpec.name] then
vim.api.nvim_del_autocmd(on_ft_plugins[plugSpec.name])
end
plugin_loader.load(plugSpec) plugin_loader.load(plugSpec)
end, end,
}) })
@ -27,7 +31,7 @@ function M.on_cmds(cmds, plugSpec)
plugin_loader.load(cmd_plugins[opt.name]) plugin_loader.load(cmd_plugins[opt.name])
vim.cmd(opt.name .. ' ' .. opt.args) vim.cmd(opt.name .. ' ' .. opt.args)
end, { end, {
nargs = "*", nargs = '*',
complete = function(...) complete = function(...)
return {} return {}
end, end,
@ -35,4 +39,18 @@ function M.on_cmds(cmds, plugSpec)
end end
end end
function M.on_ft(fts, plugSpec)
on_ft_plugins[plugSpec.name] = vim.api.nvim_create_autocmd({ 'FileType' }, {
group = group,
pattern = fts,
callback = function(_)
vim.api.nvim_del_autocmd(on_ft_plugins[plugSpec.name])
if event_plugins[plugSpec.name] then
vim.api.nvim_del_autocmd(event_plugins[plugSpec.name])
end
plugin_loader.load(plugSpec)
end,
})
end
return M return M

View File

@ -29,7 +29,11 @@ function M.add(plugins)
hooks.on_events(plug.events, plug) hooks.on_events(plug.events, plug)
end end
if not plug.events and not plug.cmds then if plug.on_ft then
hooks.on_ft(plug.on_ft, plug)
end
if not plug.events and not plug.cmds and not plug.on_ft then
loader.load(plug) loader.load(plug)
end end
end end