add example/form.lua
All checks were successful
NeoVim tests / code-quality (push) Successful in 1m23s

This commit is contained in:
Jonathan Apodaca 2025-06-14 00:19:11 -06:00
parent ad2e579d1d
commit cf896546d7
2 changed files with 48 additions and 0 deletions

43
examples/form.lua Normal file
View File

@ -0,0 +1,43 @@
local h = require('u.renderer').h
local Renderer = require('u.renderer').Renderer
local tracker = require 'u.tracker'
-- Create a new, temporary, buffer to the side:
vim.cmd.vnew()
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'wipe'
vim.bo.buflisted = false
local renderer = Renderer.new()
-- Create two signals:
local s_name = tracker.create_signal '[(name here)]'
local s_age = tracker.create_signal '[(age here)]'
-- Utility to trim brackets from strings:
local function trimb(s) return (s:gsub('^%[(.*)%]$', '%1')) end
-- Render effect that depends on the signals. This will re-run every time
-- one of the signals changes.
tracker.create_effect(function()
local name = s_name:get()
local age = s_age:get()
-- Each time the signals change, we re-render the buffer:
renderer:render {
h.Type({}, '# Form Example'),
'\n\n',
-- Setting the `signal` attribute to a signal will cause the renderer to
-- update the signal's value whenever text in the tag is changed. This lets
-- us setup two-way data-binding between the buffer and the signals.
{ 'Name: ', h.Structure({ signal = s_name }, name) },
'\n',
{ 'Age: ', h.Structure({ signal = s_age }, age) },
'\n\n',
-- Display the current values of the signals. If you change the values in
-- the tags above, you can see the changes reflected here immediately.
{ 'Hello, "', trimb(name), '", you are "', trimb(age), '" years old.' },
}
end)

View File

@ -296,10 +296,15 @@ function Renderer:_reconcile() -- {{{
-- --
-- Step 2: reconcile extmarks: -- Step 2: reconcile extmarks:
-- You may be tempted to try to keep track of which extmarks are needed, and
-- only delete those that are not needed. However, each time a tree is
-- rendered, brand new extmarks are created. For simplicity, it is better to
-- just delete all extmarks, and recreate them.
-- --
-- Clear current extmarks: -- Clear current extmarks:
vim.api.nvim_buf_clear_namespace(self.bufnr, self.ns, 0, -1) vim.api.nvim_buf_clear_namespace(self.bufnr, self.ns, 0, -1)
-- Set current extmarks: -- Set current extmarks:
for _, extmark in ipairs(self.curr.extmarks) do for _, extmark in ipairs(self.curr.extmarks) do
extmark.id = vim.api.nvim_buf_set_extmark( extmark.id = vim.api.nvim_buf_set_extmark(