This commit is contained in:
parent
ad2e579d1d
commit
4e10e1e799
43
examples/form.lua
Normal file
43
examples/form.lua
Normal 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)
|
@ -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(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user