Skip to content

Commit

Permalink
feat(latex): async image rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
benlubas authored and vhyrro committed May 18, 2024
1 parent a94d7a1 commit b1c96a0
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 149 deletions.
20 changes: 13 additions & 7 deletions lua/neorg/modules/core/highlights/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ module.config.public = {
link = "+NonText",
escape = "+@type",
},

-- Rendered Latex, this will dictate the foreground color of latex images rendered via
-- core.latex.renderer
rendered = {
latex = "+Normal",
}
},

-- Handles the dimming of certain highlight groups.
Expand Down Expand Up @@ -593,22 +599,22 @@ module.public = {
return "NONE"
end,

hex_to_rgb = function(hex_colour)
return tonumber(hex_colour:sub(1, 2), 16),
tonumber(hex_colour:sub(3, 4), 16),
tonumber(hex_colour:sub(5), 16)
end,

dim_color = function(colour, percent)
if colour == "NONE" then
return colour
end

local function hex_to_rgb(hex_colour)
return tonumber(hex_colour:sub(1, 2), 16),
tonumber(hex_colour:sub(3, 4), 16),
tonumber(hex_colour:sub(5), 16)
end

local function alter(attr)
return math.floor(attr * (100 - percent) / 100)
end

local r, g, b = hex_to_rgb(colour)
local r, g, b = module.public.hex_to_rgb(colour)

if not r or not g or not b then
return "NONE"
Expand Down
38 changes: 20 additions & 18 deletions lua/neorg/modules/core/integrations/image/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,42 @@ module.private = {

module.public = {
new_image = function(buffernr, png_path, position, window, scale, virtual_padding)
local geometry = {
x = position.column_start,
y = position.row_start + (virtual_padding and 1 or 0),
width = position.column_end - position.column_start,
height = scale,
}
local image = require("image").from_file(png_path, {
window = window,
buffer = buffernr,
inline = true, -- let image.nvim track the position for us
with_virtual_padding = virtual_padding,
x = position.column_start,
y = position.row_start + (virtual_padding and 1 or 0),
width = position.column_end - position.column_start,
height = scale,
})
image:render(geometry)
end,
get_images = function()
return (require("image").get_images())
-- image:render(geometry)
return image
end,
---Render an image or list of images
---@param images any[]
render = function(images)
for _, image in pairs(images) do
image:clear()
image:render()
for _, limage in pairs(images) do
limage.image:clear()
limage.image:render()
end
end,
clear = function()
local images = module.public.get_images()
for _, image in pairs(images) do
image:clear()
clear = function(images)
for _, limage in pairs(images) do
limage.image:clear()
end
end,
clear_at_cursor = function(images, row)
for _, image in pairs(images) do
local cleared = {}
for id, limage in pairs(images) do
local image = limage.image
if image.geometry.y == row then
image:clear()
table.insert(cleared, id)
end
end
return cleared
end,
}

Expand Down
68 changes: 46 additions & 22 deletions lua/neorg/modules/core/integrations/treesitter/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ module.public = {
end
end,
--- Gets all nodes of a given type from the AST
---@param type string #The type of node to filter out
---@param node_type string #The type of node to filter out
---@param opts? table #A table of two options: `buf` and `ft`, for the buffer and format to use respectively.
get_all_nodes = function(type, opts)
get_all_nodes = function(node_type, opts)
local result = {}
opts = opts or {}

Expand All @@ -231,29 +231,46 @@ module.public = {

-- Do we need to go through each tree? lol
vim.treesitter.get_parser(opts.buf, opts.ft):for_each_tree(function(tree)
-- Get the root for that tree
---@type TSNode
local root = tree:root()
table.insert(result, module.public.search_tree(tree, node_type))
end)

--- Recursively searches for a node of a given type
---@param node userdata #The starting point for the search
local function descend(node)
-- Iterate over all children of the node and try to match their type
for child, _ in node:iter_children() do ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
if child:type() == type then
table.insert(result, child)
else
-- If no match is found try descending further down the syntax tree
for _, child_node in ipairs(descend(child) or {}) do
table.insert(result, child_node)
end
return vim.tbl_flatten(result)
end,
--- Gets all nodes of a given type from the AST
---@param node_type string #The type of node to filter out
---@param path string path to the file to parse
---@param filetype string? file type of the file or `norg` if omitted
get_all_nodes_in_file = function(node_type, path, filetype)
path = vim.fs.normalize(path)
if not filetype then filetype = "norg" end

local contents = io.open(path, "r"):read("*a")
local tree = vim.treesitter.get_string_parser(contents, filetype):parse()[1]
if not (tree or tree.root) then return {} end

return module.public.search_tree(tree, node_type)
end,
search_tree = function(tree, node_type)
local result = {}
local root = tree:root()

--- Recursively searches for a node of a given type
---@param node userdata #The starting point for the search
local function descend(node)
-- Iterate over all children of the node and try to match their type
for child, _ in node:iter_children() do ---@diagnostic disable-line -- TODO: type error workaround <pysan3>
if child:type() == node_type then
table.insert(result, child)
else
-- If no match is found try descending further down the syntax tree
for _, child_node in ipairs(descend(child) or {}) do
table.insert(result, child_node)
end
end
end
end

descend(root)
end)

descend(root)
return result
end,
--- Executes function callback on each child node of the root
Expand Down Expand Up @@ -288,11 +305,18 @@ module.public = {
descend(root)
end,
get_node_text = function(node, source)
source = source or 0

if not node then return "" end
local start_row, start_col = node:start()
local end_row, end_col = node:end_()

-- when source is the string contents of the file
if type(source) == "string" then
local _, _, start_bytes = node:start()
local _, _, end_bytes = node:end_()
return string.sub(source, start_bytes, end_bytes)
end

source = source or 0
local eof_row = vim.api.nvim_buf_line_count(source)

if end_row >= eof_row then
Expand Down
Loading

0 comments on commit b1c96a0

Please sign in to comment.