hephaestus/heph.nvim/plugin/heph.lua
Erich Blume e3db2ac550 heph.nvim: plug-and-play managed daemon (autostart, self-heal, client/server guardrail)
The plugin now manages its own hephd by default (autostart = true): if nothing
is serving the socket it spawns a local daemon against the default XDG paths,
kills only what it spawned on VimLeavePre, and self-heals — rpc.call retries
once through a respawn hook when the connection drops (the prior owner releases
the DB lock on exit, so a respawn can claim it).

- daemon.ensure() connects to an already-running daemon (any mode) or spawns one
  we own; stop_spawned()/is_managed() track lifecycle.
- A server/client daemon you started is always respected (spawn only when nothing
  serves the socket). autostart = false → connect-only, warns/errors if down,
  and clears the self-heal hook so it fails loudly.
- config: autostart defaults true; new `db` option; $HEPH_SOCKET / $HEPH_DB
  fallbacks isolate a dev Neovim onto a separate daemon + DB.

e2e: managed_daemon_spec covers autostart spawn, self-heal-after-kill, and
connect-only error. 10 specs green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-02 09:37:49 -07:00

55 lines
1.5 KiB
Lua

--- heph.nvim plugin entry: register the `heph://` buffer autocmds and the
--- `:Heph` command. Loaded once by Neovim from `runtimepath/plugin/`.
if vim.g.loaded_heph then
return
end
vim.g.loaded_heph = true
local grp = vim.api.nvim_create_augroup("heph", { clear = true })
-- `heph://node/<id>` buffers load and save through the daemon (tech-spec §8).
vim.api.nvim_create_autocmd("BufReadCmd", {
group = grp,
pattern = "heph://node/*",
callback = function(ev)
local ok, err = pcall(require("heph.node").read, ev.buf, ev.match)
if not ok then
vim.notify(tostring(err), vim.log.levels.ERROR)
end
end,
})
vim.api.nvim_create_autocmd("BufWriteCmd", {
group = grp,
pattern = "heph://node/*",
callback = function(ev)
local ok, err = pcall(require("heph.node").write, ev.buf, ev.match)
if not ok then
vim.notify(tostring(err), vim.log.levels.ERROR)
end
end,
})
-- Release the socket and stop any daemon this nvim spawned, cleanly, on exit.
vim.api.nvim_create_autocmd("VimLeavePre", {
group = grp,
callback = function()
pcall(function()
require("heph.rpc").close()
end)
pcall(function()
require("heph.daemon").stop_spawned()
end)
end,
})
vim.api.nvim_create_user_command("Heph", function(opts)
require("heph.command").run(opts)
end, {
nargs = "*",
desc = "hephaestus",
complete = function(arglead, cmdline, cursorpos)
return require("heph.command").complete(arglead, cmdline, cursorpos)
end,
})