hephaestus/heph.nvim/lua/heph/init.lua

62 lines
1.8 KiB
Lua
Raw Normal View History

--- heph.nvim — the primary surface for hephaestus (tech-spec §8): an
--- obsidian.nvim replacement that is a thin client of the local `hephd` over
--- its unix-socket JSON-RPC. This module is the public entry point.
local config = require("heph.config")
local M = {}
--- The resolved config from the last `setup` (nil before setup).
M.config = nil
--- Configure the plugin. `opts.socket` overrides the daemon socket path;
--- `opts.keymaps = false` disables the default keymaps. Idempotent.
function M.setup(opts)
local cfg = vim.tbl_deep_extend("force", config.defaults, opts or {})
cfg.socket = config.resolve_socket(cfg.socket)
cfg.db = config.resolve_db(cfg.db)
M.config = cfg
local rpc = require("heph.rpc")
local daemon = require("heph.daemon")
rpc.setup(cfg.socket)
if cfg.autostart then
-- Plug-and-play: bring up a managed local daemon if none is serving, and
-- self-heal a dropped connection on later calls.
local ok = pcall(daemon.ensure, {
socket = cfg.socket,
db = cfg.db,
bin = cfg.bin,
autostart = true,
})
if not ok then
require("heph.util").notify(
"could not start hephd; will retry on first use",
vim.log.levels.WARN
)
end
rpc.set_respawn(function()
pcall(daemon.ensure, {
socket = cfg.socket,
db = cfg.db,
bin = cfg.bin,
autostart = true,
})
end)
else
-- Explicit architecture: connect only, never spawn over the user's daemon.
rpc.set_respawn(nil)
if not daemon.ensure({ socket = cfg.socket, autostart = false }) then
require("heph.util").notify(
"no hephd reachable at " .. cfg.socket .. " (autostart disabled)",
vim.log.levels.WARN
)
end
end
config.apply_keymaps(cfg)
return M
end
return M