Skip to content

Commit

Permalink
fix(input): lua function completion
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Aug 23, 2022
1 parent d886a1b commit 96b09a0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lua/dressing/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,20 @@ M.completefunc = function(findstart, base)
local pieces = split(completion, ",")
if pieces[1] == "custom" or pieces[1] == "customlist" then
local vimfunc = pieces[2]
local ret = vim.fn[vimfunc](base, base, vim.fn.strlen(base))
local ret
if vim.startswith(vimfunc, "v:lua.") then
local load_func = string.format("return %s(...)", vimfunc:sub(7))
local luafunc, err = loadstring(load_func)
if not luafunc then
vim.api.nvim_err_writeln(
string.format("Could not find completion function %s: %s", vimfunc, err)
)
return {}
end
ret = luafunc(base, base, vim.fn.strlen(base))
else
ret = vim.fn[vimfunc](base, base, vim.fn.strlen(base))
end
if pieces[1] == "custom" then
ret = split(ret, "\n")
end
Expand Down
16 changes: 16 additions & 0 deletions tests/manual/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ local cases = {
prompt = "Complete customlist: ",
completion = "customlist,CustomCompleteList",
},
{
prompt = "Complete custom lua: ",
completion = "custom,v:lua.custom_complete_func",
},
{
prompt = "Complete customlist: ",
completion = "customlist,v:lua.custom_complete_list",
},
}

vim.cmd([[
Expand All @@ -30,6 +38,14 @@ function! CustomCompleteList(arglead, cmdline, cursorpos)
endfunction
]])

function _G.custom_complete_func(arglead, cmdline, cursorpos)
return "first\nsecond\nthird"
end

function _G.custom_complete_list(arglead, cmdline, cursorpos)
return { "first", "second", "third" }
end

local function next()
local opts = cases[idx]
if opts then
Expand Down

0 comments on commit 96b09a0

Please sign in to comment.