Skip to content

Commit 65ce845

Browse files
committed
feat(ask): end prompt with \n or press <C-CR> to append instead of submit
also consolidates the `opts.select.prompts` default ask entries into just one because this captures that flexibility better
1 parent d080eb4 commit 65ce845

4 files changed

Lines changed: 48 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Integrate the [opencode](https://github.com/sst/opencode) AI assistant with Neov
3434
config = function()
3535
---@type opencode.Opts
3636
vim.g.opencode_opts = {
37-
-- Your configuration, if any — see `lua/opencode/config.lua`, or "goto definition" on the type or field.
37+
-- Your configuration, if any. Goto definition on the type or field for details.
3838
}
3939

4040
-- Required for `opts.events.reload`.
@@ -266,7 +266,10 @@ Input a prompt for `opencode`.
266266
- Press `<Up>` to browse recent asks.
267267
- Highlights and completes contexts and `opencode` subagents.
268268
- Press `<Tab>` to trigger built-in completion.
269-
- Registers `opts.ask.blink_cmp_sources` when using `snacks.input` and `blink.cmp`.
269+
- End the prompt with `\n` to append instead of submit.
270+
- Additionally, when using `snacks.input`:
271+
- Press `<C-CR>` to append instead of submit.
272+
- When using `blink.cmp`, registers `opts.ask.blink_cmp_sources`.
270273

271274
### 📝 Select — `require("opencode").select()`
272275

lua/opencode.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ local M = {}
66
--- - Press the up arrow to browse recent asks.
77
--- - Highlights and completes contexts and `opencode` subagents.
88
--- - Press `<Tab>` to trigger built-in completion.
9-
--- - Registers `opts.ask.blink_cmp_sources` when using `snacks.input` and `blink.cmp`.
9+
--- - End the prompt with `\n` to append instead of submit.
10+
--- - Additionally, when using `snacks.input`:
11+
--- - Press `<C-CR>` to append instead of submit.
12+
--- - When using `blink.cmp`, registers `opts.ask.blink_cmp_sources`.
1013
---
1114
---@param default? string Text to pre-fill the input with.
1215
---@param opts? opencode.api.prompt.Opts Options for `prompt()`.
@@ -17,6 +20,16 @@ M.ask = function(default, opts)
1720
return require("opencode.ui.ask")
1821
.ask(default, opts.context)
1922
:next(function(input) ---@param input string
23+
-- TODO: Should we remove `opts.clear` and `opts.submit` in favor of just checking if the input ends with `\n`?
24+
-- (maybe even in `prompt()` itself?)
25+
-- Confusing to have both.
26+
-- I think it's better, but don't love the breaking change.
27+
-- Although for most users, I imagine they just use `opts.submit = false` and thus won't be affected.
28+
if input:sub(-2) == "\\n" then
29+
input = input:sub(1, -3)
30+
opts.clear = false
31+
opts.submit = false
32+
end
2033
opts.context:clear()
2134
return require("opencode.api.prompt").prompt(input, opts)
2235
end)

lua/opencode/config.lua

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ local defaults = {
5858
["@grapple"] = function(context) return context:grapple_tags() end,
5959
},
6060
prompts = {
61-
ask_append = { prompt = "", ask = true }, -- Handy to insert context mid-prompt. Simpler than exposing every context as a prompt by default.
62-
ask_this = { prompt = "@this: ", ask = true, submit = true },
61+
ask = { prompt = "", ask = true, submit = true },
6362
diagnostics = { prompt = "Explain @diagnostics", submit = true },
6463
diff = { prompt = "Review the following git diff for correctness and readability: @diff", submit = true },
6564
document = { prompt = "Add comments documenting @this", submit = true },
@@ -80,6 +79,23 @@ local defaults = {
8079
relative = "cursor",
8180
row = -3, -- Row above the cursor
8281
col = 0, -- Align with the cursor
82+
keys = {
83+
i_cr = {
84+
desc = "submit",
85+
},
86+
i_c_cr = {
87+
"<c-cr>",
88+
function(win)
89+
-- Append `\n` to leverage `ask()`'s auto-append behavior in that case
90+
local text = win:text() .. "\\n"
91+
vim.api.nvim_buf_set_lines(win.buf, 0, -1, false, { text })
92+
win:execute("confirm")
93+
end,
94+
mode = "i",
95+
desc = "append",
96+
},
97+
},
98+
footer_keys = { "<cr>", "<c-cr>" },
8399
},
84100
},
85101
},
@@ -174,6 +190,15 @@ local defaults = {
174190
---@type opencode.Opts
175191
M.opts = vim.tbl_deep_extend("force", vim.deepcopy(defaults), vim.g.opencode_opts or {})
176192

193+
local snacks_ok, snacks = pcall(require, "snacks")
194+
---@cast snacks Snacks
195+
if not snacks_ok or not snacks.config.get("input", {}).enabled then
196+
-- Even though it has no effect, passing these opts to the native `vim.ui.input` will error because
197+
-- they mix string and integer keys which Neovim doesn't support in `vim.g` (see comment on `vim.g.opencode_opts`),
198+
-- and Neovim's native `vim.ui.select` implementation apparently uses those.
199+
M.opts.ask.snacks = {}
200+
end
201+
177202
-- Allow removing default `contexts` and `prompts` by setting them to `false` in your user config.
178203
-- TODO: Add to type definition, and apply to `opts.select.commands`.
179204
local user_opts = vim.g.opencode_opts or {}

lua/opencode/provider/snacks.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ end
2121
---Check if `snacks.terminal` is available and enabled.
2222
function Snacks.health()
2323
local snacks_ok, snacks = pcall(require, "snacks")
24+
---@cast snacks Snacks
2425
if not snacks_ok then
2526
return "`snacks.nvim` is not available.", {
2627
"Install `snacks.nvim` and enable `snacks.terminal.`",
2728
}
28-
elseif not snacks and snacks.config.get("terminal", {}).enabled then
29+
elseif not snacks.config.get("terminal", {}).enabled then
2930
return "`snacks.terminal` is not enabled.",
3031
{
3132
"Enable `snacks.terminal` in your `snacks.nvim` configuration.",

0 commit comments

Comments
 (0)