Skip to content

Commit

Permalink
Attempt #2 to fix #352
Browse files Browse the repository at this point in the history
The previous fix (#388) worked _only for the first time the edit with
instructions window was opened_.

Now it works every time it's opened (at least up to however many times I
tested it. Theoretically it could fail on the 10,000th time.)
  • Loading branch information
aaronik committed Feb 6, 2024
1 parent aadb607 commit f76a21a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lua/chatgpt/code_edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,21 @@ M.edit_with_instructions = function(output_lines, bufnr, selection, ...)
if mode == "i" and (popup == input_window or popup == output_window) then
goto continue
end

popup:map(mode, Config.options.edit_with_instructions.keymaps.cycle_windows, function()
active_panel = active_panel or instructions_input -- otherwise active_panel is nil when window opens
local in_table = inTable(open_extra_panels, active_panel)
-- There's a bug where active_panel is something not in here, maybe an
-- old window, leading to #352 Ideally there's no global variables, but
-- this gets the job done, even if it doesn't fix it at its core.
local possible_windows = {
input_window, output_window, settings_panel,
help_panel, instructions_input, unpack(open_extra_panels)
}

if active_panel == nil or not inTable(possible_windows, active_panel) then
active_panel = instructions_input
end

local active_panel_is_in_extra_panels = inTable(open_extra_panels, active_panel)
if active_panel == instructions_input then
vim.api.nvim_set_current_win(input_window.winid)
active_panel = input_window
Expand All @@ -370,9 +382,9 @@ M.edit_with_instructions = function(output_lines, bufnr, selection, ...)
vim.api.nvim_set_current_win(open_extra_panels[1].winid)
active_panel = open_extra_panels[1]
end
elseif in_table then
elseif active_panel_is_in_extra_panels then
-- next index with wrap around and 0 for instructions_input
local next_index = (in_table + 1) % (#open_extra_panels + 1)
local next_index = (active_panel_is_in_extra_panels + 1) % (#open_extra_panels + 1)
if next_index == 0 then
vim.api.nvim_set_current_win(instructions_input.winid)
active_panel = instructions_input
Expand Down

0 comments on commit f76a21a

Please sign in to comment.