generated from eblume/project-template
Some checks failed
Build / validate (pull_request) Failing after 5m1s
Node buffers now open with the editable YAML frontmatter block on top
(node.get {frontmatter: true}); on :w, `frontmatter.lua` parses the
block, diffs it against what was rendered, and routes each changed field
to the right RPC:
- title → node.update rename
- attention → task.set_attention
- do_date/late_on/recurrence → task.set_schedule (YYYY-MM-DD → local-ms;
a removed line clears via null)
- project → task.set_project (resolved by name)
- tags → tag.add / tag.remove
A mistyped state surfaces the daemon's validation error; a buffer with no
block edits no metadata (deleting the block can't wipe tags). Body rides
node.update as before (the store strips any echoed frontmatter).
Body-position features are content-relative, so the prepended block
doesn't disturb them; e2e specs that targeted absolute line 1 now locate
body lines by content via a new `h.find` helper. New frontmatter_spec
covers render + the full diff→RPC round-trip. 21 nvim e2e specs green.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
78 lines
2.8 KiB
Lua
78 lines
2.8 KiB
Lua
--- Buffer-backed nodes (tech-spec §8): a node's markdown body is edited in a
|
|
--- real buffer named `heph://node/<id>`. `:e` loads it via `node.get`
|
|
--- (with the editable YAML **frontmatter** block prepended, §8.3); `:w` diffs
|
|
--- the frontmatter into structured RPCs, then saves the body via `node.update`
|
|
--- (the backend CRDT-diffs the whole body, and strips any frontmatter echoed
|
|
--- back — so sending the stripped body is correct and idempotent).
|
|
|
|
local rpc = require("heph.rpc")
|
|
local util = require("heph.util")
|
|
local frontmatter = require("heph.frontmatter")
|
|
|
|
local M = {}
|
|
|
|
-- buf -> the frontmatter table rendered on read, so `write` can diff against it.
|
|
M._canonical = {}
|
|
|
|
--- `BufReadCmd` handler for `heph://node/<id>`: load frontmatter + body.
|
|
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, frontmatter = true })
|
|
local text = (node and node.body) or ""
|
|
local fm = frontmatter.parse(text)
|
|
-- `plain` split keeps a trailing "" element for a trailing newline, so the
|
|
-- text round-trips exactly through `table.concat` on write.
|
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, vim.split(text, "\n", { plain = true }))
|
|
vim.b[buf].heph_node_id = id
|
|
vim.b[buf].heph_node_kind = (node and node.kind) or "doc"
|
|
M._canonical[buf] = fm or {}
|
|
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: route frontmatter edits to RPCs, then save the 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 text = table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
|
|
local fm, body = frontmatter.parse(text)
|
|
|
|
local params = { id = id }
|
|
if fm then
|
|
-- A frontmatter block is present: translate its edits to RPCs. (Absent
|
|
-- block ⇒ the user removed it; treat the whole buffer as body, touch no
|
|
-- metadata.) The backend also strips defensively, so `body` is what stores.
|
|
local canonical = M._canonical[buf] or {}
|
|
frontmatter.apply(id, canonical, fm)
|
|
if fm.title and fm.title ~= canonical.title then
|
|
params.title = fm.title
|
|
end
|
|
params.body = body
|
|
M._canonical[buf] = fm
|
|
else
|
|
params.body = text
|
|
end
|
|
|
|
rpc.call("node.update", params)
|
|
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
|