Skip to content

Commit fb33ce6

Browse files
committed
refactor: simplify keymaps by having a corresponding api function
1 parent fa31457 commit fb33ce6

File tree

8 files changed

+129
-98
lines changed

8 files changed

+129
-98
lines changed

lua/opencode/api.lua

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,107 @@ function M.next_history()
228228
end
229229
end
230230

231+
function M.prev_prompt_history()
232+
local config = require('opencode.config').get()
233+
local key = config.keymap.window.prev_prompt_history
234+
if key ~= '<up>' then
235+
return M.prev_history()
236+
end
237+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
238+
local at_boundary = current_line <= 1
239+
240+
if at_boundary then
241+
return M.prev_history()
242+
end
243+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), 'n', false)
244+
end
245+
246+
function M.next_prompt_history()
247+
local config = require('opencode.config').get()
248+
local key = config.keymap.window.next_prompt_history
249+
if key ~= '<down>' then
250+
return M.next_history()
251+
end
252+
local current_line = vim.api.nvim_win_get_cursor(0)[1]
253+
local at_boundary = current_line >= vim.api.nvim_buf_line_count(0)
254+
255+
if at_boundary then
256+
return M.next_history()
257+
end
258+
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(key, true, false, true), 'n', false)
259+
end
260+
261+
function M.next_message()
262+
require('opencode.ui.navigation').goto_next_message()
263+
end
264+
265+
function M.prev_message()
266+
require('opencode.ui.navigation').goto_prev_message()
267+
end
268+
269+
function M.submit_input_prompt()
270+
input_window.handle_submit()
271+
end
272+
273+
function M.mention_file()
274+
local picker = require('opencode.ui.file_picker')
275+
local context = require('opencode.context')
276+
require('opencode.ui.mention').mention(function(mention_cb)
277+
picker.pick(function(file)
278+
mention_cb(file.path)
279+
context.add_file(file.path)
280+
end)
281+
end)
282+
end
283+
284+
function M.mention()
285+
local config = require('opencode.config').get()
286+
local char = config.keymap.window.mention
287+
ui.focus_input({ restore_position = true, start_insert = true })
288+
require('opencode.ui.completion').trigger_completion(char)()
289+
end
290+
291+
function M.slash_commands()
292+
local config = require('opencode.config').get()
293+
local char = config.keymap.window.slash_commands
294+
ui.focus_input({ restore_position = true, start_insert = true })
295+
require('opencode.ui.completion').trigger_completion(char)()
296+
end
297+
298+
function M.focus_input()
299+
ui.focus_input({ restore_position = true, start_insert = true })
300+
end
301+
302+
function M.debug_output()
303+
local config = require('opencode.config').get()
304+
if not config.debug.enabled then
305+
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
306+
return
307+
end
308+
local debug_helper = require('opencode.ui.debug_helper')
309+
debug_helper.debug_output()
310+
end
311+
312+
function M.debug_message()
313+
local config = require('opencode.config').get()
314+
if not config.debug.enabled then
315+
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
316+
return
317+
end
318+
local debug_helper = require('opencode.ui.debug_helper')
319+
debug_helper.debug_message()
320+
end
321+
322+
function M.debug_session()
323+
local config = require('opencode.config').get()
324+
if not config.debug.enabled then
325+
vim.notify('Debugging is not enabled in the config', vim.log.levels.WARN)
326+
return
327+
end
328+
local debug_helper = require('opencode.ui.debug_helper')
329+
debug_helper.debug_session()
330+
end
331+
231332
function M.initialize()
232333
ui.render_output(true)
233334

@@ -274,7 +375,7 @@ function M.select_agent()
274375
end)
275376
end
276377

277-
function M.switch_to_next_mode()
378+
function M.switch_mode()
278379
local modes = require('opencode.config_file').get_opencode_agents()
279380

280381
local current_index = util.index_of(modes, state.current_mode)

lua/opencode/config.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ M.defaults = {
4444
diff_revert_this = '<leader>orT',
4545
diff_restore_snapshot_file = '<leader>orr',
4646
diff_restore_snapshot_all = '<leader>orR',
47-
open_configuration_file = '<leader>oC',
4847
swap_position = '<leader>ox', -- Swap Opencode pane left/right
4948
permission_accept = '<leader>opa',
5049
permission_accept_all = '<leader>opA',

lua/opencode/core.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,16 +157,6 @@ function M.before_run(opts)
157157
})
158158
end
159159

160-
function M.add_file_to_context()
161-
local picker = require('opencode.ui.file_picker')
162-
require('opencode.ui.mention').mention(function(mention_cb)
163-
picker.pick(function(file)
164-
mention_cb(file.path)
165-
context.add_file(file.path)
166-
end)
167-
end)
168-
end
169-
170160
function M.configure_provider()
171161
require('opencode.provider').select(function(selection)
172162
if not selection then

lua/opencode/keymap.lua

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,9 @@ function M.toggle_permission_keymap(buf)
6868
local api = require('opencode.api')
6969

7070
if state.current_permission then
71-
M.buf_keymap(keymaps.permission_accept, function()
72-
api.respond_to_permission('once')
73-
end, buf, { 'n', 'i' })
74-
75-
M.buf_keymap(keymaps.permission_accept_all, function()
76-
api.respond_to_permission('always')
77-
end, buf, { 'n', 'i' })
78-
79-
M.buf_keymap(keymaps.permission_deny, function()
80-
api.respond_to_permission('reject')
81-
end, buf, { 'n', 'i' })
71+
M.buf_keymap(keymaps.permission_accept, api.permission_accept, buf, { 'n', 'i' })
72+
M.buf_keymap(keymaps.permission_accept_all, api.permission_accept_all, buf, { 'n', 'i' })
73+
M.buf_keymap(keymaps.permission_deny, api.permission_deny, buf, { 'n', 'i' })
8274
else
8375
M.clear_permission_keymap(buf)
8476
end

lua/opencode/ui/input_window.lua

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,40 +196,28 @@ end
196196

197197
function M.setup_keymaps(windows)
198198
local map = require('opencode.keymap').buf_keymap
199-
local nav_history = require('opencode.ui.util').navigate_history
200-
local nav = require('opencode.ui.navigation')
201-
local core = require('opencode.core')
202199
local api = require('opencode.api')
203-
local completion = require('opencode.ui.completion')
204200
local keymaps = config.keymap.window
205201
local input_buf = windows.input_buf
206202

207-
map(keymaps.submit, M.handle_submit, input_buf, 'n')
208-
map(keymaps.submit_insert, M.handle_submit, input_buf, 'i')
209-
210-
map(keymaps.mention, completion.trigger_completion(keymaps.mention), input_buf, 'i')
211-
map(keymaps.slash_commands, completion.trigger_completion(keymaps.slash_commands), input_buf, 'i')
212-
map(keymaps.mention_file, core.add_file_to_context, input_buf, 'i')
213-
214-
map(keymaps.prev_prompt_history, nav_history(keymaps.prev_prompt_history, 'prev'), input_buf, { 'n', 'i' })
215-
map(keymaps.next_prompt_history, nav_history(keymaps.next_prompt_history, 'next'), input_buf, { 'n', 'i' })
216-
217-
map(keymaps.switch_mode, api.switch_to_next_mode, input_buf, { 'n', 'i' })
218-
219-
map(keymaps.next_message, nav.goto_next_message, input_buf, 'n')
220-
map(keymaps.prev_message, nav.goto_prev_message, input_buf, 'n')
221-
203+
map(keymaps.submit, api.submit_input_prompt, input_buf, 'n')
204+
map(keymaps.submit_insert, api.submit_input_prompt, input_buf, 'i')
205+
map(keymaps.mention, api.mention, input_buf, 'i')
206+
map(keymaps.slash_commands, api.slash_commands, input_buf, 'i')
207+
map(keymaps.mention_file, api.mention_file, input_buf, 'i')
208+
map(keymaps.prev_prompt_history, api.prev_prompt_history, input_buf, { 'n', 'i' })
209+
map(keymaps.next_prompt_history, api.next_prompt_history, input_buf, { 'n', 'i' })
210+
map(keymaps.switch_mode, api.switch_mode, input_buf, { 'n', 'i' })
211+
map(keymaps.next_message, api.next_message, input_buf, 'n')
212+
map(keymaps.prev_message, api.prev_message, input_buf, 'n')
222213
map(keymaps.close, api.close, input_buf, 'n')
223214
map(keymaps.stop, api.stop, input_buf, 'n')
224215
map(keymaps.toggle_pane, api.toggle_pane, input_buf, { 'n', 'i' })
225-
226216
map(keymaps.select_child_session, api.select_child_session, input_buf, 'n')
227217

228-
if config.debug.enabled then
229-
local debug_helper = require('opencode.ui.debug_helper')
230-
map(keymaps.debug_output, debug_helper.debug_output, input_buf, 'n')
231-
map(keymaps.debug_session, debug_helper.debug_session, input_buf, 'n')
232-
end
218+
map(keymaps.debug_output, api.debug_output, input_buf, 'n')
219+
map(keymaps.debug_session, api.debug_session, input_buf, 'n')
220+
map(keymaps.debug_message, api.debug_message, input_buf, 'n')
233221
end
234222

235223
function M.setup_autocmds(windows, group)

lua/opencode/ui/output_window.lua

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -112,43 +112,24 @@ function M.close()
112112
end
113113

114114
function M.setup_keymaps(windows)
115-
local ui = require('opencode.ui.ui')
116115
local api = require('opencode.api')
117116
local map = require('opencode.keymap').buf_keymap
118-
local nav = require('opencode.ui.navigation')
119117

120118
local keymaps = config.keymap.window
121119
local output_buf = windows.output_buf
122120

123121
map(keymaps.close, api.close, output_buf, 'n')
124-
125-
map(keymaps.next_message, nav.goto_next_message, output_buf, 'n')
126-
map(keymaps.prev_message, nav.goto_prev_message, output_buf, 'n')
127-
122+
map(keymaps.next_message, api.next_message, output_buf, 'n')
123+
map(keymaps.prev_message, api.prev_message, output_buf, 'n')
128124
map(keymaps.stop, api.stop, output_buf, { 'n' })
129-
130125
map(keymaps.toggle_pane, api.toggle_pane, output_buf, { 'n' })
131-
132-
map(keymaps.focus_input, function()
133-
ui.focus_input({ restore_position = true, start_insert = true })
134-
end, output_buf, 'n')
135-
136-
map(keymaps.switch_mode, api.switch_to_next_mode, output_buf, 'n')
137-
126+
map(keymaps.focus_input, api.focus_input, output_buf, 'n')
127+
map(keymaps.switch_mode, api.switch_mode, output_buf, 'n')
138128
map(keymaps.select_child_session, api.select_child_session, output_buf, 'n')
139129

140-
if config.debug.enabled then
141-
local debug_helper = require('opencode.ui.debug_helper')
142-
if debug_helper.debug_output then
143-
map(keymaps.debug_output, debug_helper.debug_output, output_buf, 'n')
144-
end
145-
if debug_helper.debug_message then
146-
map(keymaps.debug_message, debug_helper.debug_message, output_buf, 'n')
147-
end
148-
if debug_helper.debug_session then
149-
map(keymaps.debug_session, debug_helper.debug_session, output_buf, 'n')
150-
end
151-
end
130+
map(keymaps.debug_output, api.debug_output, output_buf, 'n')
131+
map(keymaps.debug_message, api.debug_message, output_buf, 'n')
132+
map(keymaps.debug_session, api.debug_session, output_buf, 'n')
152133
end
153134

154135
function M.setup_autocmds(windows, group)

lua/opencode/ui/ui.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ function M.focus_input(opts)
125125
return
126126
end
127127

128+
if vim.api.nvim_get_current_win() == windows.input_win then
129+
return
130+
end
131+
128132
vim.api.nvim_set_current_win(windows.input_win)
129133

130134
if opts.restore_position and state.last_input_window_position then

lua/opencode/ui/util.lua

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)