Skip to content

Commit f329d12

Browse files
committed
feat: add overlap option to reduce virtual padding for overlapping images
When an image overlaps existing buffer lines (e.g. a rendered diagram overlapping its code block), the new `overlap` option specifies how many buffer lines the image covers. Virtual padding is reduced accordingly so content below isn't pushed down unnecessarily. Also simplifies render_offset_top handling to support negative values, allowing images to be shifted upward relative to their anchor position.
1 parent da2be65 commit f329d12

2 files changed

Lines changed: 8 additions & 11 deletions

File tree

lua/image/image.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ function Image:render(geometry)
108108

109109
-- create extmark
110110
if was_rendered then
111-
local total_height = height + (self.render_offset_top or 0)
111+
-- subtract overlap lines to allow the rendered image to overlap with them
112+
local total_height = math.max(0, height + (self.render_offset_top or 0) - (self.overlap or 0) + 1)
112113
local has_up_to_date_extmark = previous_extmark and previous_extmark.height == total_height
113114

114115
if not has_up_to_date_extmark then
@@ -121,8 +122,7 @@ function Image:render(geometry)
121122
local filler = {}
122123
local extmark_opts = { id = self.internal_id, strict = false }
123124
if self.with_virtual_padding then
124-
-- only reserve real height for the extmark, padding is applied during rendering
125-
local total_lines = height
125+
local total_lines = math.max(0, height + (self.render_offset_top or 0) - (self.overlap or 0) + 1)
126126
for _ = 0, total_lines - 1 do
127127
filler[#filler + 1] = { { " ", "" } }
128128
end
@@ -298,6 +298,7 @@ local from_file = function(path, options, state)
298298
inline = opts.inline or opts.with_virtual_padding or false,
299299
is_rendered = false,
300300
render_offset_top = opts.render_offset_top or 0,
301+
overlap = opts.overlap or 0,
301302
crop_hash = nil,
302303
resize_hash = nil,
303304
namespace = opts.namespace or nil,

lua/image/renderer.lua

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ local render = function(image)
194194
if image.window == nil then
195195
absolute_x = original_x
196196
absolute_y = original_y
197-
-- apply render_offset_top
198-
if image.render_offset_top and image.render_offset_top > 0 then
199-
--
200-
absolute_y = absolute_y + image.render_offset_top
201-
end
197+
absolute_y = absolute_y + (image.render_offset_top or 0)
202198
else
203199
-- get window object
204200
local window = nil
@@ -317,10 +313,10 @@ local render = function(image)
317313
absolute_x = screen_pos.col - 1
318314
absolute_y = screen_pos.row
319315
end
320-
-- apply render_offset_top offset if set (but not for floating windows and not during partial scroll)
316+
-- apply render_offset_top except for floating windows or during partial scroll
321317
local is_floating = window and window.is_floating or false
322-
if image.render_offset_top and image.render_offset_top > 0 and not is_floating and not is_partial_scroll then
323-
absolute_y = absolute_y + image.render_offset_top
318+
if not is_floating and not is_partial_scroll then
319+
absolute_y = absolute_y + (image.render_offset_top or 0)
324320
end
325321
end
326322

0 commit comments

Comments
 (0)