From 35d74a403f7c0397c7c0885f97024c913d798b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Wed, 10 Jul 2019 20:24:23 +0200 Subject: [PATCH] is complete --- lua/luadev/complete.lua | 33 +++++++++++++++++++++++++++++++++ plugin/luadev.vim | 1 + 2 files changed, 34 insertions(+) create mode 100644 lua/luadev/complete.lua diff --git a/lua/luadev/complete.lua b/lua/luadev/complete.lua new file mode 100644 index 0000000..fc59ec9 --- /dev/null +++ b/lua/luadev/complete.lua @@ -0,0 +1,33 @@ +local function complete() + local line = vim.api.nvim_get_current_line() + local endcol = vim.api.nvim_win_get_cursor(0)[2] + local text = string.sub(line,1,endcol) + local x,y,z = string.match(text,"([%.%w%[%]_]-)([:%.%[])([%w_]-)$") + --require'luadev'.print(x,y,z) + local status, obj + if x ~= nil then + status, obj = pcall(loadstring("return "..x)) + else + status, obj = true, _G + y, z = ".", string.match(text,"([%w_]-)$") + end + --require'luadev'.print(status,obj,y,z) + local entries = {} + -- TODO: metatable of userdata? (and for tables..) + if (not status) or type(obj) ~= 'table' then + return + end + for k,_ in pairs(obj) do + if type(k) == "string" and k:sub(1,string.len(z)) == z then + table.insert(entries,k) + end + end + local start = endcol - string.len(z) + 1 + table.sort(entries) + --require'luadev'.print(vim.inspect(entries)) + -- BUG/REGRESSION?? complete() doesn't respect 'completeopt' + vim.api.nvim_call_function("complete", {start, entries}) +end +-- TODO: luadev should add a command to source a file like this: +--package.loaded['luadev.complete']=complete +return complete diff --git a/plugin/luadev.vim b/plugin/luadev.vim index 9093956..ce55f49 100644 --- a/plugin/luadev.vim +++ b/plugin/luadev.vim @@ -3,6 +3,7 @@ command! Luadev lua require'luadev'.start() noremap (Luadev-RunLine) lua require'luadev'.exec(vim.api.nvim_get_current_line()) vnoremap (Luadev-Run) :call luaeval("require'luadev'.exec(_A)", get_visual_selection()) noremap (Luadev-RunWord) :call luaeval("require'luadev'.exec(_A)", get_current_word()) +inoremap (Luadev-Complete) lua require'luadev.complete'() " thanks to @xolox on stackoverflow function! s:get_visual_selection()