fix: wrap all ACP handlers in vim.schedule to ensure safe UI operations (#2670)

This commit is contained in:
yetone
2025-09-02 13:37:26 +08:00
committed by GitHub
parent 8794356cf4
commit a4a4c91711
2 changed files with 55 additions and 53 deletions

View File

@@ -566,7 +566,7 @@ function ACPClient:_handle_session_update(params)
end end
if self.config.handlers and self.config.handlers.on_session_update then 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
end end
@@ -581,6 +581,7 @@ function ACPClient:_handle_request_permission(message_id, params)
if not session_id or not tool_call then return end if not session_id or not tool_call then return end
if self.config.handlers and self.config.handlers.on_request_permission then if self.config.handlers and self.config.handlers.on_request_permission then
vim.schedule(function()
self.config.handlers.on_request_permission( self.config.handlers.on_request_permission(
tool_call, tool_call,
options, options,
@@ -593,6 +594,7 @@ function ACPClient:_handle_request_permission(message_id, params)
}) })
end end
) )
end)
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 not session_id or not path then return end
if self.config.handlers and self.config.handlers.on_read_file then if self.config.handlers and self.config.handlers.on_read_file then
vim.schedule(function()
self.config.handlers.on_read_file( self.config.handlers.on_read_file(
path, path,
params.line ~= vim.NIL and params.line or nil, params.line ~= vim.NIL and params.line or nil,
params.limit ~= vim.NIL and params.limit or nil, params.limit ~= vim.NIL and params.limit or nil,
function(content) self:_send_result(message_id, { content = content }) end function(content) self:_send_result(message_id, { content = content }) end
) )
end)
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 not session_id or not path or not content then return end
if self.config.handlers and self.config.handlers.on_write_file then if self.config.handlers and self.config.handlers.on_write_file then
vim.schedule(function()
self.config.handlers.on_write_file( self.config.handlers.on_write_file(
path, path,
content, content,
function(error) self:_send_result(message_id, error == nil and vim.NIL or error) end function(error) self:_send_result(message_id, error == nil and vim.NIL or error) end
) )
end)
end end
end end

View File

@@ -988,19 +988,16 @@ function M._stream_acp(opts)
end, end,
}) })
vim.schedule(function() selector:open() end) selector:open()
end, end,
on_read_file = function(path, line, limit, callback) on_read_file = function(path, line, limit, callback)
vim.schedule(function()
local abs_path = Utils.to_absolute_path(path) local abs_path = Utils.to_absolute_path(path)
local lines = Utils.read_file_from_buf_or_disk(abs_path) local lines = Utils.read_file_from_buf_or_disk(abs_path)
lines = lines or {} lines = lines or {}
if line ~= nil and limit ~= nil then lines = vim.list_slice(lines, line, line + limit) end if line ~= nil and limit ~= nil then lines = vim.list_slice(lines, line, line + limit) end
callback(table.concat(lines, "\n")) callback(table.concat(lines, "\n"))
end)
end, end,
on_write_file = function(path, content, callback) on_write_file = function(path, content, callback)
vim.schedule(function()
local abs_path = Utils.to_absolute_path(path) local abs_path = Utils.to_absolute_path(path)
local file = io.open(abs_path, "w") local file = io.open(abs_path, "w")
if file then if file then
@@ -1021,7 +1018,6 @@ function M._stream_acp(opts)
return return
end end
callback("Failed to write file: " .. abs_path) callback("Failed to write file: " .. abs_path)
end)
end, end,
}, },
}) })