Skip to content

Commit

Permalink
support cpying self-referenced table (#2076)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrsh7th authored Nov 2, 2024
1 parent 7d40510 commit f17d9b4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
41 changes: 26 additions & 15 deletions lua/cmp/utils/misc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,39 @@ misc.set = function(t, keys, v)
c[keys[#keys]] = v
end

---Copy table
---@generic T
---@param tbl T
---@return T
misc.copy = function(tbl)
if type(tbl) ~= 'table' then
return tbl
end
do
local function do_copy(tbl, seen)
if type(tbl) ~= 'table' then
return tbl
end
if seen[tbl] then
return seen[tbl]
end

if islist(tbl) then
local copy = {}
seen[tbl] = copy
for i, value in ipairs(tbl) do
copy[i] = do_copy(value, seen)
end
return copy
end

if islist(tbl) then
local copy = {}
for i, value in ipairs(tbl) do
copy[i] = misc.copy(value)
seen[tbl] = copy
for key, value in pairs(tbl) do
copy[key] = do_copy(value, seen)
end
return copy
end

local copy = {}
for key, value in pairs(tbl) do
copy[key] = misc.copy(value)
---Copy table
---@generic T
---@param tbl T
---@return T
misc.copy = function(tbl)
return do_copy(tbl, {})
end
return copy
end

---Safe version of vim.str_utfindex
Expand Down
27 changes: 27 additions & 0 deletions lua/cmp/utils/misc_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ local misc = require('cmp.utils.misc')
describe('misc', function()
before_each(spec.before)

it('copy', function()
-- basic.
local tbl, copy
tbl = {
a = {
b = 1,
},
}
copy = misc.copy(tbl)
assert.are_not.equal(tbl, copy)
assert.are_not.equal(tbl.a, copy.a)
assert.are.same(tbl, copy)

-- self reference.
tbl = {
a = {
b = 1,
},
}
tbl.a.c = tbl.a
copy = misc.copy(tbl)
assert.are_not.equal(tbl, copy)
assert.are_not.equal(tbl.a, copy.a)
assert.are_not.equal(tbl.a.c, copy.a.c)
assert.are.same(tbl, copy)
end)

it('merge', function()
local merged
merged = misc.merge({
Expand Down

0 comments on commit f17d9b4

Please sign in to comment.