Skip to content

Commit

Permalink
feat: namespace integration images
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd committed Dec 29, 2023
1 parent 9b6248b commit ec57744
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 1 addition & 3 deletions lua/image/image.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ end
---@return number?
function Image:get_extmark_id()
local extmark = buf_extmark_map[self.buffer .. ":" .. self.geometry.y]
if extmark then
return extmark.id
end
if extmark then return extmark.id end
end

---@param geometry? ImageGeometry
Expand Down
17 changes: 10 additions & 7 deletions lua/image/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,20 @@ api.clear = function(id)
end
end

---@param opts? { window?: number, buffer?: number }
---@param opts? { window?: number, buffer?: number, namespace?: string }
---@return Image[]
api.get_images = function(opts)
local images = {}
local namespace = opts and opts.namespace or nil
for _, current_image in pairs(state.images) do
if
(opts and opts.window and opts.window == current_image.window and not opts.buffer)
or (opts and opts.window and opts.window == current_image.window and opts.buffer and opts.buffer == current_image.buffer)
or not opts
then
table.insert(images, current_image)
if (namespace and current_image.namespace == namespace) or not namespace then
if
(opts and opts.window and opts.window == current_image.window and not opts.buffer)
or (opts and opts.window and opts.window == current_image.window and opts.buffer and opts.buffer == current_image.buffer)
or not opts
then
table.insert(images, current_image)
end
end
end
return images
Expand Down
5 changes: 4 additions & 1 deletion lua/image/utils/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ local create_document_integration = function(config)
local previous_images = ctx.api.get_images({
window = window.id,
buffer = window.buffer,
namespace = config.name,
})
local new_image_ids = {}
local file_path = vim.api.nvim_buf_get_name(window.buffer)
Expand Down Expand Up @@ -91,6 +92,7 @@ local create_document_integration = function(config)
window = item.window.id,
buffer = item.window.buffer,
with_virtual_padding = true,
namespace = config.name,
}, function(image)
if not image then return end
render_image(image)
Expand All @@ -103,6 +105,7 @@ local create_document_integration = function(config)
window = item.window.id,
buffer = item.window.buffer,
with_virtual_padding = true,
namespace = config.name,
})
if ok and image then render_image(image) end
end
Expand Down Expand Up @@ -163,7 +166,7 @@ local create_document_integration = function(config)
callback = function(args)
if not has_valid_filetype(ctx, vim.bo[args.buf].filetype) then return end
local current_window = vim.api.nvim_get_current_win()
local images = ctx.api.get_images({ window = current_window })
local images = ctx.api.get_images({ window = current_window, namespace = config.name })
for _, image in ipairs(images) do
image:clear()
end
Expand Down
4 changes: 3 additions & 1 deletion lua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
---@field from_file fun(path: string, options?: ImageOptions): Image|nil
---@field from_url fun(url: string, options?: ImageOptions, callback: fun(image: Image|nil))
---@field clear fun(id?: string)
---@field get_images fun(opts?: { window?: number, buffer?: number }): Image[]
---@field get_images fun(opts?: { window?: number, buffer?: number, namespace?: string }): Image[]
---@field hijack_buffer fun(path: string, window?: number, buffer?: number, options?: ImageOptions): Image|nil

---@class State
Expand Down Expand Up @@ -62,6 +62,7 @@
---@field window? number
---@field buffer? number
---@field with_virtual_padding? boolean
---@field namespace? string

---@class ImageBounds
---@field top number
Expand Down Expand Up @@ -110,6 +111,7 @@
---@field brightness fun(self: Image, brightness: number)
---@field saturation fun(self: Image, saturation: number)
---@field hue fun(self: Image, hue: number)
---@field namespace? string

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

0 comments on commit ec57744

Please sign in to comment.