Merge pull request #13834 from bfredl/omnilua

omnifunc for builtin lua
This commit is contained in:
bfredl 2023-03-07 00:28:53 +01:00 committed by GitHub
commit 39096f48f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 6 deletions

View File

@ -1404,6 +1404,12 @@ inspect({object}, {options}) *vim.inspect()*
https://github.com/kikito/inspect.lua https://github.com/kikito/inspect.lua
https://github.com/mpeterv/vinspect https://github.com/mpeterv/vinspect
lua_omnifunc({find_start}, {_}) *vim.lua_omnifunc()*
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.
notify({msg}, {level}, {opts}) *vim.notify()* notify({msg}, {level}, {opts}) *vim.notify()*
Display a notification to the user. Display a notification to the user.

View File

@ -192,6 +192,8 @@ The following new APIs or features were added.
disabled by default and can be enabled by setting the disabled by default and can be enabled by setting the
`workspace.didChangeWatchedFiles.dynamicRegistration=true` capability. `workspace.didChangeWatchedFiles.dynamicRegistration=true` capability.
• Added an omnifunc implementation for lua, |vim.lua_omnifunc()|
============================================================================== ==============================================================================
CHANGED FEATURES *news-changes* CHANGED FEATURES *news-changes*

View File

@ -42,6 +42,18 @@ for k, v in pairs({
vim._submodules[k] = v vim._submodules[k] = v
end 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 = { vim.log = {
levels = { levels = {
TRACE = 0, TRACE = 0,
@ -575,15 +587,13 @@ function vim._on_key(char)
end end
--- Generate a list of possible completions for the string. --- 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 --- 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. --- 2. Can we get it to return things from global namespace even with `print(` in front.
function vim._expand_pat(pat, env) function vim._expand_pat(pat, env)
env = env or _G env = env or _G
pat = string.sub(pat, 2, #pat)
if pat == '' then if pat == '' then
local result = vim.tbl_keys(env) local result = vim.tbl_keys(env)
table.sort(result) table.sort(result)
@ -644,7 +654,7 @@ function vim._expand_pat(pat, env)
local mt = getmetatable(final_env) local mt = getmetatable(final_env)
if mt and type(mt.__index) == 'table' then if mt and type(mt.__index) == 'table' then
field = rawget(mt.__index, key) 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] field = vim[key]
end end
end end
@ -674,6 +684,7 @@ function vim._expand_pat(pat, env)
end end
if final_env == vim then if final_env == vim then
insert_keys(vim._submodules) insert_keys(vim._submodules)
insert_keys(vim._extra)
end end
keys = vim.tbl_keys(keys) keys = vim.tbl_keys(keys)
@ -745,6 +756,28 @@ vim._expand_pat_get_parts = function(lua_string)
return parts, search_index return parts, search_index
end 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. ---Prints given arguments in human-readable format.
---Example: ---Example:
---<pre>lua ---<pre>lua

View File

@ -1857,6 +1857,12 @@ int nlua_expand_pat(expand_T *xp, char *pat, int *num_results, char ***results)
lua_getfield(lstate, -1, "_expand_pat"); lua_getfield(lstate, -1, "_expand_pat");
luaL_checktype(lstate, -1, LUA_TFUNCTION); luaL_checktype(lstate, -1, LUA_TFUNCTION);
// ex expansion prepends a ^, but don't worry, it is not a regex
if (pat[0] != '^') {
return FAIL;
}
pat++;
// [ vim, vim._expand_pat, buf ] // [ vim, vim._expand_pat, buf ]
lua_pushlstring(lstate, (const char *)pat, strlen(pat)); lua_pushlstring(lstate, (const char *)pat, strlen(pat));

View File

@ -5,7 +5,7 @@ local eq = helpers.eq
local exec_lua = helpers.exec_lua local exec_lua = helpers.exec_lua
local get_completions = function(input, env) local get_completions = function(input, env)
return exec_lua("return {vim._expand_pat(...)}", '^' .. input, env) return exec_lua("return {vim._expand_pat(...)}", input, env)
end end
local get_compl_parts = function(parts) local get_compl_parts = function(parts)
@ -107,9 +107,12 @@ describe('nlua_expand_pat', function()
end) end)
it('should work with lazy submodules of "vim" global', function() it('should work with lazy submodules of "vim" global', function()
eq({{ 'inspect' }, 4 }, eq({{ 'inspect', 'inspect_pos' }, 4 },
get_completions('vim.inspec')) get_completions('vim.inspec'))
eq({{ 'treesitter' }, 4 },
get_completions('vim.treesi'))
eq({{ 'set' }, 11 }, eq({{ 'set' }, 11 },
get_completions('vim.keymap.se')) get_completions('vim.keymap.se'))
end) end)

View File

@ -2900,6 +2900,29 @@ describe('lua stdlib', function()
end) end)
end) end)
it('vim.lua_omnifunc', function()
local screen = Screen.new(60,5)
screen:set_default_attr_ids {
[1] = {foreground = Screen.colors.Blue1, bold = true};
[2] = {background = Screen.colors.WebGray};
[3] = {background = Screen.colors.LightMagenta};
[4] = {bold = true};
[5] = {foreground = Screen.colors.SeaGreen, bold = true};
}
screen:attach()
command [[ set omnifunc=v:lua.vim.lua_omnifunc ]]
-- Note: the implementation is shared with lua command line completion.
-- More tests for completion in lua/command_line_completion_spec.lua
feed [[ivim.insp<c-x><c-o>]]
screen:expect{grid=[[
vim.inspect^ |
{1:~ }{2: inspect }{1: }|
{1:~ }{3: inspect_pos }{1: }|
{1:~ }|
{4:-- Omni completion (^O^N^P) }{5:match 1 of 2} |
]]}
end)
end) end)
describe('lua: builtin modules', function() describe('lua: builtin modules', function()