Skip to content

Commit 44709a3

Browse files
authored
feat: ctrl-n or keymap to switch between windows (#20)
1 parent ece2c44 commit 44709a3

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

lua/goose/api.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,35 @@ function M.select_session()
6363
return true
6464
end
6565

66+
function M.toggle_pane()
67+
if not state.windows then
68+
core.open({ new_session = false, focus = "output" })
69+
return true
70+
end
71+
72+
local current_win = vim.api.nvim_get_current_win()
73+
if current_win == state.windows.input_win then
74+
-- When moving from input to output, exit insert mode first
75+
vim.cmd('stopinsert')
76+
vim.api.nvim_set_current_win(state.windows.output_win)
77+
else
78+
-- When moving from output to input, just change window
79+
-- (don't automatically enter insert mode)
80+
vim.api.nvim_set_current_win(state.windows.input_win)
81+
82+
-- Fix placeholder text when switching to input window
83+
local lines = vim.api.nvim_buf_get_lines(state.windows.input_buf, 0, -1, false)
84+
if #lines == 1 and lines[1] == "" then
85+
-- Only show placeholder if the buffer is empty
86+
require('goose.ui.window_config').setup_placeholder(state.windows)
87+
else
88+
-- Clear placeholder if there's text in the buffer
89+
vim.api.nvim_buf_clear_namespace(state.windows.input_buf, vim.api.nvim_create_namespace('input-placeholder'), 0, -1)
90+
end
91+
end
92+
return true
93+
end
94+
6695
-- Command definitions that call the API functions
6796
M.commands = {
6897
open_input = {
@@ -121,6 +150,14 @@ M.commands = {
121150
end
122151
},
123152

153+
toggle_pane = {
154+
name = "GooseTogglePane",
155+
desc = "Toggle between input and output panes",
156+
fn = function()
157+
M.toggle_pane()
158+
end
159+
},
160+
124161
run = {
125162
name = "GooseRun",
126163
desc = "Run Goose with a prompt (continue last session)",

lua/goose/config.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ M.defaults = {
1919
stop = '<C-c>',
2020
next_message = ']]',
2121
prev_message = '[[',
22-
mention_file = '@'
22+
mention_file = '@',
23+
toggle_pane = '<C-n>'
2324
}
2425
},
2526
ui = {

lua/goose/ui/window_config.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ function M.setup_autocmds(windows)
4848
group = group,
4949
buffer = windows.output_buf,
5050
callback = function()
51-
M.setup_placeholder(windows)
5251
vim.cmd('stopinsert')
5352
end
5453
})
@@ -57,7 +56,18 @@ function M.setup_autocmds(windows)
5756
vim.api.nvim_create_autocmd('WinEnter', {
5857
group = group,
5958
buffer = windows.input_buf,
60-
callback = function() vim.cmd('startinsert') end
59+
callback = function()
60+
-- Don't automatically enter insert mode when switching windows
61+
-- Check if the buffer has content
62+
local lines = vim.api.nvim_buf_get_lines(windows.input_buf, 0, -1, false)
63+
if #lines == 1 and lines[1] == "" then
64+
-- Only show placeholder if the buffer is empty
65+
M.setup_placeholder(windows)
66+
else
67+
-- Clear placeholder if there's text in the buffer
68+
vim.api.nvim_buf_clear_namespace(windows.input_buf, vim.api.nvim_create_namespace('input-placeholder'), 0, -1)
69+
end
70+
end
6171
})
6272

6373
vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, {
@@ -198,6 +208,15 @@ function M.setup_keymaps(windows)
198208
vim.keymap.set('i', window_keymap.mention_file, function()
199209
require('goose.core').add_file_to_context()
200210
end, { buffer = windows.input_buf, silent = true })
211+
212+
-- Add toggle pane keymapping for both buffers in normal and insert mode
213+
vim.keymap.set({ 'n', 'i' }, window_keymap.toggle_pane, function()
214+
api.toggle_pane()
215+
end, { buffer = windows.input_buf, silent = true })
216+
217+
vim.keymap.set({ 'n', 'i' }, window_keymap.toggle_pane, function()
218+
api.toggle_pane()
219+
end, { buffer = windows.output_buf, silent = true })
201220
end
202221

203222
return M

0 commit comments

Comments
 (0)