Skip to content

Commit

Permalink
fix: bug causing double extmark clearing weirdness
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Oct 17, 2023
1 parent ae351ca commit a3e0b9f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
32 changes: 13 additions & 19 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,19 @@ function Image:render(geometry)

-- utils.debug( ("[image] render: %s, success: %s x: %s, y: %s, width: %s, height: %s"):format( self.id, was_rendered, self.geometry.x, self.geometry.y, self.geometry.width, self.geometry.height))

-- clear if render was prevented
if self.is_rendered and not was_rendered then self.global_state.backend.clear(self.id, true) end
-- clear if already rendered but rendering this should be prevented
if self.is_rendered and not was_rendered then
self.global_state.backend.clear(self.id, true)
return
end

-- virtual padding
if self.buffer and self.with_virtual_padding then
if was_rendered and self.buffer and self.with_virtual_padding then
local row = self.geometry.y
local width = self.rendered_geometry.width or 1
local height = self.rendered_geometry.height or 1

local previous_extmark = buf_extmark_map[self.buffer .. ":" .. row]
local height = self.rendered_geometry.height

-- clear previous extmark if rendering was prevented
if not was_rendered and previous_extmark then
-- utils.debug(("(image.render) clearing extmark %s"):format(previous_extmark.id))
if vim.api.nvim_buf_is_valid(self.buffer) then
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, previous_extmark.id)
end
buf_extmark_map[self.buffer .. ":" .. row] = nil
return
end
local extmark_key = self.buffer .. ":" .. row
local previous_extmark = buf_extmark_map[extmark_key]

-- create extmark
if was_rendered then
Expand All @@ -64,12 +57,12 @@ function Image:render(geometry)
if previous_extmark ~= nil then
-- utils.debug(("(image.render) clearing extmark %s"):format(previous_extmark.id))
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, previous_extmark.id)
buf_extmark_map[extmark_key] = nil
end

local text = string.rep(" ", width)
local filler = {}
for _ = 0, height - 1 do
filler[#filler + 1] = { { text, "" } }
filler[#filler + 1] = { { " ", "" } }
end

-- utils.debug(("(image.render) creating extmark %s"):format(self.internal_id))
Expand All @@ -84,7 +77,7 @@ function Image:render(geometry)
virt_lines = filler,
}
)
if ok then buf_extmark_map[self.buffer .. ":" .. row] = { id = self.internal_id, height = height } end
if ok then buf_extmark_map[extmark_key] = { id = self.internal_id, height = height } end
end
end

Expand Down Expand Up @@ -115,6 +108,7 @@ end
function Image:clear(shallow)
-- utils.debug(("[image] clear %s, shallow: %s"):format(self.id, shallow))
self.global_state.backend.clear(self.id, shallow or false)

self.rendered_geometry = {
x = nil,
y = nil,
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 @@ -24,6 +24,7 @@ return document.create_document_integration({
local key = query.captures[id]
local value = vim.treesitter.get_node_text(node, buf)

-- TODO: fix node:range() taking into account the extmarks for SOME FKING REASON
if key == "image" then
local start_row, start_col, end_row, end_col = node:range()
current_image = {
Expand Down
2 changes: 1 addition & 1 deletion lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ local render = function(image)
else
state.images[image.id] = image
end
return true
return false
end

-- compute final geometry and prevent useless rerendering
Expand Down

0 comments on commit a3e0b9f

Please sign in to comment.