Skip to content

Commit 80a364b

Browse files
committed
feat(prompt): resolve prompts referenced by name
1 parent 69c94dc commit 80a364b

File tree

2 files changed

+32
-53
lines changed

2 files changed

+32
-53
lines changed

README.md

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ vim.g.opencode_opts = {
114114

115115
### 🗣️ Prompt — `require("opencode").prompt()` | `:[range]OpencodePrompt`
116116

117-
Send a prompt.
117+
Send a prompt to `opencode`.
118118

119119
Replaces placeholders with the corresponding context:
120120

@@ -131,9 +131,25 @@ Replaces placeholders with the corresponding context:
131131
| `@diff` | Git diff |
132132
| `@grapple` | [grapple.nvim](https://github.com/cbochs/grapple.nvim) tags |
133133

134+
Or pass a prompt name from `opts.prompts` to review, explain, and improve your code:
135+
136+
| Name | Prompt |
137+
|------------------------------------|-----------------------------------------------------------|
138+
| `ask` | *...* |
139+
| `explain` | Explain `@this` and its context |
140+
| `optimize` | Optimize `@this` for performance and readability |
141+
| `document` | Add comments documenting `@this` |
142+
| `test` | Add tests for `@this` |
143+
| `review` | Review `@this` for correctness and readability |
144+
| `diagnostics` | Explain `@diagnostics` |
145+
| `fix` | Fix `@diagnostics` |
146+
| `diff` | Review the following git diff for correctness and readability: `@diff` |
147+
| `buffer` | `@buffer` |
148+
| `this` | `@this` |
149+
134150
### ✍️ Ask — `require("opencode").ask()`
135151

136-
Input a prompt.
152+
Input a prompt to send to `opencode`.
137153

138154
- Highlights placeholders.
139155
- Completes placeholders.
@@ -143,7 +159,7 @@ Input a prompt.
143159

144160
### 🧑‍🏫 Command — `require("opencode").command()`
145161

146-
Send a [command](https://opencode.ai/docs/keybinds) to control `opencode`:
162+
Send a [command](https://opencode.ai/docs/keybinds) to `opencode`:
147163

148164
| Command | Description |
149165
|---------------------------|----------------------------------------------------------|
@@ -173,48 +189,9 @@ Toggle `opencode` via `opts.provider.toggle`. Usually not explicitly needed —
173189

174190
A single entrypoint to all `opencode.nvim` functionality 😄
175191

176-
#### Prompt
177-
178-
Select from `opts.prompts` to review, explain, and improve your code:
179-
180-
| Name | Prompt |
181-
|------------------------------------|-----------------------------------------------------------|
182-
| `ask` | *...* |
183-
| `explain` | Explain `@this` and its context |
184-
| `optimize` | Optimize `@this` for performance and readability |
185-
| `document` | Add comments documenting `@this` |
186-
| `test` | Add tests for `@this` |
187-
| `review` | Review `@this` for correctness and readability |
188-
| `diagnostics` | Explain `@diagnostics` |
189-
| `fix` | Fix `@diagnostics` |
190-
| `diff` | Review the following git diff for correctness and readability: `@diff` |
191-
| `buffer` | `@buffer` |
192-
| `this` | `@this` |
193-
194-
#### Command
195-
196-
Select from `opts.commands` to control `opencode`:
197-
198-
| Command | Description |
199-
|---------------------------|----------------------------------------------------------|
200-
| `session_new` | Start a new session |
201-
| `session_share` | Share the current session |
202-
| `session_interrupt` | Interrupt the current session |
203-
| `session_compact` | Compact the current session (reduce context size) |
204-
| `messages_copy` | Copy the last message in the session |
205-
| `messages_undo` | Undo the last message in the session |
206-
| `messages_redo` | Redo the last message in the session |
207-
| `agent_cycle` | Cycle the selected agent |
208-
209-
#### Provider
210-
211-
Manage `opts.provider`:
212-
213-
| Name | Function |
214-
|------|----------|
215-
| `toggle` | Toggle `opencode` |
216-
| `start` | Start `opencode` |
217-
| `show` | Show `opencode` |
192+
- Prompt library (`opts.prompts`)
193+
- Command library (`opts.commands`)
194+
- Manage provider (`opts.provider`)
218195

219196
## 👀 Events
220197

lua/opencode.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ end
1818

1919
---Prompt `opencode`.
2020
---
21-
---1. Clears the TUI input if `opts.clear`.
22-
---2. Appends `prompt` to the TUI input.
23-
--- - Injects `opts.contexts` into `prompt`.
24-
---3. Submits the TUI input if `opts.submit`.
25-
--- - Listens for Server-Sent-Events to forward as `OpencodeEvent` autocmd.
26-
--- - Calls `opts.on_send`.
27-
---4. Calls `callback` if provided.
21+
---1. Resolves `prompt` if it's a prompt name from `opts.prompts`.
22+
---2. Clears the TUI input if `opts.clear`.
23+
---3. Injects `opts.contexts` into `prompt`.
24+
---4. Appends `prompt` to the TUI input.
25+
---5. Submits the TUI input if `opts.submit`.
26+
---6. Listens for Server-Sent-Events to forward as `OpencodeEvent` autocmd.
27+
---7. Calls `callback` if provided.
2828
---
29-
---@param prompt string The prompt to send to `opencode`, with optional `opts.contexts` placeholders.
29+
---@param prompt string The prompt to send to `opencode`, or a prompt name from `opts.prompts`.
3030
---@param opts? opencode.prompt.Opts
3131
---@param callback? fun()
3232
function M.prompt(prompt, opts, callback)
33+
local referenced_prompt = require("opencode.config").opts.prompts[prompt]
34+
prompt = referenced_prompt and referenced_prompt.prompt or prompt
3335
opts = {
3436
clear = opts and opts.clear or false,
3537
submit = opts and opts.submit or false,

0 commit comments

Comments
 (0)