Skip to content

Commit 4bbc46c

Browse files
committed
refactor lazy load on filetype
1 parent 8cb77a1 commit 4bbc46c

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

lua/strive/init.lua

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,7 @@ function Plugin:on(events)
785785

786786
-- Create autocmds for each event within this group
787787
for _, event in ipairs(self.events) do
788+
event = event == 'StriveDone' and 'User StriveDone' or event
788789
local pattern
789790
if event:find('%s') then
790791
local t = vim.split(event, '%s')
@@ -806,6 +807,16 @@ function Plugin:on(events)
806807
return self
807808
end
808809

810+
local function _split(s, sep)
811+
local t = {}
812+
for c in vim.gsplit(s, sep, { trimempty = true }) do
813+
if #c > 0 then
814+
table.insert(t, c)
815+
end
816+
end
817+
return t
818+
end
819+
809820
-- Set up lazy loading for specific filetypes
810821
function Plugin:ft(filetypes)
811822
self.is_lazy = true
@@ -817,9 +828,38 @@ function Plugin:ft(filetypes)
817828
group = id,
818829
pattern = self.filetypes,
819830
once = true,
820-
callback = function()
831+
callback = function(args)
821832
if not self.loaded then
822-
self:load()
833+
return self:load(false, function()
834+
local res = api.nvim_exec2('autocmd FileType', { output = true })
835+
if not res.output then
836+
return
837+
end
838+
res = { unpack(vim.split(res.output, '\n'), 1) }
839+
local group_start = nil
840+
for i, item in ipairs(res) do
841+
if item:find('FileType$') then
842+
group_start = i
843+
end
844+
if item:find(self.plugin_name, 1, true) then
845+
local data = _split(item, '%s')
846+
if data[1] == '*' or data[1] == vim.bo[args.buf].filetype then
847+
local au_id = data[3]:match('(%d+):')
848+
if not au_id then
849+
return
850+
end
851+
local g = res[group_start]:match('^(.-)%s+FileType$')
852+
api.nvim_exec_autocmds('FileType', {
853+
group = g,
854+
modeline = false,
855+
buffer = args.buf,
856+
data = args.data,
857+
})
858+
break
859+
end
860+
end
861+
end
862+
end)
823863
end
824864
end,
825865
})
@@ -858,7 +898,7 @@ function Plugin:cmd(commands)
858898
bang = true,
859899
complete = function(_, cmd_line)
860900
if not self.loaded then
861-
self:load()
901+
return self:load()
862902
end
863903
local ok, result = pcall(vim.fn.getcompletion, cmd_line, 'cmdline')
864904
return ok and result or {}
@@ -877,7 +917,7 @@ function Plugin:cond(condition)
877917
(type(condition) == 'string' and api.nvim_eval(condition))
878918
or (type(condition) == 'function' and condition())
879919
then
880-
self:load()
920+
self:load(true)
881921
end
882922
return self
883923
end
@@ -1511,7 +1551,6 @@ function M.clean()
15111551

15121552
-- Scan both start and opt directories
15131553
scan_directory(PACK_DIR)
1514-
scan_directory(OPT_DIR)
15151554

15161555
local strive_plugin = Plugin.new({
15171556
name = 'nvimdev/strive',
@@ -1627,7 +1666,7 @@ local function setup_auto_install()
16271666

16281667
-- UI has not initialized yet, register for UIEnter event
16291668
api.nvim_create_autocmd('UIEnter', {
1630-
group = api.nvim_create_augroup('strive_auto_install', { clear = true }),
1669+
group = api.nvim_create_augroup('strive', { clear = false }),
16311670
callback = function()
16321671
vim.schedule(function()
16331672
M.log('debug', 'UIEnter triggered, installing plugins')
@@ -1644,6 +1683,19 @@ if DEFAULT_SETTINGS.auto_install then
16441683
setup_auto_install()
16451684
end
16461685

1686+
api.nvim_create_autocmd('UIEnter', {
1687+
group = api.nvim_create_augroup('strive', { clear = false }),
1688+
once = true,
1689+
callback = function()
1690+
vim.schedule(function()
1691+
api.nvim_exec_autocmds('User', {
1692+
pattern = 'StriveDone',
1693+
modeline = false,
1694+
})
1695+
end)
1696+
end,
1697+
})
1698+
16471699
local t = { install = 1, update = 2, clean = 3 }
16481700
api.nvim_create_user_command('Strive', function(args)
16491701
if t[args.args] then

0 commit comments

Comments
 (0)