u.nvim/lua/u/renderer.lua
Jonathan Apodaca 9db70c4c10
Some checks failed
NeoVim tests / plenary-tests (push) Failing after 10s
experiment with hyperscript impl: callbacks work!
2025-02-26 22:26:16 -07:00

319 lines
11 KiB
Lua

local utils = require 'u.utils'
--- @alias Tag { kind: 'tag'; name: string, attributes: table<string, unknown>, children: Tree }
--- @alias Node nil | boolean | string | Tag
--- @alias Tree Node | Node[]
local TagMetaTable = {}
--------------------------------------------------------------------------------
-- Renderer class
--------------------------------------------------------------------------------
--- @alias RendererHighlight { group: string; start: [number, number]; stop: [number, number ] }
--- @class Renderer
--- @field bufnr number
--- @field ns number
--- @field changedtick number
--- @field old { lines: string[]; hls: RendererHighlight[] }
--- @field curr { lines: string[]; hls: RendererHighlight[] }
--- @field tag_infos { tag:Tag[]; start0: [number, number]; stop0: [number, number] }[]
local Renderer = {}
Renderer.__index = Renderer
--- function h(name: string, attributes: table<string, unknown>, children: Node | Node[]): Node
--- @param name string
--- @param attributes? table<string, any>
--- @param children? Node | Node[]
--- @return Tag
function Renderer.h(name, attributes, children)
return setmetatable({
kind = 'tag',
name = name,
attributes = attributes or {},
children = children,
}, TagMetaTable)
end
--- @param x any
--- @return boolean
function Renderer.is_tag(x) return type(x) == 'table' and getmetatable(x) == TagMetaTable end
--- @param x any
--- @return boolean
function Renderer.is_tag_arr(x)
if type(x) ~= 'table' then return false end
return #x == 0 or not Renderer.is_tag(x)
end
--- @param bufnr number|nil
function Renderer.new(bufnr)
if bufnr == nil then bufnr = vim.api.nvim_get_current_buf() end
if vim.b[bufnr]._renderer_ns == nil then vim.b[bufnr]._renderer_ns = vim.api.nvim_create_namespace '' end
local self = setmetatable({
bufnr = bufnr,
ns = vim.b[bufnr]._renderer_ns,
changedtick = 0,
old = { lines = {}, hls = {} },
curr = { lines = {}, hls = {} },
}, Renderer)
return self
end
--- @param opts {
--- tree: Tree;
--- on_tag?: fun(tag: Tag, start0: [number, number], stop0: [number, number]): any;
--- }
function Renderer.markup_to_lines(opts)
--- @type string[]
local lines = {}
local curr_line1 = 1
local curr_col1 = 1 -- exclusive: sits one position **beyond** the last inserted text
--- @param s string
local function put(s)
lines[curr_line1] = (lines[curr_line1] or '') .. s
curr_col1 = #lines[curr_line1] + 1
end
local function put_line()
table.insert(lines, '')
curr_line1 = curr_line1 + 1
curr_col1 = 1
end
--- @param node Node
local function visit(node)
if node == nil or type(node) == 'boolean' then return end
if type(node) == 'string' then
local node_lines = vim.split(node, '\n')
for lnum, s in ipairs(node_lines) do
if lnum > 1 then put_line() end
put(s)
end
elseif Renderer.is_tag(node) then
local start0 = { curr_line1 - 1, curr_col1 - 1 }
-- visit the children:
if Renderer.is_tag_arr(node.children) then
for _, child in ipairs(node.children) do
-- newlines are not controlled by array entries, do NOT output a line here:
visit(child)
end
else
visit(node.children)
end
local stop0 = { curr_line1 - 1, curr_col1 - 1 }
if opts.on_tag then opts.on_tag(node, start0, stop0) end
elseif Renderer.is_tag_arr(node) then
for _, child in ipairs(node) do
-- newlines are not controlled by array entries, do NOT output a line here:
visit(child)
end
end
end
visit(opts.tree)
return lines
end
--- @param opts {
--- tree: string;
--- format_tag?: fun(tag: Tag): string;
--- }
function Renderer.markup_to_string(opts) return table.concat(Renderer.markup_to_lines(opts), '\n') end
--- @param tree Tree
function Renderer:render(tree)
local changedtick = vim.b[self.bufnr].changedtick
if changedtick ~= self.changedtick then
self.curr = { lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false), hls = {} }
self.changedtick = changedtick
end
--- @type RendererHighlight[]
local hls = {}
--- @type { tag:Tag[]; start0: [number, number]; stop0: [number, number] }[]
local tag_infos = {}
--- @type string[]
local lines = Renderer.markup_to_lines {
tree = tree,
on_tag = function(tag, start0, stop0)
table.insert(tag_infos, { tag = tag, start0 = start0, stop0 = stop0 })
local function attr_num(nm)
if not tag.attributes[nm] then return end
return tonumber(tag.attributes[nm])
end
local function attr_bool(nm)
if not tag.attributes[nm] then return end
return tag.attributes[nm] and true or false
end
if tag.name == 'text' then
local group = tag.attributes.hl
if type(group) == 'string' then
table.insert(hls, { group = group, start = start0, stop = stop0 })
local local_exists = #vim.tbl_keys(vim.api.nvim_get_hl(self.ns, { name = group })) > 0
local global_exists = #vim.tbl_keys(vim.api.nvim_get_hl(0, { name = group }))
local exists = local_exists or global_exists
if not exists or attr_bool 'hl:force' then
vim.api.nvim_set_hl_ns(self.ns)
vim.api.nvim_set_hl(self.ns, group, {
fg = tag.attributes['hl:fg'],
bg = tag.attributes['hl:bg'],
sp = tag.attributes['hl:sp'],
blend = attr_num 'hl:blend',
bold = attr_bool 'hl:bold',
standout = attr_bool 'hl:standout',
underline = attr_bool 'hl:underline',
undercurl = attr_bool 'hl:undercurl',
underdouble = attr_bool 'hl:underdouble',
underdotted = attr_bool 'hl:underdotted',
underdashed = attr_bool 'hl:underdashed',
strikethrough = attr_bool 'hl:strikethrough',
italic = attr_bool 'hl:italic',
reverse = attr_bool 'hl:reverse',
nocombine = attr_bool 'hl:nocombine',
link = tag.attributes['hl:link'],
default = attr_bool 'hl:default',
ctermfg = attr_num 'hl:ctermfg',
ctermbg = attr_num 'hl:ctermbg',
cterm = tag.attributes['hl:cterm'],
force = attr_bool 'hl:force',
})
end
end
end
end,
}
self.old = self.curr
self.curr = { lines = lines, hls = hls }
self.tag_infos = tag_infos
self:_reconcile()
end
--- @private
--- @param info string
--- @param start integer
--- @param end_ integer
--- @param strict_indexing boolean
--- @param replacement string[]
function Renderer:_set_lines(info, start, end_, strict_indexing, replacement)
self:_log { 'set_lines', self.bufnr, start, end_, strict_indexing, replacement }
vim.api.nvim_buf_set_lines(self.bufnr, start, end_, strict_indexing, replacement)
self:_log { 'after(' .. info .. ')', vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) }
end
--- @private
--- @param info string
--- @param start_row integer
--- @param start_col integer
--- @param end_row integer
--- @param end_col integer
--- @param replacement string[]
function Renderer:_set_text(info, start_row, start_col, end_row, end_col, replacement)
self:_log { 'set_text', self.bufnr, start_row, start_col, end_row, end_col, replacement }
vim.api.nvim_buf_set_text(self.bufnr, start_row, start_col, end_row, end_col, replacement)
self:_log { 'after(' .. info .. ')', vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) }
end
--- @private
function Renderer:_log(...)
--
-- vim.print(...)
end
--- @private
function Renderer:_reconcile()
local line_changes = utils.levenshtein(self.old.lines, self.curr.lines)
self.old = self.curr
self:_log { line_changes = line_changes }
for _, line_change in ipairs(line_changes) do
local lnum0 = line_change.index - 1
if line_change.kind == 'add' then
self:_set_lines('add-line', lnum0, lnum0, true, { line_change.item })
elseif line_change.kind == 'change' then
-- Compute inter-line diff, and apply:
self:_log '--------------------------------------------------------------------------------'
local col_changes = utils.levenshtein(vim.split(line_change.from, ''), vim.split(line_change.to, ''))
for _, col_change in ipairs(col_changes) do
local cnum0 = col_change.index - 1
self:_log { line_change = col_change, cnum = cnum0, lnum = lnum0 }
if col_change.kind == 'add' then
self:_set_text('add-char', lnum0, cnum0, lnum0, cnum0, { col_change.item })
elseif col_change.kind == 'change' then
self:_set_text('change-char', lnum0, cnum0, lnum0, cnum0 + 1, { col_change.to })
elseif col_change.kind == 'delete' then
self:_set_text('del-char', lnum0, cnum0, lnum0, cnum0 + 1, {})
else
-- No change
end
end
elseif line_change.kind == 'delete' then
self:_set_lines('del-line', lnum0, lnum0 + 1, true, {})
else
-- No change
end
end
vim.on_key(nil, self.ns)
vim.on_key(function(key, typed)
-- Discard if not in the current buffer:
if vim.api.nvim_get_current_buf() ~= self.bufnr then return end
-- find the tag with the smallest intersection that contains the cursor:
local pos0 = vim.api.nvim_win_get_cursor(0)
pos0[1] = pos0[1] - 1 -- make it actually 0-based
local cursor_line0, cursor_col0 = unpack(pos0)
-- Find the tag that contains the cursor: If the tag is listening for the
-- key that just fired, call its callback:
for _, tag_info in ipairs(self.tag_infos) do
local tag = tag_info.tag
local start0, stop0 = tag_info.start0, tag_info.stop0
if
-- line:
start0[1] <= cursor_line0
and stop0[1] >= cursor_line0
-- column:
and start0[2] <= cursor_col0
and stop0[2] > cursor_col0
then
-- is the tag listening?
if tag.attributes.on_key and type(tag.attributes.on_key[key]) == 'function' then
-- key:
return tag.attributes.on_key[key]()
elseif tag.attributes.on_typed and type(tag.attributes.on_typed[typed]) == 'function' then
-- typed:
return tag.attributes.on_typed[typed]()
end
end
end
end, self.ns)
-- vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, self.curr.lines)
vim.api.nvim_buf_clear_namespace(self.bufnr, self.ns, 0, -1)
for _, hl in ipairs(self.curr.hls) do
(vim.hl or vim.highlight).range(self.bufnr, self.ns, hl.group, hl.start, hl.stop, {
inclusive = false,
priority = (vim.hl or vim.highlight).priorities.user,
regtype = 'v',
})
end
self.changedtick = vim.b[self.bufnr].changedtick
end
return Renderer