lua: complete methods in metatables

This commit is contained in:
Björn Linse
2021-01-23 11:22:34 +01:00
committed by chentau
parent d95a465b43
commit d82688973d

View File

@@ -562,19 +562,21 @@ function vim._expand_pat(pat, env)
local parts, search_index = vim._expand_pat_get_parts(last_part) local parts, search_index = vim._expand_pat_get_parts(last_part)
local match_pat = '^' .. string.sub(last_part, search_index, #last_part) local match_part = string.sub(last_part, search_index, #last_part)
local prefix_match_pat = string.sub(pat, 1, #pat - #match_pat + 1) or '' local prefix_match_pat = string.sub(pat, 1, #pat - #match_part) or ''
local final_env = env local final_env = env
for _, part in ipairs(parts) do for _, part in ipairs(parts) do
if type(final_env) ~= 'table' then if type(final_env) ~= 'table' then
return {}, 0 return {}, 0
end end
local key
-- Normally, we just have a string -- Normally, we just have a string
-- Just attempt to get the string directly from the environment -- Just attempt to get the string directly from the environment
if type(part) == "string" then if type(part) == "string" then
final_env = rawget(final_env, part) key = part
else else
-- However, sometimes you want to use a variable, and complete on it -- However, sometimes you want to use a variable, and complete on it
-- With this, you have the power. -- With this, you have the power.
@@ -593,23 +595,42 @@ function vim._expand_pat(pat, env)
return {}, 0 return {}, 0
end end
final_env = rawget(final_env, result) key = result
end 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 if not final_env then
return {}, 0 return {}, 0
end end
end end
local result = vim.tbl_map(function(v) local keys = {}
return v local function insert_keys(obj)
end, vim.tbl_filter(function(name) for k,_ in pairs(obj) do
return string.find(name, match_pat) ~= nil if type(k) == "string" and string.sub(k,1,string.len(match_part)) == match_part then
end, vim.tbl_keys(final_env))) table.insert(keys,k)
end
end
end
table.sort(result) 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
return result, #prefix_match_pat table.sort(keys)
return keys, #prefix_match_pat
end end
vim._expand_pat_get_parts = function(lua_string) vim._expand_pat_get_parts = function(lua_string)