generated from ellisonleao/nvim-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathconfig.lua
More file actions
207 lines (197 loc) · 7.38 KB
/
config.lua
File metadata and controls
207 lines (197 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
---@module 'snacks'
local M = {}
---Your `opencode.nvim` configuration.
---Passed via global variable for [simpler UX and faster startup](https://mrcjkb.dev/posts/2023-08-22-setup.html).
---
---Note that Neovim does not yet support metatables or mixed integer and string keys in `vim.g`, affecting some `snacks.nvim` options.
---In that case you may modify `require("opencode.config").opts` directly.
---See [opencode.nvim #36](https://github.com/NickvanDyke/opencode.nvim/issues/36) and [neovim #12544](https://github.com/neovim/neovim/issues/12544#issuecomment-1116794687).
---@type opencode.Opts|nil
vim.g.opencode_opts = vim.g.opencode_opts
---@class opencode.Opts
---
---Where to look for an `opencode` server, and optionally how to manage one.
---@field server? opencode.cli.server.Opts
---
---Contexts to inject into prompts, keyed by their placeholder.
---@field contexts? table<string, fun(context: opencode.Context): string|nil>
---
---Prompts to reference or select from.
---@field prompts? table<string, opencode.Prompt>
---
---Options for `ask()`.
---Supports [`snacks.input`](https://github.com/folke/snacks.nvim/blob/main/docs/input.md).
---@field ask? opencode.ask.Opts
---
---Options for `select()`.
---Supports [`snacks.picker`](https://github.com/folke/snacks.nvim/blob/main/docs/picker.md).
---@field select? opencode.select.Opts
---
---Options for the in-process LSP that interacts with `opencode`.
---@field lsp? opencode.lsp.Opts
---
---Options for `opencode` event handling.
---@field events? opencode.events.Opts
---@class opencode.Prompt : opencode.api.prompt.Opts
---@field prompt string The prompt to send to `opencode`.
---@field ask? boolean Call `ask(prompt)` instead of `prompt(prompt)`. Useful for prompts that expect additional user input.
---@type opencode.Opts
local defaults = {
server = {
port = nil,
start = function()
require("opencode.terminal").start("opencode --port")
end,
stop = function()
require("opencode.terminal").stop()
end,
toggle = function()
require("opencode.terminal").toggle("opencode --port")
end,
},
-- stylua: ignore
contexts = {
["@this"] = function(context) return context:this() end,
["@buffer"] = function(context) return context:buffer() end,
["@buffers"] = function(context) return context:buffers() end,
["@visible"] = function(context) return context:visible_text() end,
["@diagnostics"] = function(context) return context:diagnostics() end,
["@quickfix"] = function(context) return context:quickfix() end,
["@diff"] = function(context) return context:git_diff() end,
["@marks"] = function(context) return context:marks() end,
["@grapple"] = function(context) return context:grapple_tags() end,
},
prompts = {
ask = { prompt = "", ask = true, submit = true },
diagnostics = { prompt = "Explain @diagnostics", submit = true },
diff = { prompt = "Review the following git diff for correctness and readability: @diff", submit = true },
document = { prompt = "Add comments documenting @this", submit = true },
explain = { prompt = "Explain @this and its context", submit = true },
fix = { prompt = "Fix @diagnostics", submit = true },
implement = { prompt = "Implement @this", submit = true },
optimize = { prompt = "Optimize @this for performance and readability", submit = true },
review = { prompt = "Review @this for correctness and readability", submit = true },
test = { prompt = "Add tests for @this", submit = true },
},
ask = {
prompt = "Ask opencode: ",
completion = "customlist,v:lua.opencode_completion",
snacks = {
icon = " ",
win = {
title_pos = "left",
relative = "cursor",
row = -3, -- Row above the cursor
col = 0, -- Align with the cursor
keys = {
i_cr = {
desc = "submit",
},
i_s_cr = {
"<S-CR>",
function(win)
-- Append `\n` to leverage `ask()`'s auto-append behavior in that case
local text = win:text() .. "\\n"
vim.api.nvim_buf_set_lines(win.buf, 0, -1, false, { text })
win:execute("confirm")
end,
mode = "i",
desc = "append",
},
},
footer_keys = { "<CR>", "<S-CR>" },
b = {
completion = true,
},
bo = {
filetype = "opencode_ask",
},
on_buf = function(win)
-- Make sure your completion plugin has the LSP source enabled,
-- either by default or for the `opencode_ask` filetype!
vim.lsp.start(require("opencode.ui.ask.cmp"), {
bufnr = win.buf,
})
end,
},
},
},
select = {
prompt = "opencode: ",
sections = {
prompts = true,
commands = {
["session.new"] = "Start a new session",
["session.select"] = "Select a session",
["session.share"] = "Share the current session",
["session.interrupt"] = "Interrupt the current session",
["session.compact"] = "Compact the current session (reduce context size)",
["session.undo"] = "Undo the last action in the current session",
["session.redo"] = "Redo the last undone action in the current session",
["agent.cycle"] = "Cycle the selected agent",
["prompt.submit"] = "Submit the current prompt",
["prompt.clear"] = "Clear the current prompt",
},
server = true,
},
snacks = {
preview = "preview",
layout = {
preset = "vscode",
hidden = {}, -- preview is hidden by default in `vim.ui.select`
},
},
},
lsp = {
enabled = false,
filetypes = nil,
handlers = {
hover = {
enabled = true,
model = nil,
},
code_action = { enabled = true },
},
},
events = {
enabled = true,
reload = true,
permissions = {
enabled = true,
idle_delay_ms = 1000,
},
},
}
---Plugin options, lazily merged from `defaults` and `vim.g.opencode_opts`.
---@type opencode.Opts
M.opts = vim.tbl_deep_extend("force", vim.deepcopy(defaults), vim.g.opencode_opts or {})
---@diagnostic disable-next-line: undefined-field
if M.opts.provider then
-- TODO: Remove later
vim.notify(
"The `provider` option has been removed for maintenance reasons. Please use the simpler `server` option instead, and/or manage your `opencode` how you do other programs. See `README.md#server` for details. Sorry for the inconvenience!",
vim.log.levels.WARN,
{ title = "opencode" }
)
end
local snacks_ok, snacks = pcall(require, "snacks")
---@cast snacks Snacks
if not snacks_ok or not snacks.config.get("input", {}).enabled then
-- Even though it has no effect, passing these opts to the native `vim.ui.input` will error because
-- they mix string and integer keys which Neovim doesn't support in `vim.g` (see comment on `vim.g.opencode_opts`),
-- and Neovim's native `vim.ui.select` implementation apparently uses those.
M.opts.ask.snacks = {}
end
-- Allow removing default `contexts` and `prompts` by setting them to `false` in your user config.
-- TODO: Add to type definition, and apply to `opts.select.commands`.
local user_opts = vim.g.opencode_opts or {}
for _, field in ipairs({ "contexts", "prompts" }) do
if user_opts[field] and M.opts[field] then
for k, v in pairs(user_opts[field]) do
if not v then
M.opts[field][k] = nil
end
end
end
end
return M