lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Luís Eduardo Jason Santos wrote:
We have a LuaState loaded with the standard library and all code completion comes from there.

As of version 1.3.1 users can add arbitrary 'require' calls to this environment -- thus, being able to add lua and binary modules to the code completion. I didn't want to do this automatically for I cannot anticipate the kind of problems a given module might bring if I load it at edit time.

Some months ago I wrote a Vim plug-in (see attachment) that enables completion of all global variables in the main state of the Lua Interface to Vim. By calling require() from a :lua command the user can add arbitrary Lua modules to the completion list. Unfortunately I can't test or use the plug-in anymore because I can't get the Lua Interface to Vim compiled :-) which means I'm not sure whether the plug-in works out of the box.

 - Peter Odding
" Vim file type plug-in
" Maintainer: Peter Odding <>
" Last Change: January 19, 2010

" When you've compiled Vim with the Lua interface and you've placed this file
" in "~/.vim/after/ftplugin/lua.vim" (or "~\vimfiles\after\ftplugin\lua.vim"
" when you're on Windows) Vim will support dynamic completion of global
" identifiers using omni completion (CTRL-X CTRL-O). If you want your favorite
" modules included in the completion, simply require() them from a :lua
" command.

" Don't source the script when the Lua interface isn't supported.
if !has('lua')
  echomsg "Failed to load dynamic completion! (Lua interface not available)"

" Register the dynamic completion function.
setlocal omnifunc=LuaInterfaceComplete

" Don't source this script again when it isn't needed.
if exists('*LuaInterfaceComplete')

" Define the dynamic completion function.
function LuaInterfaceComplete(findstart, base)

  " Find start of identifier before text cursor.
  if a:findstart
    let before = getline('.')[0 : col('.')-1]
    return match(before, '\ze\w\+\(\.\w\+\)*$')

  " Generate list of global identifiers.
	redir => listing
	silent lua << LUA_SCRIPT
	for k, v in pairs(_G) do
		if type(k) == 'string' then
			if type(v) == 'function' then
				print(k .. '()')
			elseif type(v) ~= 'table' then
			elseif type(v) == 'table' and k ~= '_G' then
				local m = k
				for k, v in pairs(v) do
					if type(k) == 'string' then
						if type(v) == 'function' then
							print(m .. '.' .. k .. '()')
							print(m .. '.' .. k)
  redir END

  " Return filtered list of global identifiers.
  let pattern = "^" . a:base
  return sort(filter(split(listing, "\n"), 'v:val =~ pattern'))