Skip to content

Commit

Permalink
fix: fix images not working if initially inside folds & extmark tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 7, 2023
1 parent 4c3de82 commit 03f4eb7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
32 changes: 18 additions & 14 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,26 @@ function Image:render(geometry)
return
end

-- create extmark if outdated or it doesn't exist
if was_rendered and previous_extmark then
if previous_extmark.height == height then return end
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, previous_extmark.id)
end
-- create extmark
if was_rendered then
local text = string.rep(" ", width)
local filler = {}
for _ = 0, height - 1 do
filler[#filler + 1] = { { text, "" } }
local has_up_to_date_extmark = previous_extmark and previous_extmark.height == height

if not has_up_to_date_extmark then
if previous_extmark ~= nil then
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, previous_extmark.id)
end

local text = string.rep(" ", width)
local filler = {}
for _ = 0, height - 1 do
filler[#filler + 1] = { { text, "" } }
end
vim.api.nvim_buf_set_extmark(self.buffer, self.global_state.extmarks_namespace, row > 0 and row - 1 or 0, 0, {
id = self.internal_id,
virt_lines = filler,
})
buf_extmark_map[self.buffer .. ":" .. row] = { id = self.internal_id, height = height }
end
vim.api.nvim_buf_set_extmark(self.buffer, self.global_state.extmarks_namespace, row > 0 and row - 1 or 0, 0, {
id = self.internal_id,
virt_lines = filler,
})
buf_extmark_map[self.buffer .. ":" .. row] = { id = self.internal_id, height = height }
end

-- TODO: chain rerendering only the next affected image after this one
Expand Down
1 change: 1 addition & 0 deletions lua/image/integrations/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local document = require("image/utils/document")

return document.create_document_integration({
name = "markdown",
-- debug = true,
default_options = {
clear_in_insert_mode = false,
download_remote_images = true,
Expand Down
1 change: 1 addition & 0 deletions lua/image/integrations/syslang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local document = require("image/utils/document")

return document.create_document_integration({
name = "syslang",
-- debug = true,
default_options = {
clear_in_insert_mode = false,
download_remote_images = true,
Expand Down
11 changes: 9 additions & 2 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ local render = function(image)
local image_rows = math.floor(image.image_height / term_size.cell_height)
local image_columns = math.floor(image.image_width / term_size.cell_width)

-- utils.debug(("renderer.render() %s"):format(image.original_path))

local original_x = image.geometry.x or 0
local original_y = image.geometry.y or 0
local x_offset = 0
Expand Down Expand Up @@ -114,7 +116,10 @@ local render = function(image)
with_masks = state.options.window_overlap_clear_enabled,
ignore_masking_filetypes = state.options.window_overlap_clear_ft_ignore,
})
if window == nil then return false end
if window == nil then
utils.debug("invalid window", image.id)
return false
end

-- bail if the window is not visible
if not window.is_visible then return false end
Expand All @@ -134,7 +139,9 @@ local render = function(image)

-- bail if the image is inside a fold
if image.buffer and is_folded then
-- utils.debug("inside fold", image.id)
-- utils.debug("image is inside a fold", image.id)
state.images[image.id] = image
image:clear(true)
return false
end

Expand Down
4 changes: 3 additions & 1 deletion lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ local create_document_integration = function(config)

---@param image Image
local render_image = function(image)
trace("rendering image %s at x=%d y=%d", match.url, match.range.start_col, match.range.start_row + 1)
trace(
("rendering image %s at x=%d y=%d"):format(match.url, match.range.start_col, match.range.start_row + 1)
)

image:render({
height = height,
Expand Down

0 comments on commit 03f4eb7

Please sign in to comment.