Compare commits
4 commits
perf/async
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b137d64f6 | ||
|
|
fe16245881 | ||
|
|
072859ce04 | ||
|
|
b5b18520d7 |
4 changed files with 262 additions and 39 deletions
|
|
@ -5,24 +5,49 @@ local M = {}
|
||||||
local keys_cache = nil
|
local keys_cache = nil
|
||||||
---@type table<string, string[]>?
|
---@type table<string, string[]>?
|
||||||
local enums_cache = nil
|
local enums_cache = nil
|
||||||
|
local loading = false
|
||||||
|
---@type {ctx: blink.cmp.Context, callback: fun(response: blink.cmp.CompletionResponse)}[]
|
||||||
|
local pending = {}
|
||||||
|
|
||||||
function M.new()
|
function M.new()
|
||||||
return setmetatable({}, { __index = M })
|
return setmetatable({}, { __index = M })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local ghostty_config_dirs = {
|
||||||
|
vim.fn.expand('$XDG_CONFIG_HOME/ghostty'),
|
||||||
|
vim.fn.expand('$HOME/.config/ghostty'),
|
||||||
|
'/etc/ghostty',
|
||||||
|
}
|
||||||
|
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.enabled()
|
function M.enabled()
|
||||||
return vim.bo.filetype == 'ghostty'
|
if vim.bo.filetype == 'ghostty' then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
if vim.bo.filetype ~= 'config' and vim.bo.filetype ~= '' then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local path = vim.api.nvim_buf_get_name(0)
|
||||||
|
if path == '' then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
local real = vim.uv.fs_realpath(path) or path
|
||||||
|
for _, dir in ipairs(ghostty_config_dirs) do
|
||||||
|
if real:find(dir, 1, true) == 1 then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param stdout string
|
||||||
---@return blink.cmp.CompletionItem[]
|
---@return blink.cmp.CompletionItem[]
|
||||||
local function parse_keys()
|
local function parse_keys(stdout)
|
||||||
local Kind = require('blink.cmp.types').CompletionItemKind
|
local Kind = require('blink.cmp.types').CompletionItemKind
|
||||||
local result = vim.system({ 'ghostty', '+show-config', '--docs' }):wait()
|
|
||||||
local items = {}
|
local items = {}
|
||||||
local doc_lines = {}
|
local doc_lines = {}
|
||||||
|
|
||||||
for line in ((result.stdout or '') .. '\n'):gmatch('(.-)\n') do
|
for line in (stdout .. '\n'):gmatch('(.-)\n') do
|
||||||
if line:match('^#') then
|
if line:match('^#') then
|
||||||
local stripped = line:gsub('^# ?', '')
|
local stripped = line:gsub('^# ?', '')
|
||||||
doc_lines[#doc_lines + 1] = stripped
|
doc_lines[#doc_lines + 1] = stripped
|
||||||
|
|
@ -42,28 +67,25 @@ local function parse_keys()
|
||||||
return items
|
return items
|
||||||
end
|
end
|
||||||
|
|
||||||
---@return table<string, string[]>
|
---@return string?
|
||||||
local function parse_enums()
|
function M.bash_completion_path()
|
||||||
local bin = vim.fn.exepath('ghostty')
|
local bin = vim.fn.exepath('ghostty')
|
||||||
if bin == '' then
|
if bin == '' then
|
||||||
return {}
|
return nil
|
||||||
end
|
end
|
||||||
local real = vim.uv.fs_realpath(bin)
|
local real = vim.uv.fs_realpath(bin)
|
||||||
if not real then
|
if not real then
|
||||||
return {}
|
return nil
|
||||||
end
|
end
|
||||||
local prefix = real:match('(.*)/bin/ghostty$')
|
local prefix = real:match('(.*)/bin/ghostty$')
|
||||||
if not prefix then
|
if not prefix then
|
||||||
return {}
|
return nil
|
||||||
end
|
end
|
||||||
local path = prefix .. '/share/bash-completion/completions/ghostty.bash'
|
return prefix .. '/share/bash-completion/completions/ghostty.bash'
|
||||||
local fd = io.open(path, 'r')
|
end
|
||||||
if not fd then
|
|
||||||
return {}
|
|
||||||
end
|
|
||||||
local content = fd:read('*a')
|
|
||||||
fd:close()
|
|
||||||
|
|
||||||
|
---@return table<string, string[]>
|
||||||
|
local function parse_enums(content)
|
||||||
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 = {}
|
||||||
|
|
@ -79,13 +101,10 @@ end
|
||||||
|
|
||||||
---@param ctx blink.cmp.Context
|
---@param ctx blink.cmp.Context
|
||||||
---@param callback fun(response: blink.cmp.CompletionResponse)
|
---@param callback fun(response: blink.cmp.CompletionResponse)
|
||||||
---@return fun()
|
local function respond(ctx, callback)
|
||||||
function M:get_completions(ctx, callback)
|
if not keys_cache or not enums_cache then
|
||||||
if not keys_cache then
|
return
|
||||||
keys_cache = parse_keys()
|
|
||||||
enums_cache = parse_enums()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local line = ctx.line
|
local line = ctx.line
|
||||||
local col = ctx.cursor[2]
|
local col = ctx.cursor[2]
|
||||||
local eq_pos = line:find('=')
|
local eq_pos = line:find('=')
|
||||||
|
|
@ -108,16 +127,81 @@ function M:get_completions(ctx, callback)
|
||||||
is_incomplete_backward = false,
|
is_incomplete_backward = false,
|
||||||
items = items,
|
items = items,
|
||||||
})
|
})
|
||||||
return function() end
|
return
|
||||||
end
|
end
|
||||||
callback({ items = {} })
|
callback({ items = {} })
|
||||||
else
|
else
|
||||||
callback({
|
callback({
|
||||||
is_incomplete_forward = false,
|
is_incomplete_forward = false,
|
||||||
is_incomplete_backward = false,
|
is_incomplete_backward = false,
|
||||||
items = vim.deepcopy(keys_cache),
|
items = keys_cache,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param ctx blink.cmp.Context
|
||||||
|
---@param callback fun(response: blink.cmp.CompletionResponse)
|
||||||
|
---@return fun()
|
||||||
|
function M:get_completions(ctx, callback)
|
||||||
|
if keys_cache then
|
||||||
|
respond(ctx, callback)
|
||||||
|
return function() end
|
||||||
|
end
|
||||||
|
|
||||||
|
pending[#pending + 1] = { ctx = ctx, callback = callback }
|
||||||
|
if not loading then
|
||||||
|
loading = true
|
||||||
|
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()
|
||||||
|
keys_cache = parse_keys(config_out)
|
||||||
|
enums_cache = parse_enums(enums_content)
|
||||||
|
loading = false
|
||||||
|
for _, p in ipairs(pending) do
|
||||||
|
respond(p.ctx, p.callback)
|
||||||
|
end
|
||||||
|
pending = {}
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
vim.system({ 'ghostty', '+show-config', '--docs' }, {}, function(result)
|
||||||
|
config_out = result.stdout or ''
|
||||||
|
on_all_done()
|
||||||
|
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
|
||||||
return function() end
|
return function() end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
47
lua/blink-cmp-ghostty/health.lua
Normal file
47
lua/blink-cmp-ghostty/health.lua
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
local M = {}
|
||||||
|
|
||||||
|
function M.check()
|
||||||
|
vim.health.start('blink-cmp-ghostty')
|
||||||
|
|
||||||
|
local ok = pcall(require, 'blink.cmp')
|
||||||
|
if ok then
|
||||||
|
vim.health.ok('blink.cmp is installed')
|
||||||
|
else
|
||||||
|
vim.health.error('blink.cmp is not installed')
|
||||||
|
end
|
||||||
|
|
||||||
|
local bin = vim.fn.exepath('ghostty')
|
||||||
|
if bin ~= '' then
|
||||||
|
vim.health.ok('ghostty executable found: ' .. bin)
|
||||||
|
else
|
||||||
|
vim.health.error('ghostty executable not found')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local result = vim.system({ 'ghostty', '+show-config', '--docs' }):wait()
|
||||||
|
if result.code == 0 and result.stdout and result.stdout ~= '' then
|
||||||
|
vim.health.ok('ghostty +show-config --docs produces output')
|
||||||
|
else
|
||||||
|
vim.health.warn(
|
||||||
|
'ghostty +show-config --docs failed (config key documentation will be unavailable)'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
local source = require('blink-cmp-ghostty')
|
||||||
|
local path = source.bash_completion_path()
|
||||||
|
if not path then
|
||||||
|
vim.health.warn('could not resolve bash completion path (enum completions will be unavailable)')
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local fd = io.open(path, 'r')
|
||||||
|
if fd then
|
||||||
|
fd:close()
|
||||||
|
vim.health.ok('bash completion file found: ' .. path)
|
||||||
|
else
|
||||||
|
vim.health.warn(
|
||||||
|
'bash completion file not found at ' .. path .. ' (enum completions will be unavailable)'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return M
|
||||||
16
lua/blink-cmp-ghostty/types.lua
Normal file
16
lua/blink-cmp-ghostty/types.lua
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
---@class blink.cmp.Source
|
||||||
|
|
||||||
|
---@class blink.cmp.CompletionItem
|
||||||
|
---@field label string
|
||||||
|
---@field kind? integer
|
||||||
|
---@field documentation? {kind: string, value: string}
|
||||||
|
---@field filterText? string
|
||||||
|
|
||||||
|
---@class blink.cmp.Context
|
||||||
|
---@field line string
|
||||||
|
---@field cursor integer[]
|
||||||
|
|
||||||
|
---@class blink.cmp.CompletionResponse
|
||||||
|
---@field is_incomplete_forward? boolean
|
||||||
|
---@field is_incomplete_backward? boolean
|
||||||
|
---@field items blink.cmp.CompletionItem[]
|
||||||
|
|
@ -19,31 +19,51 @@ local BASH_COMPLETION = table.concat({
|
||||||
}, '\n')
|
}, '\n')
|
||||||
|
|
||||||
local function mock_system()
|
local function mock_system()
|
||||||
local original = vim.system
|
local original_system = vim.system
|
||||||
|
local original_schedule = vim.schedule
|
||||||
---@diagnostic disable-next-line: duplicate-set-field
|
---@diagnostic disable-next-line: duplicate-set-field
|
||||||
vim.system = function(cmd)
|
vim.system = function(cmd, _, on_exit)
|
||||||
if cmd[1] == 'ghostty' then
|
if cmd[1] == 'ghostty' then
|
||||||
return {
|
local result = { stdout = CONFIG_DOCS, code = 0 }
|
||||||
wait = function()
|
if on_exit then
|
||||||
return { stdout = CONFIG_DOCS, code = 0 }
|
on_exit(result)
|
||||||
end,
|
return {}
|
||||||
}
|
|
||||||
end
|
end
|
||||||
return {
|
return {
|
||||||
wait = function()
|
wait = function()
|
||||||
return { stdout = '', code = 1 }
|
return result
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
local result = { stdout = '', code = 1 }
|
||||||
|
if on_exit then
|
||||||
|
on_exit(result)
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
return {
|
||||||
|
wait = function()
|
||||||
|
return result
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
vim.schedule = function(fn)
|
||||||
|
fn()
|
||||||
|
end
|
||||||
return function()
|
return function()
|
||||||
vim.system = original
|
vim.system = original_system
|
||||||
|
vim.schedule = original_schedule
|
||||||
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
|
||||||
|
|
@ -57,22 +77,41 @@ local function mock_enums()
|
||||||
end
|
end
|
||||||
return original_realpath(path)
|
return original_realpath(path)
|
||||||
end
|
end
|
||||||
io.open = function(path, mode)
|
vim.uv.fs_open = function(path, flags, mode, callback)
|
||||||
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
|
||||||
io.open = original_open
|
vim.uv.fs_open = original_fs_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
|
||||||
|
|
||||||
|
|
@ -98,6 +137,43 @@ describe('blink-cmp-ghostty', function()
|
||||||
helpers.delete_buffer(bufnr)
|
helpers.delete_buffer(bufnr)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('returns true for config filetype in ghostty config dir', function()
|
||||||
|
local source = require('blink-cmp-ghostty')
|
||||||
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.api.nvim_set_current_buf(bufnr)
|
||||||
|
vim.api.nvim_set_option_value('filetype', 'config', { buf = bufnr })
|
||||||
|
local config_path = vim.fn.expand('$HOME/.config/ghostty/config')
|
||||||
|
vim.api.nvim_buf_set_name(bufnr, config_path)
|
||||||
|
local original_realpath = vim.uv.fs_realpath
|
||||||
|
vim.uv.fs_realpath = function(p)
|
||||||
|
if p == config_path then
|
||||||
|
return config_path
|
||||||
|
end
|
||||||
|
return original_realpath(p)
|
||||||
|
end
|
||||||
|
assert.is_true(source.enabled())
|
||||||
|
vim.uv.fs_realpath = original_realpath
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('returns false for config filetype outside ghostty dir', function()
|
||||||
|
local source = require('blink-cmp-ghostty')
|
||||||
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
||||||
|
vim.api.nvim_set_current_buf(bufnr)
|
||||||
|
vim.api.nvim_set_option_value('filetype', 'config', { buf = bufnr })
|
||||||
|
vim.api.nvim_buf_set_name(bufnr, '/tmp/some-other/config')
|
||||||
|
local original_realpath = vim.uv.fs_realpath
|
||||||
|
vim.uv.fs_realpath = function(p)
|
||||||
|
if p == '/tmp/some-other/config' then
|
||||||
|
return '/tmp/some-other/config'
|
||||||
|
end
|
||||||
|
return original_realpath(p)
|
||||||
|
end
|
||||||
|
assert.is_false(source.enabled())
|
||||||
|
vim.uv.fs_realpath = original_realpath
|
||||||
|
helpers.delete_buffer(bufnr)
|
||||||
|
end)
|
||||||
|
|
||||||
it('returns false for other filetypes', function()
|
it('returns false for other filetypes', function()
|
||||||
local bufnr = helpers.create_buffer({}, 'lua')
|
local bufnr = helpers.create_buffer({}, 'lua')
|
||||||
local source = require('blink-cmp-ghostty')
|
local source = require('blink-cmp-ghostty')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue