From ea2ba4df23a8aec2b97d758b014a89394a9462c3 Mon Sep 17 00:00:00 2001 From: runiq Date: Thu, 12 Jan 2023 22:10:21 +0100 Subject: [PATCH] Merge lists in lazy.core.util.merge --- lua/lazy/core/util.lua | 10 ++++++++++ tests/core/util_spec.lua | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lua/lazy/core/util.lua b/lua/lazy/core/util.lua index bc7720d..41ed88e 100644 --- a/lua/lazy/core/util.lua +++ b/lua/lazy/core/util.lua @@ -302,6 +302,10 @@ local function can_merge(v) return type(v) == "table" and (vim.tbl_isempty(v) or not M.is_list(v)) end +local function can_merge_list(v) + return type(v) == "table" and M.is_list(v) +end + --- Merges the values similar to vim.tbl_deep_extend with the **force** behavior, --- but the values can be any type, in which case they override the values on the left. --- Values will me merged in-place in the first left-most table. If you want the result to be in @@ -324,6 +328,12 @@ function M.merge(...) for k, v in pairs(value) do ret[k] = M.merge(ret[k], v) end + elseif can_merge_list(ret) and can_merge_list(value) then + for _, v in ipairs(value) do + if not vim.tbl_contains(ret, v) then + ret[#ret+1] = v + end + end elseif value == vim.NIL then ret = nil else diff --git a/tests/core/util_spec.lua b/tests/core/util_spec.lua index 87102d3..67e7017 100644 --- a/tests/core/util_spec.lua +++ b/tests/core/util_spec.lua @@ -145,7 +145,11 @@ describe("util", function() }, { input = { { a = { 1, 2 } }, { a = { 3 } } }, - output = { a = { 3 } }, + output = { a = { 1, 2, 3 } }, + }, + { + input = { { a = { 1, 2 } }, { a = { 1, 3 } } }, + output = { a = { 1, 2, 3 } }, }, { input = { { b = { 1, 2 } }, { a = { 3 }, b = vim.NIL } },