A Neovim Lua plugin that bridges Neovim and the opencode CLI (external binary). It discovers or starts an opencode server, communicates via REST + SSE, and provides UI for prompting, context injection, session management, and edit review.
- Public API:
lua/opencode.lua— exportsask(),select(),prompt(),command(),operator(),format(),statusline - Config:
vim.g.opencode_optsglobal (not asetup()call); merged with defaults fromlua/opencode/config.lua - Plugin files:
plugin/highlights.luasets highlight groups;plugin/events/registers four autocmd groups (OpencodeReload,OpencodeStatus,OpencodePermissions,OpencodeEdits) that listen forOpencodeEvent:*User events to reload edited buffers, update statusline, display permission requests, and diff edit proposals
- Config is passed via
vim.g.opencode_optsfor simpler UX and faster startup (seelua/opencode/config.lua:6) snacks.nvimnested opts go underask.snacks/select.snacks, then get merged into the passed options at usage time (lua/opencode/ui/ask/init.lua,lua/opencode/ui/select.lua)vim.o.autoreadis automatically set totruewhenevents.reload.enabled = true(the default) unless the user has explicitly configured it- Neovim doesn't support mixed integer/string keys in
vim.g, which affects somesnacks.inputoptions; workaround: modifyrequire("opencode.config").optsdirectly
- Required:
opencodeCLI,curl - Auto-discovery: reads OpenCode's background service registration (
service.json, orservice-<channel>.jsonfor other channels) from its state directory (unlessserver.urlis set) - Optional:
snacks.nvim(enhancesask()withsnacks.input,select()withsnacks.picker),blink.cmp(completion plugin with LSP source) - No hard Lua dependencies beyond Neovim itself
# Type-check (requires neovim + lua-language-server + cloned snacks/blink)
lua-language-server --configpath .luarc.ci.json --check=.
# Format check
stylua --check .
# Format in place
stylua ..github/workflows/ci.yml: two jobs on push/PR to main —formatrunsstylua --check .,lua-lsruns LuaLS diagnostics.github/workflows/release-please.yml: automated releases via release-please (non-fork, main branch only)
- No test framework — no tests directory, no test runner config
- Manual verification:
:checkhealth opencode
column_width = 120,indent_width = 2, spaces, double quotes, no call parentheses- File:
.stylua.toml
- Config:
.luarc.ci.json - Runtime:
LuaJIT - Library paths:
/opt/nvim/share/nvim/runtime/lua, clonedsnacks.nvimandblink.cmp
- Async: custom Promise implementation in
lua/opencode/promise/init.lua(fork ofpromise.nvim) - Server discovery flow (
lua/opencode/server/discovery/init.lua): connected server → configured URL → OpenCode background service registrations (service.jsonorservice-<channel>.json, URL + password; each tried newest first) → auto-start + poll (5s timeout) - OpenCode v2 API (
lua/opencode/server/init.lua): the HTTP API lives under/api/*and requires HTTP basic auth. The password comes from the service registration oropts.server.password. Requests carryx-opencode-directory: <nvim cwd>; the daemon is shared across projects, so/api/sessionis scoped with adirectoryquery. Prompts and commands target the most recently updated root session for Neovim's directory (Server:resolve_session()), resolved per call and never cached. OpenCode v2 has no TUI-driving or prompt-append API. - Discovery vs connection: server.connect (default true) controls whether the discovered server is automatically subscribed to via SSE. When false, the server is found but not connected.
- Context system (
lua/opencode/context/init.lua): captures buffer/win/cursor/selection before UI opens, renders placeholders (@this,@buffer, etc.) in prompts - Events: SSE subscribed on
connect()(/api/event), dispatched asOpencodeEvent:<type>User autocmds. OpenCode v2 events are shaped{ id, type, data }. - Edit review: opens diff in new tab via
:diffpatch, keymapsda/drto accept/reject,dp/dofor per-hunk - Ask completion: in-process LSP server (
lua/opencode/ui/ask/cmp.lua) providing context placeholder + agent completions - Integration policy: code that bridges another tool to opencode.nvim (e.g. picker send, terminal toggle) belongs in README examples. Code that enhances opencode.nvim's own UI (ask/select with snacks input/picker) stays in the plugin.
- Operator:
operator()setsoperatorfunc, usesg@for range + dot-repeat support
See CONTRIBUTING.md for project guidelines, priorities, and maintenance philosophy. When in doubt, follow the patterns already in the codebase.