Skip to content

Commit 8cb77a1

Browse files
committed
rewrite
1 parent e8962be commit 8cb77a1

File tree

1 file changed

+34
-67
lines changed

1 file changed

+34
-67
lines changed

lua/strive/init.lua

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ local plugin_map = {}
1313

1414
-- Data paths
1515
local data_dir = vim.fn.stdpath('data')
16-
local START_DIR = joinpath(data_dir, 'site', 'pack', 'strive', 'start')
17-
local OPT_DIR = joinpath(data_dir, 'site', 'pack', 'strive', 'opt')
16+
local PACK_DIR = joinpath(data_dir, 'strive')
1817

1918
-- Add to packpath
2019
vim.opt.packpath:prepend(joinpath(data_dir, 'site'))
@@ -629,8 +628,7 @@ end
629628

630629
-- Get the plugin installation path
631630
function Plugin:get_path()
632-
return (not self.is_local and not self.local_path)
633-
and joinpath(self.is_lazy and OPT_DIR or START_DIR, self.plugin_name)
631+
return (not self.is_local and not self.local_path) and joinpath(PACK_DIR, self.plugin_name)
634632
or (joinpath(self.local_path, self.plugin_name) or self.name)
635633
end
636634

@@ -692,7 +690,7 @@ function Plugin:load_scripts()
692690
end
693691

694692
-- Load a plugin and its dependencies
695-
function Plugin:load()
693+
function Plugin:load(do_action, callback)
696694
if self.loaded then
697695
return true
698696
end
@@ -709,24 +707,20 @@ function Plugin:load()
709707
load_opts(self.init_opts)
710708
end
711709

712-
if self.is_local then
713-
vim.opt.rtp:append(plugin_path)
710+
vim.opt.rtp:append(plugin_path)
714711

715-
local after_path = joinpath(plugin_path, 'after')
716-
if isdir(after_path) then
717-
vim.opt.rtp:append(after_path)
718-
end
712+
local after_path = joinpath(plugin_path, 'after')
713+
if isdir(after_path) then
714+
vim.opt.rtp:append(after_path)
715+
end
719716

720-
local result = Async.try_await(self:load_scripts())
721-
if result.error then
722-
M.log(
723-
'error',
724-
string.format('Failed to load scripts for %s: %s', self.name, tostring(result.error))
725-
)
726-
return
727-
end
728-
elseif self.is_lazy then
729-
vim.cmd.packadd(self.plugin_name)
717+
local result = Async.try_await(self:load_scripts())
718+
if result.error then
719+
M.log(
720+
'error',
721+
string.format('Failed to load scripts for %s: %s', self.name, tostring(result.error))
722+
)
723+
return
730724
end
731725

732726
self.loaded = true
@@ -767,6 +761,14 @@ function Plugin:load()
767761
load_opts(self.config_opts)
768762
end
769763

764+
if do_action and self.run_action then
765+
vim.cmd(self.run_action)
766+
end
767+
768+
if callback then
769+
callback()
770+
end
771+
770772
return true
771773
end)()
772774

@@ -815,34 +817,9 @@ function Plugin:ft(filetypes)
815817
group = id,
816818
pattern = self.filetypes,
817819
once = true,
818-
callback = function(args)
819-
if not self.loaded and self:load() then
820-
local augroup_name = nil
821-
if self.plugin_name:find('lspconfig', 1, true) then
822-
augroup_name = 'nvim.lsp.enable'
823-
else
824-
local res = api.nvim_exec2('autocmd FileType', { output = true })
825-
if not res.output then
826-
return
827-
end
828-
res = { unpack(vim.split(res.output, '\n'), 1) }
829-
for i, item in ipairs(res) do
830-
if item:find(self.plugin_name, 1, true) then
831-
augroup_name = res[i - 1]:match('^%s*(%S+)')
832-
end
833-
end
834-
end
835-
836-
if augroup_name then
837-
vim.schedule(function()
838-
api.nvim_exec_autocmds('FileType', {
839-
group = augroup_name,
840-
buffer = args.buf,
841-
data = args.data,
842-
modeline = false,
843-
})
844-
end)
845-
end
820+
callback = function()
821+
if not self.loaded then
822+
self:load()
846823
end
847824
end,
848825
})
@@ -873,17 +850,9 @@ function Plugin:cmd(commands)
873850
-- Remove this command to avoid recursion
874851
pcall(api.nvim_del_user_command, name)
875852
local args = opts.args ~= '' and (' ' .. opts.args) or ''
876-
local bang = opts.bang
877-
878-
Async.async(function()
879-
self:load()
880-
if self.is_local then
881-
Async.await(Async.delay(5))
882-
end
883-
Async.safe_schedule(function()
884-
execute(name, bang, args)
885-
end)
886-
end)()
853+
self:load(false, function()
854+
execute(name, opts.bang, args)
855+
end)
887856
end, {
888857
nargs = '*',
889858
bang = true,
@@ -988,7 +957,7 @@ function Plugin:theme(name)
988957
local installed = Async.await(self:is_installed())
989958
if installed then
990959
vim.schedule(function()
991-
vim.opt.rtp:append(joinpath(START_DIR, self.plugin_name))
960+
vim.opt.rtp:append(joinpath(PACK_DIR, self.plugin_name))
992961
vim.cmd.colorscheme(self.colorscheme)
993962
end)
994963
end
@@ -1087,8 +1056,7 @@ function Plugin:install()
10871056

10881057
-- Run build command if specified
10891058
if self.run_action then
1090-
self:load()
1091-
vim.cmd(self.run_action)
1059+
self:load(true)
10921060
end
10931061
else
10941062
self.status = STATUS.ERROR
@@ -1305,8 +1273,7 @@ function Plugin:install_with_retry()
13051273

13061274
-- Run command if specified
13071275
if self.run_action then
1308-
self:load()
1309-
vim.cmd(self.run_action)
1276+
self:load(true)
13101277
end
13111278
else
13121279
self.status = STATUS.ERROR
@@ -1543,7 +1510,7 @@ function M.clean()
15431510
end
15441511

15451512
-- Scan both start and opt directories
1546-
scan_directory(START_DIR)
1513+
scan_directory(PACK_DIR)
15471514
scan_directory(OPT_DIR)
15481515

15491516
local strive_plugin = Plugin.new({
@@ -1668,7 +1635,7 @@ local function setup_auto_install()
16681635
end)
16691636
startuptime()
16701637
end,
1671-
once = true, -- Only trigger once to avoid recursion
1638+
once = true,
16721639
})
16731640
end
16741641

0 commit comments

Comments
 (0)