aboutsummaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorBjörn Linse <bjorn.linse@gmail.com>2020-01-01 21:03:08 +0100
committerGitHub <noreply@github.com>2020-01-01 21:03:08 +0100
commit8645d480bd469d6bffa828bf220de6afc324b43c (patch)
tree1e0ca0a7e1d0727f10d4348ab1b582145dd27029 /runtime
parenta495d49012586696f2244a589a4d34770ad07d17 (diff)
parentea4127e9a7a624484f51c21e17f37c766da15da0 (diff)
downloadrneovim-8645d480bd469d6bffa828bf220de6afc324b43c.tar.gz
rneovim-8645d480bd469d6bffa828bf220de6afc324b43c.tar.bz2
rneovim-8645d480bd469d6bffa828bf220de6afc324b43c.zip
Merge pull request #11470 from bfredl/emptytable
metatable for empty dict value
Diffstat (limited to 'runtime')
-rw-r--r--runtime/doc/lua.txt10
-rw-r--r--runtime/lua/vim/inspect.lua5
-rw-r--r--runtime/lua/vim/shared.lua15
3 files changed, 28 insertions, 2 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index 9601537c8d..d1f244c76f 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -717,6 +717,16 @@ vim.NIL *vim.NIL*
is equivalent to a missing value: `{"foo", nil}` is the same as
`{"foo"}`
+vim.empty_dict() *vim.empty_dict()*
+ Creates a special table which will be converted to an empty
+ dictionary when converting lua values to vimL or API types. The
+ table is empty, and this property is marked using a metatable. An
+ empty table `{}` without this metatable will default to convert to
+ an array/list.
+
+ Note: if numeric keys are added to the table, the metatable will be
+ ignored and the dict converted to a list/array anyway.
+
vim.rpcnotify({channel}, {method}[, {args}...]) *vim.rpcnotify()*
Sends {event} to {channel} via |RPC| and returns immediately.
If {channel} is 0, the event is broadcast to all channels.
diff --git a/runtime/lua/vim/inspect.lua b/runtime/lua/vim/inspect.lua
index 0f3b908dc1..0448ea487f 100644
--- a/runtime/lua/vim/inspect.lua
+++ b/runtime/lua/vim/inspect.lua
@@ -244,6 +244,11 @@ function Inspector:putTable(t)
local nonSequentialKeys, nonSequentialKeysLength, sequenceLength = getNonSequentialKeys(t)
local mt = getmetatable(t)
+ if (vim and sequenceLength == 0 and nonSequentialKeysLength == 0
+ and mt == vim._empty_dict_mt) then
+ self:puts(tostring(t))
+ return
+ end
self:puts('{')
self:down(function()
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua
index b5b04d7757..6df9bf1c2f 100644
--- a/runtime/lua/vim/shared.lua
+++ b/runtime/lua/vim/shared.lua
@@ -275,9 +275,15 @@ function vim.tbl_flatten(t)
end
-- Determine whether a Lua table can be treated as an array.
+--
+-- An empty table `{}` will default to being treated as an array.
+-- Use `vim.emtpy_dict()` to create a table treated as an
+-- empty dict. Empty tables returned by `rpcrequest()` and
+-- `vim.fn` functions can be checked using this function
+-- whether they represent empty API arrays and vimL lists.
---
--@params Table
---@returns true: A non-empty array, false: A non-empty table, nil: An empty table
+--@returns true: An array-like table, false: A dict-like or mixed table
function vim.tbl_islist(t)
if type(t) ~= 'table' then
return false
@@ -296,7 +302,12 @@ function vim.tbl_islist(t)
if count > 0 then
return true
else
- return nil
+ -- TODO(bfredl): in the future, we will always be inside nvim
+ -- then this check can be deleted.
+ if vim._empty_dict_mt == nil then
+ return nil
+ end
+ return getmetatable(t) ~= vim._empty_dict_mt
end
end