Skip to content

Commit f07011d

Browse files
committed
fix(core): small delay on open
Caused by vim.system():wait() delay when checking opencode version. Fixed by adding Promise.system() which wraps vim.system in a promise (using vim.system's callback mechanism)
1 parent e092046 commit f07011d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lua/opencode/core.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ M.cancel = Promise.async(function()
296296
end
297297
end)
298298

299-
function M.opencode_ok()
299+
M.opencode_ok = Promise.async(function()
300300
if vim.fn.executable('opencode') == 0 then
301301
vim.notify(
302302
'opencode command not found - please install and configure opencode before using this plugin',
@@ -306,7 +306,7 @@ function M.opencode_ok()
306306
end
307307

308308
if not state.opencode_cli_version or state.opencode_cli_version == '' then
309-
local result = vim.system({ 'opencode', '--version' }):wait()
309+
local result = Promise.system({ 'opencode', '--version' }):await()
310310
local out = (result and result.stdout or ''):gsub('%s+$', '')
311311
state.opencode_cli_version = out:match('(%d+%%.%d+%%.%d+)') or out
312312
end
@@ -328,7 +328,7 @@ function M.opencode_ok()
328328
end
329329

330330
return true
331-
end
331+
end)
332332

333333
local function on_opencode_server()
334334
state.current_permission = nil

lua/opencode/promise.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
---@field wrap fun(obj: T | Promise<T>): Promise<T>
2323
---@field spawn fun(fn: fun(): T|nil): Promise<T>
2424
---@field async fun(fn: fun(...): T?): fun(...): Promise<T>
25+
---@field system fun()
2526
local Promise = {}
2627
Promise.__index = Promise
2728

@@ -336,4 +337,23 @@ function Promise.async(fn)
336337
end
337338
end
338339

340+
---Wrap vim.system in a promise
341+
---@generic T
342+
---@param cmd table vim.system cmd options
343+
---@param opts table|nil vim.system opts
344+
---@return Promise<T>
345+
function Promise.system(cmd, opts)
346+
local p = Promise.new()
347+
348+
vim.system(cmd, opts or {}, function(result)
349+
if result.code == 0 then
350+
p:resolve(result)
351+
else
352+
p:reject(result)
353+
end
354+
end)
355+
356+
return p
357+
end
358+
339359
return Promise

0 commit comments

Comments
 (0)