From b10bb327f3909a215d65bc30b10cef3aaed5459b Mon Sep 17 00:00:00 2001 From: Charly Delay <0xcharly@users.noreply.github.com> Date: Sat, 30 Oct 2021 19:05:20 +0900 Subject: [PATCH] misc.merge: ensure that `misc.none` is never returned (#441) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change: 1. `misc.merge({a = misc.none}, {a = nil})` returned `{a = misc.none}` 2. `misc.merge({a = nil}, {a = misc.none})` returned `{a = misc.none}` (1) can cause error if a non-existing mapping is set to `config.disable` (which is an alias for `misc.none`). (2) does not cause any issue to date, but is inconsistent with the expected behavior of `misc.merge(…)`. After this change: 1. `misc.merge({a = misc.none}, {a = nil})` returns `{a = nil}` 2. `misc.merge({a = nil}, {a = misc.none})` returns `{a = nil}` Fixes #440. --- lua/cmp/utils/misc.lua | 8 ++++++-- lua/cmp/utils/misc_spec.lua | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index 90d07cf..8ce41d4 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -46,7 +46,7 @@ misc.merge = function(v1, v2) new_tbl[k] = misc.merge(v1[k], v) end for k, v in pairs(v1) do - if v2[k] == nil then + if v2[k] == nil and v ~= misc.none then new_tbl[k] = v end end @@ -56,7 +56,11 @@ misc.merge = function(v1, v2) return nil end if v1 == nil then - return v2 + if v2 == misc.none then + return nil + else + return v2 + end end if v1 == true then if merge2 then diff --git a/lua/cmp/utils/misc_spec.lua b/lua/cmp/utils/misc_spec.lua index d7cec0b..4e705ef 100644 --- a/lua/cmp/utils/misc_spec.lua +++ b/lua/cmp/utils/misc_spec.lua @@ -33,5 +33,19 @@ describe('misc', function() }, }) assert.are.equal(merged.a, nil) + + merged = misc.merge({ + a = misc.none, + }, { + a = nil, + }) + assert.are.equal(merged.a, nil) + + merged = misc.merge({ + a = nil, + }, { + a = misc.none, + }) + assert.are.equal(merged.a, nil) end) end)