From d24d498cdb020bb87d629263e89f25767b6c73c9 Mon Sep 17 00:00:00 2001 From: benlubas Date: Wed, 24 Apr 2024 13:19:43 -0400 Subject: [PATCH] feat: track col position for extmarks --- lua/image/image.lua | 22 +++++++++++++--------- lua/image/init.lua | 11 +++++++---- lua/image/renderer.lua | 5 +++++ lua/image/utils/document.lua | 2 +- lua/types.lua | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lua/image/image.lua b/lua/image/image.lua index 9c8edf9..ff38c85 100644 --- a/lua/image/image.lua +++ b/lua/image/image.lua @@ -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 local buf_extmark_map = {} @@ -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 @@ -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 @@ -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, @@ -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 @@ -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 diff --git a/lua/image/init.lua b/lua/image/init.lua index 2d943a3..35b98fc 100644 --- a/lua/image/init.lua +++ b/lua/image/init.lua @@ -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 diff --git a/lua/image/renderer.lua b/lua/image/renderer.lua index 0f8c26b..00b2fb1 100644 --- a/lua/image/renderer.lua +++ b/lua/image/renderer.lua @@ -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)) diff --git a/lua/image/utils/document.lua b/lua/image/utils/document.lua index e548d45..20633f9 100644 --- a/lua/image/utils/document.lua +++ b/lua/image/utils/document.lua @@ -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 diff --git a/lua/types.lua b/lua/types.lua index d2905c1..53f5603 100644 --- a/lua/types.lua +++ b/lua/types.lua @@ -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