generated from nvimdev/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
local function wrap_async(func) | ||
return function(...) | ||
local args = { ... } | ||
return function(callback) | ||
table.insert(args, callback) | ||
func(unpack(args)) | ||
end | ||
end | ||
end | ||
|
||
local function await(promise) | ||
local co = coroutine.running() | ||
promise(function(...) | ||
local args = { ... } | ||
vim.schedule(function() | ||
assert(coroutine.resume(co, unpack(args))) | ||
end) | ||
end) | ||
return coroutine.yield() | ||
end | ||
|
||
local function async(func) | ||
return function(...) | ||
local co = coroutine.create(func) | ||
local function step(...) | ||
local ok, err = coroutine.resume(co, ...) | ||
if not ok then | ||
error(err) | ||
end | ||
end | ||
step(...) | ||
end | ||
end | ||
|
||
return { | ||
async_fs_fstat = wrap_async(vim.uv.fs_fstat), | ||
async = async, | ||
await = await, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
local api = vim.api | ||
local au = api.nvim_create_autocmd | ||
|
||
local M = {} | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
local TaskQueue = {} | ||
TaskQueue.__index = TaskQueue | ||
|
||
function TaskQueue:new(max_concurrent) | ||
return setmetatable({ | ||
active_tasks = 0, | ||
max_concurrent_tasks = max_concurrent or 2, | ||
task_queue = {}, | ||
}, TaskQueue) | ||
end | ||
|
||
function TaskQueue:process_queue() | ||
while self.active_tasks < self.max_concurrent_tasks and #self.task_queue > 0 do | ||
local task = table.remove(self.task_queue, 1) | ||
self.active_tasks = self.active_tasks + 1 | ||
|
||
task(function() | ||
self.active_tasks = self.active_tasks - 1 | ||
self:process_queue() -- Continue processing the queue | ||
end) | ||
end | ||
end | ||
|
||
--- @class ActionFn function | ||
--- | ||
--- @param fn ActionFn | ||
function TaskQueue:queue_task(fn) | ||
table.insert(self.task_queue, fn) | ||
self:process_queue() | ||
end | ||
|
||
return TaskQueue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
local api = vim.api | ||
|
||
local window = {} | ||
function window:new() | ||
local o = {} | ||
setmetatable(o, self) | ||
self.__index = self | ||
self.content = {} | ||
self.last_row = -1 | ||
return o | ||
end | ||
function window:create_buffer() | ||
self.bufnr = api.nvim_create_buf(false, false) | ||
vim.bo[self.bufnr].buftype = 'nofile' | ||
vim.bo[self.bufnr].bufhidden = 'wipe' | ||
end | ||
|
||
function window:set_mappings() | ||
vim.keymap.set('n', 'q', function() | ||
if self.winid and api.nvim_win_is_valid(self.winid) then | ||
api.nvim_win_close(self.winid, true) | ||
self.bufnr, self.winid = nil, nil | ||
end | ||
end, { buffer = self.bufnr, desc = 'quit window' }) | ||
end | ||
|
||
function window:create_window() | ||
self:create_buffer() | ||
self.winid = api.nvim_open_win(self.bufnr, true, { | ||
relative = 'editor', | ||
height = math.floor(vim.o.lines * 0.5), | ||
width = math.floor(vim.o.columns * 0.8), | ||
row = 3, | ||
col = 10, | ||
border = 'rounded', | ||
noautocmd = true, | ||
style = 'minimal', | ||
}) | ||
vim.wo[self.winid].wrap = false | ||
self:set_mappings() | ||
end | ||
|
||
function window:get_row(repo_name) | ||
if not vim.list_contains(self.content, repo_name) then | ||
self.content[#self.content + 1] = repo_name | ||
return #self.content | ||
end | ||
for k, v in ipairs(self.content) do | ||
if v == repo_name then | ||
return k | ||
end | ||
end | ||
end | ||
|
||
function window:write_output(name, data) | ||
local row = self:get_row(name) - 1 | ||
vim.schedule(function() | ||
if not self.bufnr then | ||
self:create_window() | ||
end | ||
vim.bo[self.bufnr].modifiable = true | ||
api.nvim_buf_set_lines(self.bufnr, row, row + 1, false, { ('%s: %s'):format(name, data) }) | ||
vim.bo[self.bufnr].modifiable = false | ||
end) | ||
end | ||
|
||
return window |