Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lua/diffview/hl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/diffview/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/diffview/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""

Expand Down
4 changes: 3 additions & 1 deletion lua/diffview/oop.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local fmt = string.format
local lazy = require("diffview.lazy")
local utils = lazy.require("diffview.utils") ---@module "diffview.utils"

local M = {}

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lua/diffview/stream.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions lua/diffview/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : { [integer]: T }
---@alias falsy false|nil
---@alias truthy true|number|string|table|function|thread|userdata
Expand Down Expand Up @@ -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
Expand Down