Skip to content

Commit 943f79f

Browse files
committed
feat(api): add opts parameter to add_visual_selection
Add optional opts.open_input (default: true) to control whether the input window opens after adding selection.
1 parent 05747a2 commit 943f79f

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,16 @@ The plugin provides the following actions that can be triggered via keymaps, com
615615
| Toggle tools output (diffs, cmd output, etc.) | `<leader>ott` | `:Opencode toggle_tool_output` | `require('opencode.api').toggle_tool_output()` |
616616
| Toggle reasoning output (thinking steps) | `<leader>otr` | `:Opencode toggle_reasoning_output` | `require('opencode.api').toggle_reasoning_output()` |
617617
| Open a quick chat input with selection/current line context | `<leader>o/` | `:Opencode quick_chat` | `require('opencode.api').quick_chat()` |
618-
| Add visual selection to context | `<leader>oy` | `:Opencode add_visual_selection` | `require('opencode.api').add_visual_selection()` |
618+
| Add visual selection to context | `<leader>oy` | `:Opencode add_visual_selection` | `require('opencode.api').add_visual_selection(opts?)` |
619+
620+
**add_visual_selection opts:**
621+
622+
- `open_input` (boolean, default: `true`): Whether to open the input window after adding the selection. Set to `false` to add selection silently without changing focus.
623+
624+
Example keymap for silent add:
625+
```lua
626+
['<leader>oY'] = { 'add_visual_selection', { open_input = false }, mode = {'v'} }
627+
```
619628

620629
### Run opts
621630

lua/opencode/api.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,16 +1013,15 @@ M.review = Promise.async(function(args)
10131013
end)
10141014
end)
10151015

1016-
--- Add the current visual selection to the context without opening/focusing the panel.
1017-
--- Can be called from any buffer. Selections accumulate across files.
10181016
M.add_visual_selection = Promise.async(
1019-
---@param _ any Unused
1017+
---@param opts? {open_input?: boolean}
10201018
---@param range OpencodeSelectionRange
1021-
function(_, range)
1019+
function(opts, range)
1020+
opts = vim.tbl_extend('force', { open_input = true }, opts or {})
10221021
local context = require('opencode.context')
10231022
local added = context.add_visual_selection(range)
10241023

1025-
if added then
1024+
if added and opts.open_input then
10261025
M.open_input():await()
10271026
end
10281027
end

lua/opencode/keymap.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ local function process_keymap_entry(keymap_config, default_modes, base_opts, def
2727
-- Skip keymap if explicitly set to false (disabled)
2828
elseif config_entry then
2929
local func_name = config_entry[1]
30-
local callback = type(func_name) == 'function' and func_name or api[func_name]
30+
local func_args = config_entry[2]
31+
local raw_callback = type(func_name) == 'function' and func_name or api[func_name]
32+
local callback = raw_callback
33+
34+
if raw_callback and func_args then
35+
callback = function()
36+
raw_callback(func_args)
37+
end
38+
end
39+
3140
local modes = config_entry.mode or default_modes
3241
local opts = vim.tbl_deep_extend('force', {}, base_opts)
3342
opts.desc = config_entry.desc or cmds[func_name] and cmds[func_name].desc

0 commit comments

Comments
 (0)