diff options
author | Björn Linse <bjorn.linse@gmail.com> | 2021-01-23 11:22:34 +0100 |
---|---|---|
committer | chentau <tchen1998@gmail.com> | 2021-01-26 17:04:32 -0800 |
commit | d82688973dfce2fe0d25acb9c9a0c34dc255c893 (patch) | |
tree | c62f6c3ea9a6b9223c53b7f300dc2d6f4c86a832 /src/nvim/lua/vim.lua | |
parent | d95a465b4399c3c10b83925935ec5f4807d65b60 (diff) | |
download | rneovim-d82688973dfce2fe0d25acb9c9a0c34dc255c893.tar.gz rneovim-d82688973dfce2fe0d25acb9c9a0c34dc255c893.tar.bz2 rneovim-d82688973dfce2fe0d25acb9c9a0c34dc255c893.zip |
lua: complete methods in metatables
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r-- | src/nvim/lua/vim.lua | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index 49c34c5b89..dbf4f6014c 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -562,19 +562,21 @@ function vim._expand_pat(pat, env) local parts, search_index = vim._expand_pat_get_parts(last_part) - local match_pat = '^' .. string.sub(last_part, search_index, #last_part) - local prefix_match_pat = string.sub(pat, 1, #pat - #match_pat + 1) or '' + local match_part = string.sub(last_part, search_index, #last_part) + local prefix_match_pat = string.sub(pat, 1, #pat - #match_part) or '' local final_env = env + for _, part in ipairs(parts) do if type(final_env) ~= 'table' then return {}, 0 end + local key -- Normally, we just have a string -- Just attempt to get the string directly from the environment if type(part) == "string" then - final_env = rawget(final_env, part) + key = part else -- However, sometimes you want to use a variable, and complete on it -- With this, you have the power. @@ -593,23 +595,42 @@ function vim._expand_pat(pat, env) return {}, 0 end - final_env = rawget(final_env, result) + key = result + end + local field = rawget(final_env, key) + if field == nil then + local mt = getmetatable(final_env) + if mt and type(mt.__index) == "table" then + field = rawget(mt.__index, key) + end end + final_env = field if not final_env then return {}, 0 end end - local result = vim.tbl_map(function(v) - return v - end, vim.tbl_filter(function(name) - return string.find(name, match_pat) ~= nil - end, vim.tbl_keys(final_env))) + local keys = {} + local function insert_keys(obj) + for k,_ in pairs(obj) do + if type(k) == "string" and string.sub(k,1,string.len(match_part)) == match_part then + table.insert(keys,k) + end + end + end + + if type(final_env) == "table" then + insert_keys(final_env) + end + local mt = getmetatable(final_env) + if mt and type(mt.__index) == "table" then + insert_keys(mt.__index) + end - table.sort(result) + table.sort(keys) - return result, #prefix_match_pat + return keys, #prefix_match_pat end vim._expand_pat_get_parts = function(lua_string) |