Skip to content

Commit

Permalink
Merge pull request #159 from benlubas/fix/update_inline_images
Browse files Browse the repository at this point in the history
feat: track col position for extmarks
  • Loading branch information
3rd authored Apr 24, 2024
2 parents 584da61 + d24d498 commit 2d4b479
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
22 changes: 13 additions & 9 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local magick = require("image/magick")
local renderer = require("image/renderer")
local utils = require("image/utils")

-- { ["buf:row"]: { id, height } }
-- { ["buf:row:col"]: { id, height } }
---@type table<string, { id: number, height: number }>
local buf_extmark_map = {}

Expand All @@ -29,15 +29,18 @@ end
---get the extmark id for the virtual padding for this image
---@return number?
function Image:get_extmark_id()
local extmark = buf_extmark_map[self.buffer .. ":" .. self.geometry.y]
local extmark = buf_extmark_map[self.buffer .. ":" .. self.geometry.y .. ":" .. self.geometry.x]
if extmark then return extmark.id end
end

function Image:has_extmark_moved()
if not self.extmark then return false end
local extmark =
vim.api.nvim_buf_get_extmark_by_id(self.buffer, self.global_state.extmarks_namespace, self.extmark.id, {})
return extmark and extmark[1] ~= self.extmark.row, extmark and extmark[1] or nil
vim.api.nvim_buf_get_extmark_by_id(self.buffer, self.global_state.extmarks_namespace, self.extmark.id, {})
if extmark then
local moved = extmark[1] ~= self.extmark.row or extmark[2] ~= self.extmark.col
return moved, extmark[1], extmark[2]
end
end

---@param geometry? ImageGeometry
Expand Down Expand Up @@ -71,9 +74,10 @@ function Image:render(geometry)
-- virtual padding
if was_rendered and self.buffer and self.inline then
local row = self.geometry.y
local col = self.geometry.x
local height = self.rendered_geometry.height

local extmark_key = self.buffer .. ":" .. row
local extmark_key = self.buffer .. ":" .. row .. ":" .. col
local previous_extmark = buf_extmark_map[extmark_key]

-- create extmark
Expand All @@ -97,8 +101,8 @@ function Image:render(geometry)
end

-- utils.debug(("(image.render) creating extmark %s"):format(self.internal_id))
local extmark_row = row > 0 and row - 1 or 0
local extmark_col = self.geometry.x >= 0 and self.geometry.x or 0
local extmark_row = math.max(row or 0, 0)
local extmark_col = math.max(col or 0, 0)
local ok, extmark_id = pcall(
vim.api.nvim_buf_set_extmark,
self.buffer,
Expand All @@ -109,7 +113,7 @@ function Image:render(geometry)
)
if ok then
buf_extmark_map[extmark_key] = { id = self.internal_id, height = height or 0 }
self.extmark = { id = extmark_id, row = extmark_row }
self.extmark = { id = extmark_id, row = extmark_row, col = extmark_col }
end
end
end
Expand Down Expand Up @@ -149,7 +153,7 @@ function Image:clear(shallow)
if vim.api.nvim_buf_is_valid(self.buffer) then
vim.api.nvim_buf_del_extmark(self.buffer, self.global_state.extmarks_namespace, self.internal_id)
end
buf_extmark_map[self.buffer .. ":" .. self.geometry.y] = nil
buf_extmark_map[self.buffer .. ":" .. self.geometry.y .. ":" .. self.geometry.x] = nil
end
end

Expand Down
11 changes: 7 additions & 4 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,17 @@ api.setup = function(options)
end

-- sync with extmarks
vim.api.nvim_create_autocmd({ "BufWritePost", "TextChanged", "TextChangedI" }, {
vim.api.nvim_create_autocmd({ "BufWritePost", "TextChanged", "TextChangedI", "InsertEnter" }, {
group = group,
callback = function(event)
local images = api.get_images({ buffer = event.buf })
for _, img in ipairs(images) do
local has_moved, extmark_y = img:has_extmark_moved()
if has_moved and extmark_y ~= nil then
img.geometry.y = extmark_y + 1
local has_moved, extmark_y, extmark_x = img:has_extmark_moved()
if has_moved and extmark_x and extmark_y then
img.geometry.y = extmark_y
img.geometry.x = extmark_x
img.extmark.col = extmark_x
img.extmark.row = extmark_y
img:render()
end
end
Expand Down
5 changes: 5 additions & 0 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ local render = function(image)

local absolute_x = original_x + x_offset + window_offset_x
local absolute_y = original_y + y_offset + window_offset_y

if image.with_virtual_padding then
absolute_y = absolute_y + 1
end

local prevent_rendering = false

-- utils.debug(("(4) x: %d, y: %d, width: %d, height: %d y_offset: %d absolute_x: %d absolute_y: %d"):format( original_x, original_y, width, height, y_offset, absolute_x, absolute_y))
Expand Down
2 changes: 1 addition & 1 deletion lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ local create_document_integration = function(config)
local render_image = function(image)
image:render({
x = item.match.range.start_col,
y = item.match.range.start_row + 1,
y = item.match.range.start_row,
})
end

Expand Down
2 changes: 1 addition & 1 deletion lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
---@field saturation fun(self: Image, saturation: number)
---@field hue fun(self: Image, hue: number)
---@field namespace? string
---@field extmark? { id: number, row: number }
---@field extmark? { id: number, row: number, col: number }

-- wish proper generics were a thing here
---@class IntegrationContext
Expand Down

0 comments on commit 2d4b479

Please sign in to comment.