-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathopencode.lua
More file actions
111 lines (99 loc) · 3.35 KB
/
Copy pathopencode.lua
File metadata and controls
111 lines (99 loc) · 3.35 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
---opencode.nvim public API.
local M = {}
---@param err? string
local function on_error(err)
if err then
vim.notify(err, vim.log.levels.ERROR, { title = "opencode" })
end
end
---Input a prompt for OpenCode.
---
--- - Passes the text to `prompt()`.
--- - Press `<Up>` to browse recent asks.
--- - Highlights and completes contexts.
--- - Press `<Tab>` to trigger built-in completion.
--- - Provided by in-process LSP when using [snacks.input](https://github.com/folke/snacks.nvim/blob/main/docs/input.md).
---
---@param default? string Text to pre-fill the input with.
function M.ask(default)
require("opencode.server.discovery")
.get()
:next(function(server)
local context = require("opencode.context").new(server)
return require("opencode.ui.ask").ask(default, context):next(function(input)
return require("opencode.api.prompt").prompt(input, context)
end)
end)
:catch(on_error)
end
---Select from all opencode.nvim functionality.
---
---Highlights and previews items when using [snacks.picker](https://github.com/folke/snacks.nvim/blob/main/docs/picker.md).
---
---@param opts? opencode.select.Opts Override configured options for this call.
function M.select(opts)
require("opencode.server.discovery")
.get()
:next(function(server)
local context = require("opencode.context").new(server)
return require("opencode.ui.select").select(context, opts)
end)
:catch(on_error)
end
M.statusline = require("opencode.events.status").statusline
---Prompt OpenCode.
---
---Targets the most recently updated session for Neovim's directory.
---Injects configured contexts.
---Trailing "..." opens in `ask()`.
---
---@param prompt string
function M.prompt(prompt)
require("opencode.server.discovery")
.get()
:next(function(server)
local context = require("opencode.context").new(server)
return require("opencode.api.prompt").prompt(prompt, context)
end)
:catch(on_error)
end
---Run a registered OpenCode [command](https://opencode.ai/v2/docs/commands/).
---Targets the most recently updated session for Neovim's directory.
---
---@param command string
---@param text? string Arguments for the command.
function M.command(command, text)
require("opencode.server.discovery")
.get()
:next(function(server)
return require("opencode.api.command").command(command, text or "", server)
end)
:catch(on_error)
end
---Wraps `prompt` as an operator, supporting ranges and dot-repeat.
---
---@param prompt string
function M.operator(prompt)
_G.opencode_prompt_operator = function(kind) ---@param kind "char" | "line" | "block"
local start_pos = vim.api.nvim_buf_get_mark(0, "[")
local end_pos = vim.api.nvim_buf_get_mark(0, "]")
if start_pos[1] > end_pos[1] or (start_pos[1] == end_pos[1] and start_pos[2] > end_pos[2]) then
start_pos, end_pos = end_pos, start_pos
end
require("opencode.server.discovery")
.get()
:next(function(server)
local context = require("opencode.context").new(server, {
from = { start_pos[1], start_pos[2] },
to = { end_pos[1], end_pos[2] },
kind = kind,
})
return require("opencode.api.prompt").prompt(prompt, context)
end)
:catch(on_error)
end
vim.o.operatorfunc = "v:lua.opencode_prompt_operator"
return "g@"
end
M.format = require("opencode.context").format
return M