feat(lua): omnifunc for builting lua interpreter

also make implicit submodules "uri" and "_inspector" work with completion

this is needed for `:lua=vim.uri_<tab>` wildmenu completion
to work even before uri or _inspector functions are used.
This commit is contained in:
Björn Linse
2021-01-27 09:00:28 +01:00
committed by bfredl
parent ed05d38d9f
commit 79571b92ce
6 changed files with 79 additions and 6 deletions

View File

@@ -42,6 +42,18 @@ for k, v in pairs({
vim._submodules[k] = v
end
-- There are things which have special rules in vim._init_packages
-- for legacy reasons (uri) or for performance (_inspector).
-- most new things should go into a submodule namespace ( vim.foobar.do_thing() )
vim._extra = {
uri_from_fname = true,
uri_from_bufnr = true,
uri_to_fname = true,
uri_to_bufnr = true,
show_pos = true,
inspect_pos = true,
}
vim.log = {
levels = {
TRACE = 0,
@@ -575,15 +587,13 @@ function vim._on_key(char)
end
--- Generate a list of possible completions for the string.
--- String starts with ^ and then has the pattern.
--- String has the pattern.
---
--- 1. Can we get it to just return things in the global namespace with that name prefix
--- 2. Can we get it to return things from global namespace even with `print(` in front.
function vim._expand_pat(pat, env)
env = env or _G
pat = string.sub(pat, 2, #pat)
if pat == '' then
local result = vim.tbl_keys(env)
table.sort(result)
@@ -644,7 +654,7 @@ function vim._expand_pat(pat, env)
local mt = getmetatable(final_env)
if mt and type(mt.__index) == 'table' then
field = rawget(mt.__index, key)
elseif final_env == vim and vim._submodules[key] then
elseif final_env == vim and (vim._submodules[key] or vim._extra[key]) then
field = vim[key]
end
end
@@ -674,6 +684,7 @@ function vim._expand_pat(pat, env)
end
if final_env == vim then
insert_keys(vim._submodules)
insert_keys(vim._extra)
end
keys = vim.tbl_keys(keys)
@@ -745,6 +756,28 @@ vim._expand_pat_get_parts = function(lua_string)
return parts, search_index
end
do
-- Ideally we should just call complete() inside omnifunc, though there are
-- some bugs, so fake the two-step dance for now.
local matches
--- Omnifunc for completing lua values from from the runtime lua interpreter,
--- similar to the builtin completion for the `:lua` command.
---
--- Activate using `set omnifunc=v:lua.vim.lua_omnifunc` in a lua buffer.
function vim.lua_omnifunc(find_start, _)
if find_start == 1 then
local line = vim.api.nvim_get_current_line()
local prefix = string.sub(line, 1, vim.api.nvim_win_get_cursor(0)[2])
local pos
matches, pos = vim._expand_pat(prefix)
return (#matches > 0 and pos) or -1
else
return matches
end
end
end
---Prints given arguments in human-readable format.
---Example:
---<pre>lua