From ec577441edca399c43d77b522870dbd7b9cd38b8 Mon Sep 17 00:00:00 2001 From: 3rd <3rd@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:18:21 +0200 Subject: [PATCH] feat: namespace integration images --- lua/image/image.lua | 4 +--- lua/image/init.lua | 17 ++++++++++------- lua/image/utils/document.lua | 5 ++++- lua/types.lua | 4 +++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lua/image/image.lua b/lua/image/image.lua index 044499c..c036a90 100644 --- a/lua/image/image.lua +++ b/lua/image/image.lua @@ -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 diff --git a/lua/image/init.lua b/lua/image/init.lua index 9b02a1b..08c024d 100644 --- a/lua/image/init.lua +++ b/lua/image/init.lua @@ -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 diff --git a/lua/image/utils/document.lua b/lua/image/utils/document.lua index b0c327f..a4f962c 100644 --- a/lua/image/utils/document.lua +++ b/lua/image/utils/document.lua @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/lua/types.lua b/lua/types.lua index 8928e58..4db0736 100644 --- a/lua/types.lua +++ b/lua/types.lua @@ -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 @@ -62,6 +62,7 @@ ---@field window? number ---@field buffer? number ---@field with_virtual_padding? boolean +---@field namespace? string ---@class ImageBounds ---@field top number @@ -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