Compare commits
2 commits
main
...
perf/async
| Author | SHA1 | Date | |
|---|---|---|---|
| 3776133815 | |||
| 71d5505fb8 |
2 changed files with 77 additions and 27 deletions
|
|
@ -85,18 +85,7 @@ function M.bash_completion_path()
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return table<string, string[]>
|
---@return table<string, string[]>
|
||||||
local function parse_enums()
|
local function parse_enums(content)
|
||||||
local path = M.bash_completion_path()
|
|
||||||
if not path then
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
local fd = io.open(path, 'r')
|
|
||||||
if not fd then
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
local content = fd:read('*a')
|
|
||||||
fd:close()
|
|
||||||
|
|
||||||
local enums = {}
|
local enums = {}
|
||||||
for key, values in content:gmatch('%-%-([a-z][a-z0-9-]*)%) [^\n]* compgen %-W "([^"]+)"') do
|
for key, values in content:gmatch('%-%-([a-z][a-z0-9-]*)%) [^\n]* compgen %-W "([^"]+)"') do
|
||||||
local vals = {}
|
local vals = {}
|
||||||
|
|
@ -162,17 +151,56 @@ function M:get_completions(ctx, callback)
|
||||||
pending[#pending + 1] = { ctx = ctx, callback = callback }
|
pending[#pending + 1] = { ctx = ctx, callback = callback }
|
||||||
if not loading then
|
if not loading then
|
||||||
loading = true
|
loading = true
|
||||||
vim.system({ 'ghostty', '+show-config', '--docs' }, {}, function(result)
|
local config_out, enums_content
|
||||||
|
local remaining = 2
|
||||||
|
|
||||||
|
local function on_all_done()
|
||||||
|
remaining = remaining - 1
|
||||||
|
if remaining > 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
keys_cache = parse_keys(result.stdout or '')
|
keys_cache = parse_keys(config_out)
|
||||||
enums_cache = parse_enums()
|
enums_cache = parse_enums(enums_content)
|
||||||
loading = false
|
loading = false
|
||||||
for _, p in ipairs(pending) do
|
for _, p in ipairs(pending) do
|
||||||
respond(p.ctx, p.callback)
|
respond(p.ctx, p.callback)
|
||||||
end
|
end
|
||||||
pending = {}
|
pending = {}
|
||||||
end)
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.system({ 'ghostty', '+show-config', '--docs' }, {}, function(result)
|
||||||
|
config_out = result.stdout or ''
|
||||||
|
on_all_done()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
local path = M.bash_completion_path()
|
||||||
|
if not path then
|
||||||
|
enums_content = ''
|
||||||
|
on_all_done()
|
||||||
|
else
|
||||||
|
vim.uv.fs_open(path, 'r', 438, function(err, fd)
|
||||||
|
if err or not fd then
|
||||||
|
enums_content = ''
|
||||||
|
on_all_done()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.uv.fs_fstat(fd, function(err2, stat)
|
||||||
|
if err2 or not stat then
|
||||||
|
vim.uv.fs_close(fd)
|
||||||
|
enums_content = ''
|
||||||
|
on_all_done()
|
||||||
|
return
|
||||||
|
end
|
||||||
|
vim.uv.fs_read(fd, stat.size, 0, function(_, data)
|
||||||
|
vim.uv.fs_close(fd)
|
||||||
|
enums_content = data or ''
|
||||||
|
on_all_done()
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return function() end
|
return function() end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -55,10 +55,15 @@ local function mock_system()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local MOCK_FD = 99
|
||||||
|
|
||||||
local function mock_enums()
|
local function mock_enums()
|
||||||
local original_exepath = vim.fn.exepath
|
local original_exepath = vim.fn.exepath
|
||||||
local original_realpath = vim.uv.fs_realpath
|
local original_realpath = vim.uv.fs_realpath
|
||||||
local original_open = io.open
|
local original_fs_open = vim.uv.fs_open
|
||||||
|
local original_fs_fstat = vim.uv.fs_fstat
|
||||||
|
local original_fs_read = vim.uv.fs_read
|
||||||
|
local original_fs_close = vim.uv.fs_close
|
||||||
|
|
||||||
vim.fn.exepath = function(name)
|
vim.fn.exepath = function(name)
|
||||||
if name == 'ghostty' then
|
if name == 'ghostty' then
|
||||||
|
|
@ -72,24 +77,41 @@ local function mock_enums()
|
||||||
end
|
end
|
||||||
return original_realpath(path)
|
return original_realpath(path)
|
||||||
end
|
end
|
||||||
-- selene: allow(incorrect_standard_library_use)
|
vim.uv.fs_open = function(path, flags, mode, callback)
|
||||||
io.open = function(path, mode)
|
|
||||||
if path:match('ghostty%.bash$') then
|
if path:match('ghostty%.bash$') then
|
||||||
return {
|
callback(nil, MOCK_FD)
|
||||||
read = function()
|
return
|
||||||
return BASH_COMPLETION
|
|
||||||
end,
|
|
||||||
close = function() end,
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
return original_open(path, mode)
|
return original_fs_open(path, flags, mode, callback)
|
||||||
|
end
|
||||||
|
vim.uv.fs_fstat = function(fd, callback)
|
||||||
|
if fd == MOCK_FD then
|
||||||
|
callback(nil, { size = #BASH_COMPLETION })
|
||||||
|
return
|
||||||
|
end
|
||||||
|
return original_fs_fstat(fd, callback)
|
||||||
|
end
|
||||||
|
vim.uv.fs_read = function(fd, size, offset, callback)
|
||||||
|
if fd == MOCK_FD then
|
||||||
|
callback(nil, BASH_COMPLETION)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
return original_fs_read(fd, size, offset, callback)
|
||||||
|
end
|
||||||
|
vim.uv.fs_close = function(fd, ...)
|
||||||
|
if fd == MOCK_FD then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
return original_fs_close(fd, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
vim.fn.exepath = original_exepath
|
vim.fn.exepath = original_exepath
|
||||||
vim.uv.fs_realpath = original_realpath
|
vim.uv.fs_realpath = original_realpath
|
||||||
-- selene: allow(incorrect_standard_library_use)
|
vim.uv.fs_open = original_fs_open
|
||||||
io.open = original_open
|
vim.uv.fs_fstat = original_fs_fstat
|
||||||
|
vim.uv.fs_read = original_fs_read
|
||||||
|
vim.uv.fs_close = original_fs_close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue