(renderer) add h.<Highlight>(..) shorthand
All checks were successful
NeoVim tests / plenary-tests (push) Successful in 10s

This commit is contained in:
Jonathan Apodaca 2025-04-20 22:27:58 -06:00
parent f8056fe2e6
commit 7ad3d941f4
2 changed files with 33 additions and 28 deletions

View File

@ -75,7 +75,7 @@ function Buffer:autocmd(event, opts)
vim.api.nvim_create_autocmd(event, vim.tbl_extend('force', opts, { buffer = self.bufnr }))
end
--- @param tree Tree
--- @param tree u.renderer.Tree
function Buffer:render(tree) return self.renderer:render(tree) end
return Buffer

View File

@ -1,22 +1,27 @@
local M = {}
local H = {}
--- @alias Tag { kind: 'tag'; name: string, attributes: table<string, unknown>, children: Tree }
--- @alias Node nil | boolean | string | Tag
--- @alias Tree Node | Node[]
--- @alias u.renderer.Tag { kind: 'tag'; name: string, attributes: table<string, unknown>, children: u.renderer.Tree }
--- @alias u.renderer.Node nil | boolean | string | u.renderer.Tag
--- @alias u.renderer.Tree u.renderer.Node | u.renderer.Node[]
--- @param name string
--- @param attributes? table<string, any>
--- @param children? Node | Node[]
--- @return Tag
function M.h(name, attributes, children)
return {
kind = 'tag',
name = name,
attributes = attributes or {},
children = children,
}
end
--- @type table<string, fun(attributes: table<string, any>, children: u.renderer.Tree): u.renderer.Tag> & fun(name: string, attributes: table<string, any>, children: u.renderer.Tree): u.renderer.Tag>
M.h = setmetatable({}, {
__call = function(_, name, attributes, children)
return {
kind = 'tag',
name = name,
attributes = attributes or {},
children = children,
}
end,
__index = function(_, name)
-- vim.print('dynamic hl ' .. name)
return function(attributes, children)
return M.h('text', vim.tbl_deep_extend('force', { hl = name }, attributes), children)
end
end,
})
-- Renderer {{{
--- @alias RendererExtmark { id?: number; start: [number, number]; stop: [number, number]; opts: any; tag: any }
@ -62,8 +67,8 @@ function Renderer.new(bufnr) -- {{{
end -- }}}
--- @param opts {
--- tree: Tree;
--- on_tag?: fun(tag: Tag, start0: [number, number], stop0: [number, number]): any;
--- tree: u.renderer.Tree;
--- on_tag?: fun(tag: u.renderer.Tag, start0: [number, number], stop0: [number, number]): any;
--- }
function Renderer.markup_to_lines(opts) -- {{{
--- @type string[]
@ -82,7 +87,7 @@ function Renderer.markup_to_lines(opts) -- {{{
curr_col1 = 1
end
--- @param node Node
--- @param node u.renderer.Node
local function visit(node) -- {{{
if node == nil or type(node) == 'boolean' then return end
@ -98,7 +103,7 @@ function Renderer.markup_to_lines(opts) -- {{{
-- visit the children:
if Renderer.is_tag_arr(node.children) then
for _, child in
ipairs(node.children --[[@as Node[] ]])
ipairs(node.children --[[@as u.renderer.Node[] ]])
do
-- newlines are not controlled by array entries, do NOT output a line here:
visit(child)
@ -123,11 +128,11 @@ end -- }}}
--- @param opts {
--- tree: string;
--- format_tag?: fun(tag: Tag): string;
--- format_tag?: fun(tag: u.renderer.Tag): string;
--- }
function Renderer.markup_to_string(opts) return table.concat(Renderer.markup_to_lines(opts), '\n') end
--- @param tree Tree
--- @param tree u.renderer.Tree
function Renderer:render(tree) -- {{{
local changedtick = vim.b[self.bufnr].changedtick
if changedtick ~= self.changedtick then
@ -302,7 +307,7 @@ end -- }}}
---
--- @private (private for now)
--- @param pos0 [number; number]
--- @return { extmark: RendererExtmark; tag: Tag; }[]
--- @return { extmark: RendererExtmark; tag: u.renderer.Tag; }[]
function Renderer:get_pos_infos(pos0) -- {{{
local cursor_line0, cursor_col0 = pos0[1], pos0[2]
@ -370,7 +375,7 @@ function Renderer:get_pos_infos(pos0) -- {{{
-- created extmarks in self.curr.extmarks, which also has which tag each
-- extmark is associated with. Cross-reference with that list to get a list
-- of tags that we need to fire events for:
--- @type { extmark: RendererExtmark; tag: Tag }[]
--- @type { extmark: RendererExtmark; tag: u.renderer.Tag }[]
local matching_tags = vim
.iter(intersecting_extmarks)
--- @param ext RendererExtmark
@ -387,7 +392,7 @@ end -- }}}
-- TreeBuilder {{{
--- @class u.TreeBuilder
--- @field private nodes Node[]
--- @field private nodes u.renderer.Node[]
local TreeBuilder = {}
TreeBuilder.__index = TreeBuilder
M.TreeBuilder = TreeBuilder
@ -397,7 +402,7 @@ function TreeBuilder.new()
return self
end
--- @param nodes Tree
--- @param nodes u.renderer.Tree
--- @return u.TreeBuilder
function TreeBuilder:put(nodes)
table.insert(self.nodes, nodes)
@ -406,7 +411,7 @@ end
--- @param name string
--- @param attributes? table<string, any>
--- @param children? Node | Node[]
--- @param children? u.renderer.Node | u.renderer.Node[]
--- @return u.TreeBuilder
function TreeBuilder:put_h(name, attributes, children)
local tag = M.h(name, attributes, children)
@ -423,7 +428,7 @@ function TreeBuilder:nest(fn)
return self
end
--- @return Tree
--- @return u.renderer.Tree
function TreeBuilder:tree() return self.nodes end
-- }}}