Skip to content

Commit 294adec

Browse files
committed
udpate
1 parent 4ed7575 commit 294adec

File tree

5 files changed

+190
-100
lines changed

5 files changed

+190
-100
lines changed

Diff for: lua/minpm/async.lua

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
local function wrap_async(func)
2+
return function(...)
3+
local args = { ... }
4+
return function(callback)
5+
table.insert(args, callback)
6+
func(unpack(args))
7+
end
8+
end
9+
end
10+
11+
local function await(promise)
12+
local co = coroutine.running()
13+
promise(function(...)
14+
local args = { ... }
15+
vim.schedule(function()
16+
assert(coroutine.resume(co, unpack(args)))
17+
end)
18+
end)
19+
return coroutine.yield()
20+
end
21+
22+
local function async(func)
23+
return function(...)
24+
local co = coroutine.create(func)
25+
local function step(...)
26+
local ok, err = coroutine.resume(co, ...)
27+
if not ok then
28+
error(err)
29+
end
30+
end
31+
step(...)
32+
end
33+
end
34+
35+
return {
36+
async_fs_fstat = wrap_async(vim.uv.fs_fstat),
37+
async = async,
38+
await = await,
39+
}

Diff for: lua/minpm/event.lua

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local api = vim.api
2+
local au = api.nvim_create_autocmd
3+
4+
local M = {}
5+
6+
return M

Diff for: lua/minpm/init.lua

+46-100
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
local api, stdpath, uv = vim.api, vim.fn.stdpath, vim.uv
1+
local api, stdpath, uv, if_nil = vim.api, vim.fn.stdpath, vim.uv, vim.F.if_nil
22
local repos, INSTALL = {}, 0
3-
local buf_set_lines, create_autocmd = api.nvim_buf_set_lines, api.nvim_create_autocmd
3+
local create_autocmd = api.nvim_create_autocmd
44
local packadd = vim.cmd.packadd
55
local exec_autocmds = api.nvim_exec_autocmds
66
local data_dir = stdpath('data')
@@ -10,7 +10,7 @@ local STARTDIR = vim.fs.joinpath(data_dir, 'site', 'pack', 'minpm', 'start')
1010
local OPTDIR = vim.fs.joinpath(data_dir, 'site', 'pack', 'minpm', 'opt')
1111
---@diagnostic disable-next-line: param-type-mismatch
1212
vim.opt.packpath:prepend(vim.fs.joinpath(data_dir, 'site'))
13-
local if_nil = vim.F.if_nil
13+
local window, TaskQueue = require('minpm.win'), require('minpm.task')
1414

1515
local function as_table(data)
1616
return type(data) ~= 'table' and { data } or data
@@ -19,132 +19,77 @@ end
1919
local use_meta = {}
2020
use_meta.__index = use_meta
2121

22-
function use_meta:when(e)
23-
self.event = as_table(e)
24-
self.islazy = true
25-
local id
26-
id = create_autocmd(e, {
22+
function use_meta:handle_event()
23+
self.auid = create_autocmd(self.event, {
24+
pattern = self.ft or nil,
2725
callback = function(args)
28-
if self.remote then
29-
api.nvim_del_autocmd(id)
30-
packadd(self.tail)
31-
exec_autocmds(e, {
32-
modeline = false,
33-
data = args.data,
34-
})
35-
if self.setup_config then
36-
local module = self.tail:gsub('%.nvim', ''):gsub('-nvim', '')
37-
require(module).setup(self.setup_config)
38-
end
26+
if not self.remote then
27+
return
28+
end
29+
30+
api.nvim_del_autocmd(self.auid)
31+
packadd(self.tail)
32+
local module = self.tail:gsub('%.nvim$', ''):gsub('-nvim$', ''):gsub('^nvim%-', '')
33+
local m_setup = vim.tbl_get(require(module), 'setup')
34+
if type(m_setup) == 'function' then
35+
m_setup(self.setup_config)
36+
end
37+
38+
if self.after_config then
39+
self.after_config()
3940
end
41+
42+
exec_autocmds(self.event, {
43+
modeline = false,
44+
data = args.data,
45+
})
4046
end,
4147
})
48+
end
49+
50+
function use_meta:when(e)
51+
self.event = as_table(e)
52+
self.islazy = true
53+
self:handle_event()
4254
return self
4355
end
4456

4557
function use_meta:lang(ft)
4658
self.ft = as_table(ft)
59+
self.event = 'FileType'
60+
self:handle_event()
4761
self.islazy = true
4862
return self
4963
end
5064

51-
function use_meta:setup(config)
52-
self.setup_config = config
65+
function use_meta:dev()
66+
self.isdev = true
67+
self.remote = false
5368
return self
5469
end
5570

56-
function use_meta:config(config)
57-
assert(type(config) == 'function')
58-
self.config = config
71+
function use_meta:setup(setup_config)
72+
self.setup_config = setup_config
5973
return self
6074
end
6175

62-
local window = {}
63-
function window:new()
64-
local o = {}
65-
setmetatable(o, self)
66-
self.__index = self
67-
self.content = {}
68-
self.last_row = -1
69-
return o
70-
end
71-
72-
function window:create_window()
73-
self.bufnr = api.nvim_create_buf(false, false)
74-
self.winid = api.nvim_open_win(self.bufnr, true, {
75-
relative = 'editor',
76-
height = math.floor(vim.o.lines * 0.5),
77-
width = math.floor(vim.o.columns * 0.8),
78-
row = 3,
79-
col = 10,
80-
border = 'rounded',
81-
noautocmd = true,
82-
style = 'minimal',
83-
})
84-
vim.wo[self.winid].wrap = false
85-
vim.bo[self.bufnr].buftype = 'nofile'
86-
vim.bo[self.bufnr].bufhidden = 'wipe'
87-
vim.keymap.set('n', 'q', function()
88-
if self.winid and api.nvim_win_is_valid(self.winid) then
89-
api.nvim_win_close(self.winid, true)
90-
self.bufnr, self.winid = nil, nil
91-
end
92-
end, { buffer = self.bufnr, desc = 'quit window' })
93-
end
94-
95-
function window:get_row(repo_name)
96-
if not vim.list_contains(self.content, repo_name) then
97-
self.content[#self.content + 1] = repo_name
98-
return #self.content
99-
end
100-
for k, v in ipairs(self.content) do
101-
if v == repo_name then
102-
return k
103-
end
104-
end
105-
end
106-
107-
function window:write_output(name, data)
108-
local row = self:get_row(name) - 1
109-
vim.schedule(function()
110-
if not self.bufnr then
111-
self:create_window()
112-
end
113-
vim.bo[self.bufnr].modifiable = true
114-
buf_set_lines(self.bufnr, row, row + 1, false, { ('%s: %s'):format(name, data) })
115-
vim.bo[self.bufnr].modifiable = false
116-
end)
76+
function use_meta:config(config)
77+
assert(type(config) == 'function')
78+
self.after_config = config
79+
return self
11780
end
11881

11982
local MAX_CONCURRENT_TASKS = if_nil(vim.g.minpm_max_concurrent_tasks, 2)
120-
local active_tasks = 0
121-
local task_queue = {}
122-
123-
local function process_queue()
124-
while active_tasks < MAX_CONCURRENT_TASKS and #task_queue > 0 do
125-
local task = table.remove(task_queue, 1)
126-
active_tasks = active_tasks + 1
127-
task(function()
128-
active_tasks = active_tasks - 1
129-
process_queue()
130-
end)
131-
end
132-
end
133-
134-
local function queue_task(fn)
135-
table.insert(task_queue, fn)
136-
process_queue()
137-
end
83+
local tsq = TaskQueue:new(MAX_CONCURRENT_TASKS)
13884

13985
function use_meta:do_action(action, winobj)
140-
queue_task(function(task_done)
86+
tsq:queue_task(function(task_done)
14187
local path = vim.fs.joinpath(self.islazy and OPTDIR or STARTDIR, self.tail)
14288
local url = ('https://github.com/%s'):format(self.name)
14389
local cmd = action == INSTALL and { 'git', 'clone', '--progress', url, path }
14490
or { 'git', '-C', path, 'pull', '--progress' }
14591
uv.fs_stat(path, function(_, stat)
14692
if stat and stat.type == 'directory' then
147-
winobj:write_output(self.name, 'Directory already exists skipped')
14893
task_done()
14994
return
15095
end
@@ -173,10 +118,11 @@ function use_meta:do_action(action, winobj)
173118
end
174119

175120
local function action_wrapper(act)
121+
act = act or INSTALL
176122
return function()
177123
local winobj = window:new()
178124
vim.iter(repos):map(function(repo)
179-
if not repo.remote then
125+
if not repo.remote or repo.isdev then
180126
return
181127
end
182128
repo:do_action(act, winobj)

Diff for: lua/minpm/task.lua

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local TaskQueue = {}
2+
TaskQueue.__index = TaskQueue
3+
4+
function TaskQueue:new(max_concurrent)
5+
return setmetatable({
6+
active_tasks = 0,
7+
max_concurrent_tasks = max_concurrent or 2,
8+
task_queue = {},
9+
}, TaskQueue)
10+
end
11+
12+
function TaskQueue:process_queue()
13+
while self.active_tasks < self.max_concurrent_tasks and #self.task_queue > 0 do
14+
local task = table.remove(self.task_queue, 1)
15+
self.active_tasks = self.active_tasks + 1
16+
17+
task(function()
18+
self.active_tasks = self.active_tasks - 1
19+
self:process_queue() -- Continue processing the queue
20+
end)
21+
end
22+
end
23+
24+
--- @class ActionFn function
25+
---
26+
--- @param fn ActionFn
27+
function TaskQueue:queue_task(fn)
28+
table.insert(self.task_queue, fn)
29+
self:process_queue()
30+
end
31+
32+
return TaskQueue

Diff for: lua/minpm/win.lua

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
local api = vim.api
2+
3+
local window = {}
4+
function window:new()
5+
local o = {}
6+
setmetatable(o, self)
7+
self.__index = self
8+
self.content = {}
9+
self.last_row = -1
10+
return o
11+
end
12+
function window:create_buffer()
13+
self.bufnr = api.nvim_create_buf(false, false)
14+
vim.bo[self.bufnr].buftype = 'nofile'
15+
vim.bo[self.bufnr].bufhidden = 'wipe'
16+
end
17+
18+
function window:set_mappings()
19+
vim.keymap.set('n', 'q', function()
20+
if self.winid and api.nvim_win_is_valid(self.winid) then
21+
api.nvim_win_close(self.winid, true)
22+
self.bufnr, self.winid = nil, nil
23+
end
24+
end, { buffer = self.bufnr, desc = 'quit window' })
25+
end
26+
27+
function window:create_window()
28+
self:create_buffer()
29+
self.winid = api.nvim_open_win(self.bufnr, true, {
30+
relative = 'editor',
31+
height = math.floor(vim.o.lines * 0.5),
32+
width = math.floor(vim.o.columns * 0.8),
33+
row = 3,
34+
col = 10,
35+
border = 'rounded',
36+
noautocmd = true,
37+
style = 'minimal',
38+
})
39+
vim.wo[self.winid].wrap = false
40+
self:set_mappings()
41+
end
42+
43+
function window:get_row(repo_name)
44+
if not vim.list_contains(self.content, repo_name) then
45+
self.content[#self.content + 1] = repo_name
46+
return #self.content
47+
end
48+
for k, v in ipairs(self.content) do
49+
if v == repo_name then
50+
return k
51+
end
52+
end
53+
end
54+
55+
function window:write_output(name, data)
56+
local row = self:get_row(name) - 1
57+
vim.schedule(function()
58+
if not self.bufnr then
59+
self:create_window()
60+
end
61+
vim.bo[self.bufnr].modifiable = true
62+
api.nvim_buf_set_lines(self.bufnr, row, row + 1, false, { ('%s: %s'):format(name, data) })
63+
vim.bo[self.bufnr].modifiable = false
64+
end)
65+
end
66+
67+
return window

0 commit comments

Comments
 (0)