Skip to content

Commit 79a5476

Browse files
committed
Bug fixes and support for function signatures
* Bug fix for xolox#lua#dofile(): Unless I'm severely misunderstanding Vim I've probably found a bug in the Lua Interface for Vim: When you print() a very long line and try to intercept the output with :redir commands, the long line is repeated once more after the initial (and expected) output. This makes it impossible to send large blobs of serialized data from Lua to Vim. The workaround is simple though: Simply print() each matching entry individually and join the printed lines in Vim script before evaluating the expression. * The dynamic completion sometimes wouldn't use omni completion even when it was enabled and the best choice. This is now fixed. * The new xolox#lua#getsignature() function returns the signatures of standard library functions and is used to show signatures in tool tips and completion menus. * The standard library signatures in autoload/xolox/lua_data.vim were generated using a simple Lua script which I'm also committing.
1 parent 3aafd93 commit 79a5476

File tree

6 files changed

+407
-230
lines changed

6 files changed

+407
-230
lines changed

autoload/xolox/lua.vim

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -250,22 +250,45 @@ function! xolox#lua#completefunc(init, base) " {{{1
250250
endif
251251
let items = []
252252
if xolox#lua#getopt('lua_complete_keywords', 1)
253-
call extend(items, g:xolox#lua_complete#keywords)
253+
call extend(items, g:xolox#lua_data#keywords)
254254
endif
255255
if xolox#lua#getopt('lua_complete_globals', 1)
256-
call extend(items, g:xolox#lua_complete#globals)
256+
call extend(items, g:xolox#lua_data#globals)
257257
endif
258258
if xolox#lua#getopt('lua_complete_library', 1)
259-
call extend(items, g:xolox#lua_complete#library)
259+
call extend(items, g:xolox#lua_data#library)
260260
endif
261261
let pattern = xolox#misc#escape#pattern(a:base)
262-
return filter(items, 'v:val.word =~ pattern')
262+
call filter(items, 'v:val.word =~ pattern')
263+
return s:addsignatures(items)
263264
endfunction
264265

265266
function! s:get_completion_prefix()
266267
return match(strpart(getline('.'), 0, col('.') - 2), '\w\+\.\?\w*$')
267268
endfunction
268269

270+
function! s:addsignatures(entries)
271+
for entry in a:entries
272+
let signature = xolox#lua#getsignature(entry.word)
273+
if !empty(signature)
274+
let entry.menu = signature
275+
endif
276+
endfor
277+
return a:entries
278+
endfunction
279+
280+
function! xolox#lua#getsignature(identifier) " {{{1
281+
let identifier = substitute(a:identifier, '()$', '', '')
282+
let signature = get(g:xolox#lua_data#signatures, identifier, '')
283+
if empty(signature)
284+
let signature = get(g:xolox#lua_data#signatures, 'string.' . identifier, '')
285+
endif
286+
if empty(signature)
287+
let signature = get(g:xolox#lua_data#signatures, 'file:' . identifier, '')
288+
endif
289+
return signature
290+
endfunction
291+
269292
function! xolox#lua#omnifunc(init, base) " {{{1
270293
if a:init
271294
return s:get_completion_prefix()
@@ -277,6 +300,7 @@ function! xolox#lua#omnifunc(init, base) " {{{1
277300
endif
278301
if !exists('s:omnifunc_variables')
279302
let s:omnifunc_variables = xolox#lua#getomnivariables(s:omnifunc_modules)
303+
call s:addsignatures(s:omnifunc_variables)
280304
endif
281305
" FIXME When you type "require'" without a space in between
282306
" the getline('.') call below returns an empty string?!
@@ -287,7 +311,7 @@ function! xolox#lua#omnifunc(init, base) " {{{1
287311
return s:omnifunc_variables
288312
else
289313
let pattern = xolox#misc#escape#pattern(a:base)
290-
return filter(copy(s:omnifunc_variables), 'v:val =~ pattern')
314+
return filter(copy(s:omnifunc_variables), 'v:val.word =~ pattern')
291315
endif
292316
endfunction
293317

@@ -346,8 +370,7 @@ endfunction
346370
function! xolox#lua#getomnivariables(modules) " {{{1
347371
let starttime = xolox#misc#timer#start()
348372
let output = xolox#lua#dofile(s:omnicomplete_script, a:modules)
349-
let variables = split(output, "\n")
350-
call sort(variables, 1)
373+
let variables = eval('[' . substitute(output, '\_s\+', ',', 'g') . ']')
351374
let msg = "%s: Collected %i variables for omni completion in %s"
352375
call xolox#misc#timer#stop(msg, s:script, len(variables), starttime)
353376
return variables
@@ -369,37 +392,33 @@ function! xolox#lua#completedynamic(type) " {{{1
369392
" are available, which is kind of annoying. But I don't know of an
370393
" alternative to :silent that can be used inside of <expr>
371394
" mappings?!
372-
return a:type . "\<C-x>\<C-u>"
395+
if xolox#lua#getopt('lua_complete_omni', 0)
396+
return a:type . "\<C-x>\<C-o>"
397+
else
398+
return a:type . "\<C-x>\<C-u>"
399+
endif
373400
endif
374401
endif
375402
endif
376403
return a:type
377404
endfunction
378405

379406
function! xolox#lua#dofile(pathname, arguments) " {{{1
380-
" First try to use the Lua Interface for Vim.
381-
try
382-
call xolox#misc#msg#debug("%s: Trying Lua Interface for Vim ..", s:script)
407+
if has('lua')
408+
" Use the Lua Interface for Vim.
383409
redir => output
384410
lua arg = vim.eval('a:arguments')
385411
execute 'silent luafile' fnameescape(a:pathname)
386412
redir END
387-
if !empty(output)
388-
return output
389-
endif
390-
catch
391-
redir END
392-
call xolox#misc#msg#warn("%s: %s (at %s)", s:script, v:exception, v:throwpoint)
393-
endtry
394-
" Fall back to the command line Lua interpreter.
395-
call xolox#misc#msg#debug("Falling back to external Lua interpreter ..")
396-
let output = system(join(['lua', a:pathname] + a:arguments))
397-
if v:shell_error
398-
let msg = "%s: Failed to retrieve omni completion candidates (output: '%s')"
399-
call xolox#misc#msg#warn(msg, s:script, output)
400-
return ''
401413
else
402-
return output
414+
" Use the command line Lua interpreter.
415+
let output = xolox#misc#str#trim(system(join(['lua', a:pathname] + a:arguments)))
416+
if v:shell_error
417+
let msg = "%s: Failed to retrieve omni completion candidates (output: '%s')"
418+
call xolox#misc#msg#warn(msg, s:script, output)
419+
endif
420+
endif
421+
return xolox#misc#str#trim(output)
403422
endfunction
404423

405424
" vim: ts=2 sw=2 et

autoload/xolox/lua_complete.vim

Lines changed: 0 additions & 194 deletions
This file was deleted.

0 commit comments

Comments
 (0)