Skip to content

Commit

Permalink
fix: wrong positioning, window/gutter offset math, ty #6
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Jul 11, 2023
1 parent 847f5d6 commit c957678
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lua/image/integrations/markdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ local render = vim.schedule_wrap(

---@param image Image
local render_image = function(image)
-- utils.debug("[markdown] rendering image", id, window)
if ctx.options.sizing_strategy == "height-from-empty-lines" then
local empty_line_count = -1
local lines = vim.api.nvim_buf_get_lines(window.buffer, 0, -1, false)
Expand All @@ -84,6 +83,7 @@ local render = vim.schedule_wrap(
end
height = math.max(1, empty_line_count)
end
-- utils.debug(("[markdown] rendering image %s at x=%d y=%d"):format( match.url, match.range.start_col, match.range.start_row + 1))
image:render({
height = height,
x = match.range.start_col,
Expand Down
36 changes: 25 additions & 11 deletions lua/image/renderer.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
local utils = require("image/utils")
local magick = require("image/magick")

---@param window_id number
---@return { x: number, y: number }
local get_global_offsets = function()
local get_global_offsets = function(window_id)
local x = 0
local y = 0
if vim.opt.number then x = x + vim.opt.numberwidth:get() end
if vim.opt.signcolumn ~= "no" then x = x + 2 end
-- if vim.opt.number then x = x + vim.opt.numberwidth:get() end
-- if vim.opt.signcolumn ~= "no" then x = x + 2 end

local opts = vim.wo[window_id]
if not opts then return { x = x, y = y } end

-- tabline
if vim.opt.showtabline == 2 then y = y + 1 end
if vim.opt.winbar ~= "none" then y = y + 1 end

-- winbar
if opts.winbar ~= "" then y = y + 1 end

-- gutters
local wininfo = vim.fn.getwininfo(window_id)
if wininfo and wininfo[1] then x = x + wininfo[1].textoff end

return { x = x, y = y }
end

Expand Down Expand Up @@ -104,9 +117,9 @@ local render = function(image)
end

-- global offsets
local global_offsets = get_global_offsets()
local global_offsets = get_global_offsets(window.id)
x_offset = global_offsets.x - window.scroll_x
y_offset = global_offsets.y + 1 - window.scroll_y
y_offset = global_offsets.y - window.scroll_y

-- window offsets
window_offset_x = window.x
Expand Down Expand Up @@ -224,6 +237,7 @@ local render = function(image)
-- folds
local offset = 0
local current_win = vim.api.nvim_get_current_win()
-- TODO: can this be done without switching windows?
vim.api.nvim_command("noautocmd call nvim_set_current_win(" .. image.window .. ")")

if vim.wo.foldenable then
Expand All @@ -248,10 +262,10 @@ local render = function(image)

-- clear out of bounds images
if
absolute_y + height < bounds.top
or absolute_y > bounds.bottom
or absolute_x + width < bounds.left
or absolute_x > bounds.right
absolute_y + height <= bounds.top
or absolute_y >= bounds.bottom
or absolute_x + width <= bounds.left
or absolute_x >= bounds.right
then
if image.is_rendered then
-- utils.debug("deleting out of bounds image", { id = image.id, x = absolute_x, y = absolute_y, width = width, height = height, bounds = bounds })
Expand Down Expand Up @@ -349,10 +363,10 @@ local render = function(image)
image.crop_hash = nil
end

-- utils.debug("render", image)
image.bounds = bounds
state.backend.render(image, absolute_x, absolute_y, width, height)
image.rendered_geometry = rendered_geometry
-- utils.debug("rendered", image)

return true
end
Expand Down
2 changes: 1 addition & 1 deletion lua/image/utils/window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local get_windows = function(opts)
local buffer_filetype = vim.bo[buffer].filetype
local buffer_is_listed = #bufinfo == 1 and bufinfo[1].listed == 1
local scroll_x = 0 -- TODO
local scroll_y = tonumber(vim.fn.win_execute(id, "echo line('w0')"))
local scroll_y = tonumber(vim.fn.win_execute(id, "echo line('w0')")) - 1
local is_visible = true

local window = {
Expand Down

0 comments on commit c957678

Please sign in to comment.