Skip to content

Commit ad5629e

Browse files
committed
fix(permissions): change permissions shortcuts on focus changes
The permissions prompt should not display buffer-local shortcuts when not focused but rather global ones
1 parent 1f58990 commit ad5629e

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

lua/opencode/core.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ function M.open(opts)
7474
elseif opts.focus == 'output' then
7575
ui.focus_output({ restore_position = are_windows_closed })
7676
end
77+
state.is_opencode_focused = true
7778
end
7879

7980
--- Sends a message to the active session, creating one if necessary.

lua/opencode/state.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ local config = require('opencode.config')
1111
---@class OpencodeState
1212
---@field windows OpencodeWindowState|nil
1313
---@field input_content table
14+
---@field is_opencode_focused boolean
1415
---@field last_focused_opencode_window string|nil
1516
---@field last_input_window_position number|nil
1617
---@field last_output_window_position number|nil
@@ -46,6 +47,7 @@ local _state = {
4647
-- ui
4748
windows = nil, ---@type OpencodeWindowState
4849
input_content = {},
50+
is_opencode_focused = false,
4951
last_focused_opencode_window = nil,
5052
last_input_window_position = nil,
5153
last_output_window_position = nil,

lua/opencode/ui/autocmds.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function M.setup_autocmds(windows)
2525
vim.api.nvim_create_autocmd('WinLeave', {
2626
group = group,
2727
pattern = '*',
28-
callback = function()
28+
callback = function(e)
2929
if not require('opencode.ui.ui').is_opencode_focused() then
3030
require('opencode.context').load()
3131
require('opencode.state').last_code_win_before_opencode = vim.api.nvim_get_current_win()
@@ -39,6 +39,14 @@ function M.setup_autocmds(windows)
3939
end
4040
end,
4141
})
42+
43+
vim.api.nvim_create_autocmd('WinEnter', {
44+
group = group,
45+
pattern = '*',
46+
callback = function()
47+
require('opencode.state').is_opencode_focused = require('opencode.ui.ui').is_opencode_focused()
48+
end,
49+
})
4250
end
4351

4452
function M.setup_resize_handler(windows)

lua/opencode/ui/renderer.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ function M._setup_event_subscriptions(subscribe)
7878
state.event_manager[method](state.event_manager, 'permission.replied', M.on_permission_replied)
7979
state.event_manager[method](state.event_manager, 'file.edited', M.on_file_edited)
8080
state.event_manager[method](state.event_manager, 'custom.restore_point.created', M.on_restore_points)
81+
82+
state[method]('is_opencode_focused', M.on_focus_changed)
8183
end
8284

8385
---Unsubscribe from local state and server subscriptions
@@ -696,6 +698,20 @@ function M._rerender_part(part_id)
696698
M._replace_part_in_buffer(part_id, formatted)
697699
end
698700

701+
---Event handler for focus changes
702+
---Re-renders part associated with current permission for displaying global shortcuts or buffer-local ones
703+
function M.on_focus_changed()
704+
if not state.current_permission or not state.current_permission.callID then
705+
return
706+
end
707+
708+
local part_id = M._find_part_by_call_id(state.current_permission.callID, state.current_permission.messageID)
709+
if part_id then
710+
M._rerender_part(part_id)
711+
trigger_on_data_rendered()
712+
end
713+
end
714+
699715
---Get all actions available at a specific line
700716
---@param line integer 1-indexed line number
701717
---@return table[] List of actions available at that line

0 commit comments

Comments
 (0)