From e80046552b3c308ed4ce2fe575eabe7ed7ccd41a Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 4 Feb 2025 23:50:08 +0800 Subject: [PATCH] feat(nvim-plug): add `on_ft` support --- bundle/nvim-plug/README.md | 12 ++++--- bundle/nvim-plug/lua/plug/hooks.lua | 54 +++++++++++++++++++---------- bundle/nvim-plug/lua/plug/init.lua | 6 +++- 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/bundle/nvim-plug/README.md b/bundle/nvim-plug/README.md index 14f4d399a..3814c1df5 100644 --- a/bundle/nvim-plug/README.md +++ b/bundle/nvim-plug/README.md @@ -5,6 +5,8 @@ [![](https://spacevim.org/img/build-with-SpaceVim.svg)](https://spacevim.org) [![GPLv3 License](https://img.spacevim.org/license-GPLv3-blue.svg)](LICENSE) +**Alpha version. Any changes, including backward incompatible changes, are applied without announcements.** + ## Usage ```lua @@ -31,10 +33,12 @@ require("plug").add({ ## Plugin Spec -| name | description | -| ------ | ------------------------------------------------------- | -| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | -| `cmds` | `table`, commands lazy loading | +| name | description | +| -------- | ------------------------------------------------------- | +| `[1]` | `string`, plugin repo short name, `wsdjeg/flygrep.nvim` | +| `cmds` | `table`, commands lazy loading | +| `events` | `table`, events lazy loading | +| `on_ft` | `table`, filetypes lazy loading | ## Commands diff --git a/bundle/nvim-plug/lua/plug/hooks.lua b/bundle/nvim-plug/lua/plug/hooks.lua index 1fc965409..a96893ef1 100644 --- a/bundle/nvim-plug/lua/plug/hooks.lua +++ b/bundle/nvim-plug/lua/plug/hooks.lua @@ -1,38 +1,56 @@ 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 cmd_plugins = {} +local on_ft_plugins = {} function M.on_events(events, plugSpec) - event_plugins[plugSpec.name] = vim.api.nvim_create_autocmd(events, { - group = group, - pattern = { "*" }, - callback = function(_) - vim.api.nvim_del_autocmd(event_plugins[plugSpec.name]) - plugin_loader.load(plugSpec) - end, - }) + event_plugins[plugSpec.name] = vim.api.nvim_create_autocmd(events, { + group = group, + pattern = { '*' }, + callback = function(_) + 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) + end, + }) end --- @param cmds table --- @param plugSpec PluginSpec function M.on_cmds(cmds, plugSpec) - for _, cmd in ipairs(cmds) do + for _, cmd in ipairs(cmds) do cmd_plugins[cmd] = plugSpec - vim.api.nvim_create_user_command(cmd, function(opt) + 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(...) - return {} - end, - }) - end + nargs = '*', + complete = function(...) + return {} + 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 diff --git a/bundle/nvim-plug/lua/plug/init.lua b/bundle/nvim-plug/lua/plug/init.lua index 340365e0c..26440ce7a 100644 --- a/bundle/nvim-plug/lua/plug/init.lua +++ b/bundle/nvim-plug/lua/plug/init.lua @@ -29,7 +29,11 @@ function M.add(plugins) hooks.on_events(plug.events, plug) 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) end end