--- Buffer-backed nodes (tech-spec ยง8): a node's markdown body is edited in a --- real buffer named `heph://node/`. `:e` loads it via `node.get`; `:w` --- saves the whole buffer back via `node.update` (the backend CRDT-diffs the --- whole-buffer text, so sending the full body is correct and idempotent). local rpc = require("heph.rpc") local util = require("heph.util") local M = {} --- `BufReadCmd` handler for `heph://node/`: load the body into the buffer. function M.read(buf, uri) local _, id = util.parse_uri(uri) if not id then error("heph: not a node uri: " .. tostring(uri)) end local node = rpc.call("node.get", { id = id }) local body = (node and node.body) or "" -- `plain` split keeps a trailing "" element for a trailing newline, so the -- body round-trips exactly through `table.concat` on write. vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(body, "\n", { plain = true })) vim.b[buf].heph_node_id = id vim.b[buf].heph_node_kind = (node and node.kind) or "doc" vim.bo[buf].buftype = "acwrite" -- written via BufWriteCmd, not to a file vim.bo[buf].filetype = "markdown" vim.bo[buf].fileformat = "unix" vim.bo[buf].modified = false require("heph.link").attach(buf) end --- `BufWriteCmd` handler: persist the whole buffer as the node body. function M.write(buf, _uri) local id = vim.b[buf].heph_node_id if not id then error("heph: buffer has no heph node id") end local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) rpc.call("node.update", { id = id, body = table.concat(lines, "\n") }) vim.bo[buf].modified = false end --- Open (or focus) the buffer for node `id`. function M.open(id) vim.cmd.edit(util.node_uri(id)) end --- Force-reload the buffer for node `id` from the daemon (discards local edits). function M.reload(id) vim.cmd("edit! " .. vim.fn.fnameescape(util.node_uri(id))) end return M