From cf896546d7ea6d8d5dfa4718b2a58ff87fcbb74c Mon Sep 17 00:00:00 2001 From: Jonathan Apodaca Date: Sat, 14 Jun 2025 00:19:11 -0600 Subject: [PATCH] add example/form.lua --- examples/form.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ lua/u/renderer.lua | 5 +++++ 2 files changed, 48 insertions(+) create mode 100644 examples/form.lua diff --git a/examples/form.lua b/examples/form.lua new file mode 100644 index 0000000..e02db52 --- /dev/null +++ b/examples/form.lua @@ -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) diff --git a/lua/u/renderer.lua b/lua/u/renderer.lua index 8696be8..3c34f50 100644 --- a/lua/u/renderer.lua +++ b/lua/u/renderer.lua @@ -296,10 +296,15 @@ function Renderer:_reconcile() -- {{{ -- -- 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: vim.api.nvim_buf_clear_namespace(self.bufnr, self.ns, 0, -1) + -- Set current extmarks: for _, extmark in ipairs(self.curr.extmarks) do extmark.id = vim.api.nvim_buf_set_extmark(