From a339421f4bdc52fdca970d245e5eadf1f3126e6a Mon Sep 17 00:00:00 2001 From: hrsh7th Date: Sun, 29 Aug 2021 14:31:24 +0900 Subject: [PATCH] Fix merge bug --- README.md | 2 +- lua/cmp/utils/misc.lua | 4 ++-- lua/cmp/utils/misc_spec.lua | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 lua/cmp/utils/misc_spec.lua diff --git a/README.md b/README.md index c04cdf2..d448f27 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ source README which defines the source name as `buffer`. Which events should trigger `autocompletion`. -If you set this to empty or `nil`, `nvim-cmp` will not perform completion +If you set this to empty or `false`, `nvim-cmp` will not perform completion automatically. You can still use manual completion though (like omni-completion via the `cmp.mapping.complete` function). diff --git a/lua/cmp/utils/misc.lua b/lua/cmp/utils/misc.lua index a0f2773..be28497 100644 --- a/lua/cmp/utils/misc.lua +++ b/lua/cmp/utils/misc.lua @@ -21,8 +21,8 @@ end ---@param v2 T ---@return T misc.merge = function(v1, v2) - local merge1 = type(v1) == "table" and not vim.tbl_islist(v1) - local merge2 = type(v2) == "table" and not vim.tbl_islist(v1) + local merge1 = type(v1) == "table" and (not vim.tbl_islist(v1) or vim.tbl_isempty(v1)) + local merge2 = type(v2) == "table" and (not vim.tbl_islist(v1) or vim.tbl_isempty(v1)) if merge1 and merge2 then local new_tbl = {} for k, v in pairs(v2) do diff --git a/lua/cmp/utils/misc_spec.lua b/lua/cmp/utils/misc_spec.lua new file mode 100644 index 0000000..8dad68e --- /dev/null +++ b/lua/cmp/utils/misc_spec.lua @@ -0,0 +1,29 @@ +local spec = require('cmp.utils.spec') + +local misc = require('cmp.utils.misc') + +describe('misc', function() + before_each(spec.before) + + it('merge', function() + local merged + merged = misc.merge({ + a = {} + }, { + a = { + b = 1 + } + }) + assert.are.equal(merged.a.b, 1) + merged = misc.merge({ + a = false + }, { + a = { + b = 1 + } + }) + assert.are.equal(merged.a, false) + + end) +end) +