fix: wrap all ACP handlers in vim.schedule to ensure safe UI operations (#2670)
This commit is contained in:
@@ -566,7 +566,7 @@ function ACPClient:_handle_session_update(params)
|
||||
end
|
||||
|
||||
if self.config.handlers and self.config.handlers.on_session_update then
|
||||
self.config.handlers.on_session_update(update)
|
||||
vim.schedule(function() self.config.handlers.on_session_update(update) end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -581,18 +581,20 @@ function ACPClient:_handle_request_permission(message_id, params)
|
||||
if not session_id or not tool_call then return end
|
||||
|
||||
if self.config.handlers and self.config.handlers.on_request_permission then
|
||||
self.config.handlers.on_request_permission(
|
||||
tool_call,
|
||||
options,
|
||||
function(option_id)
|
||||
self:_send_result(message_id, {
|
||||
outcome = {
|
||||
outcome = "selected",
|
||||
optionId = option_id,
|
||||
},
|
||||
})
|
||||
end
|
||||
)
|
||||
vim.schedule(function()
|
||||
self.config.handlers.on_request_permission(
|
||||
tool_call,
|
||||
options,
|
||||
function(option_id)
|
||||
self:_send_result(message_id, {
|
||||
outcome = {
|
||||
outcome = "selected",
|
||||
optionId = option_id,
|
||||
},
|
||||
})
|
||||
end
|
||||
)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -606,12 +608,14 @@ function ACPClient:_handle_read_text_file(message_id, params)
|
||||
if not session_id or not path then return end
|
||||
|
||||
if self.config.handlers and self.config.handlers.on_read_file then
|
||||
self.config.handlers.on_read_file(
|
||||
path,
|
||||
params.line ~= vim.NIL and params.line or nil,
|
||||
params.limit ~= vim.NIL and params.limit or nil,
|
||||
function(content) self:_send_result(message_id, { content = content }) end
|
||||
)
|
||||
vim.schedule(function()
|
||||
self.config.handlers.on_read_file(
|
||||
path,
|
||||
params.line ~= vim.NIL and params.line or nil,
|
||||
params.limit ~= vim.NIL and params.limit or nil,
|
||||
function(content) self:_send_result(message_id, { content = content }) end
|
||||
)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -626,11 +630,13 @@ function ACPClient:_handle_write_text_file(message_id, params)
|
||||
if not session_id or not path or not content then return end
|
||||
|
||||
if self.config.handlers and self.config.handlers.on_write_file then
|
||||
self.config.handlers.on_write_file(
|
||||
path,
|
||||
content,
|
||||
function(error) self:_send_result(message_id, error == nil and vim.NIL or error) end
|
||||
)
|
||||
vim.schedule(function()
|
||||
self.config.handlers.on_write_file(
|
||||
path,
|
||||
content,
|
||||
function(error) self:_send_result(message_id, error == nil and vim.NIL or error) end
|
||||
)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -988,40 +988,36 @@ function M._stream_acp(opts)
|
||||
end,
|
||||
})
|
||||
|
||||
vim.schedule(function() selector:open() end)
|
||||
selector:open()
|
||||
end,
|
||||
on_read_file = function(path, line, limit, callback)
|
||||
vim.schedule(function()
|
||||
local abs_path = Utils.to_absolute_path(path)
|
||||
local lines = Utils.read_file_from_buf_or_disk(abs_path)
|
||||
lines = lines or {}
|
||||
if line ~= nil and limit ~= nil then lines = vim.list_slice(lines, line, line + limit) end
|
||||
callback(table.concat(lines, "\n"))
|
||||
end)
|
||||
local abs_path = Utils.to_absolute_path(path)
|
||||
local lines = Utils.read_file_from_buf_or_disk(abs_path)
|
||||
lines = lines or {}
|
||||
if line ~= nil and limit ~= nil then lines = vim.list_slice(lines, line, line + limit) end
|
||||
callback(table.concat(lines, "\n"))
|
||||
end,
|
||||
on_write_file = function(path, content, callback)
|
||||
vim.schedule(function()
|
||||
local abs_path = Utils.to_absolute_path(path)
|
||||
local file = io.open(abs_path, "w")
|
||||
if file then
|
||||
file:write(content)
|
||||
file:close()
|
||||
local buffers = vim.tbl_filter(
|
||||
function(bufnr)
|
||||
return vim.api.nvim_buf_is_valid(bufnr)
|
||||
and vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":p")
|
||||
== vim.fn.fnamemodify(abs_path, ":p")
|
||||
end,
|
||||
vim.api.nvim_list_bufs()
|
||||
)
|
||||
for _, buf in ipairs(buffers) do
|
||||
vim.api.nvim_buf_call(buf, function() vim.cmd("edit") end)
|
||||
end
|
||||
callback(nil)
|
||||
return
|
||||
local abs_path = Utils.to_absolute_path(path)
|
||||
local file = io.open(abs_path, "w")
|
||||
if file then
|
||||
file:write(content)
|
||||
file:close()
|
||||
local buffers = vim.tbl_filter(
|
||||
function(bufnr)
|
||||
return vim.api.nvim_buf_is_valid(bufnr)
|
||||
and vim.fn.fnamemodify(vim.api.nvim_buf_get_name(bufnr), ":p")
|
||||
== vim.fn.fnamemodify(abs_path, ":p")
|
||||
end,
|
||||
vim.api.nvim_list_bufs()
|
||||
)
|
||||
for _, buf in ipairs(buffers) do
|
||||
vim.api.nvim_buf_call(buf, function() vim.cmd("edit") end)
|
||||
end
|
||||
callback("Failed to write file: " .. abs_path)
|
||||
end)
|
||||
callback(nil)
|
||||
return
|
||||
end
|
||||
callback("Failed to write file: " .. abs_path)
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user