From 96b09a0e3c7c457140303c796bd84f13cfd9dbc0 Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 23 Aug 2022 08:41:21 -0700 Subject: [PATCH] fix(input): lua function completion --- lua/dressing/input.lua | 15 ++++++++++++++- tests/manual/completion.lua | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lua/dressing/input.lua b/lua/dressing/input.lua index 82827c7..f4aea97 100644 --- a/lua/dressing/input.lua +++ b/lua/dressing/input.lua @@ -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 diff --git a/tests/manual/completion.lua b/tests/manual/completion.lua index f40aba5..5e64683 100644 --- a/tests/manual/completion.lua +++ b/tests/manual/completion.lua @@ -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([[ @@ -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