generated from eblume/project-template
feat(nvim): frontmatter edit surface — diff block into RPCs on save (§8.3)
Some checks failed
Build / validate (pull_request) Failing after 5m1s
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>
This commit is contained in:
parent
ef56c5d5f2
commit
0e9cfc1fd7
9 changed files with 328 additions and 23 deletions
|
|
@ -1,26 +1,34 @@
|
|||
--- 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`; `: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).
|
||||
--- 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 = {}
|
||||
|
||||
--- `BufReadCmd` handler for `heph://node/<id>`: load the body into the buffer.
|
||||
-- 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 })
|
||||
local body = (node and node.body) or ""
|
||||
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
|
||||
-- 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 }))
|
||||
-- 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"
|
||||
|
|
@ -28,14 +36,32 @@ function M.read(buf, uri)
|
|||
require("heph.link").attach(buf)
|
||||
end
|
||||
|
||||
--- `BufWriteCmd` handler: persist the whole buffer as the node body.
|
||||
--- `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 lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
|
||||
rpc.call("node.update", { id = id, body = table.concat(lines, "\n") })
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue