diff --git a/lua/diffview/hl.lua b/lua/diffview/hl.lua index 0c5d5b02..891c52f5 100644 --- a/lua/diffview/hl.lua +++ b/lua/diffview/hl.lua @@ -114,8 +114,8 @@ if HAS_NVIM_0_8 then } end -vim.tbl_add_reverse_lookup(M.HlAttribute) -vim.tbl_add_reverse_lookup(style_attrs) +utils.tbl_add_reverse_lookup(M.HlAttribute) +utils.tbl_add_reverse_lookup(style_attrs) local hlattr = M.HlAttribute ---@param name string Syntax group name. @@ -242,7 +242,7 @@ function M.hi_spec_to_def_map(spec) end if spec.style then - local spec_attrs = vim.tbl_add_reverse_lookup(vim.split(spec.style, ",")) + local spec_attrs = utils.tbl_add_reverse_lookup(vim.split(spec.style, ",")) for _, attr in ipairs(style_attrs) do res[attr] = spec_attrs[attr] ~= nil diff --git a/lua/diffview/lib.lua b/lua/diffview/lib.lua index 6291f9b5..67696acd 100644 --- a/lua/diffview/lib.lua +++ b/lua/diffview/lib.lua @@ -199,7 +199,7 @@ end ---@return boolean function M.is_buf_in_use(bufnr, ignore) local ignore_map = ignore and utils.vec_slice(ignore) or {} - vim.tbl_add_reverse_lookup(ignore_map) + utils.tbl_add_reverse_lookup(ignore_map) for _, view in ipairs(M.views) do if view:instanceof(StandardView.__get()) then diff --git a/lua/diffview/logger.lua b/lua/diffview/logger.lua index 4eb2c9e8..f7975d5b 100644 --- a/lua/diffview/logger.lua +++ b/lua/diffview/logger.lua @@ -174,7 +174,7 @@ function Logger.dstring(object) if mt and mt.__tostring then return tostring(object) - elseif vim.tbl_islist(object) then + elseif utils.islist(object) then if #object == 0 then return "[]" end local s = "" diff --git a/lua/diffview/oop.lua b/lua/diffview/oop.lua index ab81bf42..7d069cfa 100644 --- a/lua/diffview/oop.lua +++ b/lua/diffview/oop.lua @@ -1,4 +1,6 @@ local fmt = string.format +local lazy = require("diffview.lazy") +local utils = lazy.require("diffview.utils") ---@module "diffview.utils" local M = {} @@ -10,7 +12,7 @@ end ---@param t T ---@return T function M.enum(t) - vim.tbl_add_reverse_lookup(t) + utils.tbl_add_reverse_lookup(t) return t end diff --git a/lua/diffview/stream.lua b/lua/diffview/stream.lua index d1bb8e88..03552a13 100644 --- a/lua/diffview/stream.lua +++ b/lua/diffview/stream.lua @@ -33,7 +33,7 @@ end ---@param src table|function function Stream:create_src(src) if type(src) == "table" then - if vim.tbl_islist(src) then + if utils.islist(src) then local itr = ipairs(src) return function() diff --git a/lua/diffview/utils.lua b/lua/diffview/utils.lua index fe7a29bc..340f5bdc 100644 --- a/lua/diffview/utils.lua +++ b/lua/diffview/utils.lua @@ -9,6 +9,9 @@ local logger = DiffviewGlobal.logger local M = {} +-- NOTE: This can be changed to `vim.islist` after Neovim 0.9 support is dropped +M.islist = vim.fn.has('nvim-0.10') == 1 and vim.islist or vim.tbl_islist + ---@class vector : { [integer]: T } ---@alias falsy false|nil ---@alias truthy true|number|string|table|function|thread|userdata @@ -546,6 +549,19 @@ function M.tbl_access(t, table_path) return cur end +---Modify `data` so every key is also a value and every value is also a key. +---Note: This function both modifies `data` and returns it, for compatibility reasons. +---@param data table<..., ...> Any data to reverse and append onto itself. +---@return table<..., ...> # The final modified data. +--- +function M.tbl_add_reverse_lookup(data) + for key, value in pairs(data) do + data[value] = key + end + + return data +end + ---Deep extend a table, and also perform a union on all sub-tables. ---@param t table ---@param ... table